Commit 0d303956 authored by Alessio Caiazza's avatar Alessio Caiazza

Merge branch 'reduce-reported-gocyclo-offenses' into 'master'

Reduce complexity of reported methods

See merge request gitlab-org/gitlab-runner!997
parents 80ffc6bf a83904ca
Pipeline #27846212 passed with stages
in 30 minutes and 15 seconds
......@@ -411,6 +411,24 @@ func (n *GitLabClient) createArtifactsForm(mpw *multipart.Writer, reader io.Read
return nil
}
func uploadRawArtifactsQuery(options common.ArtifactsOptions) url.Values {
q := url.Values{}
if options.ExpireIn != "" {
q.Set("expire_in", options.ExpireIn)
}
if options.Format != "" {
q.Set("artifact_format", string(options.Format))
}
if options.Type != "" {
q.Set("artifact_type", options.Type)
}
return q
}
func (n *GitLabClient) UploadRawArtifacts(config common.JobCredentials, reader io.Reader, options common.ArtifactsOptions) common.UploadState {
pr, pw := io.Pipe()
defer pr.Close()
......@@ -426,16 +444,7 @@ func (n *GitLabClient) UploadRawArtifacts(config common.JobCredentials, reader i
}
}()
query := url.Values{}
if options.ExpireIn != "" {
query.Set("expire_in", options.ExpireIn)
}
if options.Format != "" {
query.Set("artifact_format", string(options.Format))
}
if options.Type != "" {
query.Set("artifact_type", options.Type)
}
query := uploadRawArtifactsQuery(options)
headers := make(http.Header)
headers.Set("JOB-TOKEN", config.Token)
......
......@@ -3,6 +3,7 @@ package network
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
......@@ -981,32 +982,8 @@ func TestAbortedPatchTrace(t *testing.T) {
}
}
func testArtifactsUploadHandler(w http.ResponseWriter, r *http.Request, t *testing.T) {
if r.URL.Path != "/api/v4/jobs/10/artifacts" {
w.WriteHeader(http.StatusNotFound)
return
}
if r.Method != "POST" {
w.WriteHeader(http.StatusNotAcceptable)
return
}
if r.Header.Get("JOB-TOKEN") != "token" {
w.WriteHeader(http.StatusForbidden)
return
}
file, _, err := r.FormFile("file")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
body, err := ioutil.ReadAll(file)
assert.NoError(t, err)
switch string(body) {
func checkTestArtifactsUploadHandlerContent(w http.ResponseWriter, r *http.Request, body string) {
switch body {
case "too-large":
w.WriteHeader(http.StatusRequestEntityTooLarge)
return
......@@ -1037,6 +1014,34 @@ func testArtifactsUploadHandler(w http.ResponseWriter, r *http.Request, t *testi
w.WriteHeader(http.StatusBadRequest)
}
func testArtifactsUploadHandler(w http.ResponseWriter, r *http.Request, t *testing.T) {
if r.URL.Path != "/api/v4/jobs/10/artifacts" {
w.WriteHeader(http.StatusNotFound)
return
}
if r.Method != "POST" {
w.WriteHeader(http.StatusNotAcceptable)
return
}
if r.Header.Get("JOB-TOKEN") != "token" {
w.WriteHeader(http.StatusForbidden)
return
}
file, _, err := r.FormFile("file")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
body, err := ioutil.ReadAll(file)
require.NoError(t, err)
checkTestArtifactsUploadHandlerContent(w, r, string(body))
}
func uploadArtifacts(client *GitLabClient, config JobCredentials, artifactsFile, artifactType string, artifactFormat ArtifactFormat) UploadState {
file, err := os.Open(artifactsFile)
if err != nil {
......
......@@ -485,6 +485,55 @@ func (b *AbstractShell) cacheArchiver(w ShellWriter, info common.ShellScriptInfo
return nil
}
func (b *AbstractShell) writeUploadArtifact(w ShellWriter, info common.ShellScriptInfo, artifact common.Artifact) {
args := []string{
"artifacts-uploader",
"--url",
info.Build.Runner.URL,
"--token",
info.Build.Token,
"--id",
strconv.Itoa(info.Build.ID),
}
// Create list of files to archive
archiverArgs := []string{}
for _, path := range artifact.Paths {
archiverArgs = append(archiverArgs, "--path", path)
}
if artifact.Untracked {
archiverArgs = append(archiverArgs, "--untracked")
}
if len(archiverArgs) < 1 {
// Skip creating archive
return
}
args = append(args, archiverArgs...)
if artifact.Name != "" {
args = append(args, "--name", artifact.Name)
}
if artifact.ExpireIn != "" {
args = append(args, "--expire-in", artifact.ExpireIn)
}
if artifact.Format != "" {
args = append(args, "--artifact-format", string(artifact.Format))
}
if artifact.Type != "" {
args = append(args, "--artifact-type", artifact.Type)
}
b.guardRunnerCommand(w, info.RunnerCommand, "Uploading artifacts", func() {
w.Notice("Uploading artifacts...")
w.Command(info.RunnerCommand, args...)
})
}
func (b *AbstractShell) writeUploadArtifacts(w ShellWriter, info common.ShellScriptInfo, onSuccess bool) {
if info.Build.Runner.URL == "" {
return
......@@ -493,63 +542,18 @@ func (b *AbstractShell) writeUploadArtifacts(w ShellWriter, info common.ShellScr
b.writeExports(w, info)
b.writeCdBuildDir(w, info)
for _, artifacts := range info.Build.Artifacts {
for _, artifact := range info.Build.Artifacts {
if onSuccess {
if !artifacts.When.OnSuccess() {
if !artifact.When.OnSuccess() {
continue
}
} else {
if !artifacts.When.OnFailure() {
if !artifact.When.OnFailure() {
continue
}
}
args := []string{
"artifacts-uploader",
"--url",
info.Build.Runner.URL,
"--token",
info.Build.Token,
"--id",
strconv.Itoa(info.Build.ID),
}
// Create list of files to archive
archiverArgs := []string{}
for _, path := range artifacts.Paths {
archiverArgs = append(archiverArgs, "--path", path)
}
if artifacts.Untracked {
archiverArgs = append(archiverArgs, "--untracked")
}
if len(archiverArgs) < 1 {
// Skip creating archive
continue
}
args = append(args, archiverArgs...)
if artifacts.Name != "" {
args = append(args, "--name", artifacts.Name)
}
if artifacts.ExpireIn != "" {
args = append(args, "--expire-in", artifacts.ExpireIn)
}
if artifacts.Format != "" {
args = append(args, "--artifact-format", string(artifacts.Format))
}
if artifacts.Type != "" {
args = append(args, "--artifact-type", artifacts.Type)
}
b.guardRunnerCommand(w, info.RunnerCommand, "Uploading artifacts", func() {
w.Notice("Uploading artifacts...")
w.Command(info.RunnerCommand, args...)
})
b.writeUploadArtifact(w, info, artifact)
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment