Skip to content

Commit

Permalink
fix: Solve the problem of failure to download multi-branch-pipeline a…
Browse files Browse the repository at this point in the history
…rtifacts (#64)

* Solve the problem of failure to download multi-branch-pipeline artifacts

* polish the code
  • Loading branch information
littlejiancc authored Jul 6, 2023
1 parent f9a8b3f commit 0c8af44
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 2 deletions.
15 changes: 14 additions & 1 deletion pkg/artifact/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ func (q *Client) List(jobName string, buildID int) (artifacts []Artifact, err er

// GetArtifact download artifact using stream
func (q *Client) GetArtifact(projectName, pipelineName string, buildID int, filename string) (io.ReadCloser, error) {
artifactURL := fmt.Sprintf("/job/%s/job/%s/%d/artifact/%s", projectName, pipelineName, buildID, filename)
return q.GetArtifactFromMultiBranchPipeline(pipelineName, pipelineName, false, "", buildID, filename)
}

// GetArtifactFromMultiBranchPipeline download multi pipeline artifact using stream
func (q *Client) GetArtifactFromMultiBranchPipeline(projectName, pipelineName string, isMultiBranch bool, branchName string, buildID int, filename string) (io.ReadCloser, error) {
artifactURL := generateArtifactURL(projectName, pipelineName, isMultiBranch, branchName, buildID, filename)
resp, err := q.RequestWithResponse(http.MethodGet, artifactURL, nil, nil)
if err != nil {
return nil, err
Expand All @@ -50,3 +55,11 @@ func (q *Client) GetArtifact(projectName, pipelineName string, buildID int, file

return resp.Body, nil
}

// generateArtifactURL generate artifactURL by pipelineType
func generateArtifactURL(projectName, pipelineName string, isMultiBranch bool, branchName string, buildID int, filename string) string {
if isMultiBranch {
return fmt.Sprintf("/job/%s/job/%s/job/%s/%d/artifact/%s", projectName, pipelineName, branchName, buildID, filename)
}
return fmt.Sprintf("/job/%s/job/%s/%d/artifact/%s", projectName, pipelineName, buildID, filename)
}
109 changes: 108 additions & 1 deletion pkg/artifact/artifacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"io/ioutil"

"github.com/golang/mock/gomock"
"github.com/jenkins-zh/jenkins-client/pkg/mock/mhttp"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/jenkins-zh/jenkins-client/pkg/mock/mhttp"
)

var _ = Describe("artifacts test", func() {
Expand Down Expand Up @@ -94,4 +96,109 @@ var _ = Describe("artifacts test", func() {
Expect(err).Should(HaveOccurred())
})
})

Context("generateArtifactURL", func() {
var (
projectName string
pipelineName string
isMultiBranch bool
branchName string
buildID int
filename string
)
It("should success with pipeline", func() {
projectName = "project"
pipelineName = "pipeline"
isMultiBranch = false
branchName = "main"
buildID = 1
filename = "a.jar"
want := "/job/project/job/pipeline/1/artifact/a.jar"
url := generateArtifactURL(projectName, pipelineName, isMultiBranch, branchName, buildID, filename)
Expect(url).To(Equal(want))
})

It("should success with multi-branch-pipeline", func() {
projectName = "project"
pipelineName = "pipeline"
isMultiBranch = true
branchName = "main"
buildID = 1
filename = "a.jar"
want := "/job/project/job/pipeline/job/main/1/artifact/a.jar"
url := generateArtifactURL(projectName, pipelineName, isMultiBranch, branchName, buildID, filename)
Expect(url).To(Equal(want))
})
})

Context("GetArtifactFromMultiBranchPipeline", func() {
It("should success with NoScmPipelineType", func() {
artifactClient.UserName = username
artifactClient.Token = password

projectName := "fakename"
pipelineName := "fakename"
filename := "fakename"
branchName := "main"
PrepareGetArtifact(roundTripper, artifactClient.URL, username, password, projectName, pipelineName, 1, filename)

body, err := artifactClient.GetArtifactFromMultiBranchPipeline(projectName, pipelineName, false, branchName, 1, filename)
Expect(err).To(BeNil())
Expect(func() bool {
b, err := ioutil.ReadAll(body)
if err != nil {
return false
}
return len(b) > 0
}()).To(Equal(true))
})

It("should success with MultiBranchPipelineType", func() {
artifactClient.UserName = username
artifactClient.Token = password

projectName := "fakename"
pipelineName := "fakename"
filename := "fakename"
branchName := "main"
PrepareGetMultiBranchPipelineArtifact(roundTripper, artifactClient.URL, username, password, projectName, pipelineName, 1, filename, branchName)

body, err := artifactClient.GetArtifactFromMultiBranchPipeline(projectName, pipelineName, true, branchName, 1, filename)
Expect(err).To(BeNil())
Expect(func() bool {
b, err := ioutil.ReadAll(body)
if err != nil {
return false
}
return len(b) > 0
}()).To(Equal(true))
})

It("should fail with NoScmPipelineType", func() {
artifactClient.UserName = username
artifactClient.Token = password

projectName := "fakename"
pipelineName := "fakename"
filename := "fakename"
PrepareGetNoExistsArtifact(roundTripper, artifactClient.URL, username, password, projectName, pipelineName, 1, filename)

_, err := artifactClient.GetArtifactFromMultiBranchPipeline(projectName, pipelineName, false, "", 1, filename)
Expect(err).Should(HaveOccurred())
})

It("should fail with MultiBranchPipelineType", func() {
artifactClient.UserName = username
artifactClient.Token = password

projectName := "fakename"
pipelineName := "fakename"
filename := "fakename"
branchName := "main"
PrepareGetNoExistsMultiBranchPipelineArtifact(roundTripper, artifactClient.URL, username, password, projectName, pipelineName, 1, filename, branchName)

_, err := artifactClient.GetArtifactFromMultiBranchPipeline(projectName, pipelineName, true, branchName, 1, filename)
Expect(err).Should(HaveOccurred())
})
})
})
36 changes: 36 additions & 0 deletions pkg/artifact/artifacts_test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ func PrepareGetArtifact(roundTripper *mhttp.MockRoundTripper, rootURL, user, pas
return
}

// PrepareGetMultiBranchPipelineArtifact only for test
func PrepareGetMultiBranchPipelineArtifact(roundTripper *mhttp.MockRoundTripper, rootURL, user, passwd, projectName, pipelineName string, buildID int, filename, branchName string) (response *http.Response) {
var api = fmt.Sprintf("/job/%s/job/%s/job/%s/%d/artifact/%s", projectName, pipelineName, branchName, buildID, filename)
request, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s%s", rootURL, api), nil)
response = &http.Response{
StatusCode: 200,
Request: request,
Body: ioutil.NopCloser(bytes.NewBufferString(`this is test file`)),
}
roundTripper.EXPECT().
RoundTrip(core.NewRequestMatcher(request)).Return(response, nil)

if user != "" && passwd != "" {
request.SetBasicAuth(user, passwd)
}
return
}

// PrepareGetNoExistsArtifact only for test
func PrepareGetNoExistsArtifact(roundTripper *mhttp.MockRoundTripper, rootURL, user, passwd, projectName, pipelineName string, buildID int, filename string) (response *http.Response) {
var api = fmt.Sprintf("/job/%s/job/%s/%d/artifact/%s", projectName, pipelineName, buildID, filename)
Expand All @@ -80,3 +98,21 @@ func PrepareGetNoExistsArtifact(roundTripper *mhttp.MockRoundTripper, rootURL, u
}
return
}

// PrepareGetNoExistsMultiBranchPipelineArtifact only for test
func PrepareGetNoExistsMultiBranchPipelineArtifact(roundTripper *mhttp.MockRoundTripper, rootURL, user, passwd, projectName, pipelineName string, buildID int, filename, branchName string) (response *http.Response) {
var api = fmt.Sprintf("/job/%s/job/%s/job/%s/%d/artifact/%s", projectName, pipelineName, branchName, buildID, filename)
request, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s%s", rootURL, api), nil)
response = &http.Response{
StatusCode: 404,
Request: request,
Body: nil,
}
roundTripper.EXPECT().
RoundTrip(core.NewRequestMatcher(request)).Return(response, nil)

if user != "" && passwd != "" {
request.SetBasicAuth(user, passwd)
}
return
}

0 comments on commit 0c8af44

Please sign in to comment.