Skip to content

Commit 42f9fef

Browse files
authored
Merge pull request #262 from buildkite/bm/artifact_get
2 parents 6f702c7 + 24c6adc commit 42f9fef

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed

artifacts.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ func (as *ArtifactsService) ListByJob(ctx context.Context, org string, pipeline
8383
return artifacts, resp, err
8484
}
8585

86+
func (as *ArtifactsService) Get(ctx context.Context, org, pipeline, build, job, id string) (Artifact, *Response, error) {
87+
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/builds/%s/jobs/%s/artifacts/%s", org, pipeline, build, job, id)
88+
req, err := as.client.NewRequest(ctx, "GET", u, nil)
89+
if err != nil {
90+
return Artifact{}, nil, err
91+
}
92+
93+
var artifact Artifact
94+
resp, err := as.client.Do(req, &artifact)
95+
if err != nil {
96+
return Artifact{}, resp, err
97+
}
98+
99+
return artifact, resp, err
100+
}
101+
86102
// DownloadArtifactByURL gets artifacts for a specific build
87103
//
88104
// buildkite API docs: https://buildkite.com/docs/api/artifacts#list-artifacts-for-a-build

artifacts_test.go

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package buildkite
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"fmt"
7+
"net/http"
8+
"testing"
9+
10+
"github.com/google/go-cmp/cmp"
11+
)
12+
13+
func TestArtifactsService_ListByBuild(t *testing.T) {
14+
t.Parallel()
15+
16+
server, client, teardown := newMockServerAndClient(t)
17+
t.Cleanup(teardown)
18+
19+
server.HandleFunc("/v2/organizations/my-great-org/pipelines/sup-keith/builds/123/artifacts", func(w http.ResponseWriter, r *http.Request) {
20+
testMethod(t, r, "GET")
21+
_, _ = fmt.Fprint(w, `[{"id":"art-1","job_id":"job-1","path":"artifact.txt"},{"id":"art-2","job_id":"job-2","path":"artifact2.txt"}]`)
22+
})
23+
24+
artifacts, _, err := client.Artifacts.ListByBuild(context.Background(), "my-great-org", "sup-keith", "123", nil)
25+
if err != nil {
26+
t.Errorf("ListByBuild returned error: %v", err)
27+
}
28+
29+
want := []Artifact{
30+
{ID: "art-1", JobID: "job-1", Path: "artifact.txt"},
31+
{ID: "art-2", JobID: "job-2", Path: "artifact2.txt"},
32+
}
33+
if diff := cmp.Diff(artifacts, want); diff != "" {
34+
t.Errorf("ListByBuild diff: (-got +want)\n%s", diff)
35+
}
36+
}
37+
38+
func TestArtifactsService_ListByBuild_WithPagination(t *testing.T) {
39+
t.Parallel()
40+
41+
server, client, teardown := newMockServerAndClient(t)
42+
t.Cleanup(teardown)
43+
44+
server.HandleFunc("/v2/organizations/my-great-org/pipelines/sup-keith/builds/123/artifacts", func(w http.ResponseWriter, r *http.Request) {
45+
testMethod(t, r, "GET")
46+
testFormValues(t, r, values{
47+
"page": "2",
48+
"per_page": "10",
49+
})
50+
_, _ = fmt.Fprint(w, `[{"id":"art-1"}]`)
51+
})
52+
53+
opt := &ArtifactListOptions{
54+
ListOptions: ListOptions{Page: 2, PerPage: 10},
55+
}
56+
artifacts, _, err := client.Artifacts.ListByBuild(context.Background(), "my-great-org", "sup-keith", "123", opt)
57+
if err != nil {
58+
t.Errorf("ListByBuild returned error: %v", err)
59+
}
60+
61+
want := []Artifact{{ID: "art-1"}}
62+
if diff := cmp.Diff(artifacts, want); diff != "" {
63+
t.Errorf("ListByBuild diff: (-got +want)\n%s", diff)
64+
}
65+
}
66+
67+
func TestArtifactsService_ListByJob(t *testing.T) {
68+
t.Parallel()
69+
70+
server, client, teardown := newMockServerAndClient(t)
71+
t.Cleanup(teardown)
72+
73+
server.HandleFunc("/v2/organizations/my-great-org/pipelines/sup-keith/builds/123/jobs/job-456/artifacts", func(w http.ResponseWriter, r *http.Request) {
74+
testMethod(t, r, "GET")
75+
_, _ = fmt.Fprint(w, `[{"id":"art-1","job_id":"job-456","path":"output.log"}]`)
76+
})
77+
78+
artifacts, _, err := client.Artifacts.ListByJob(context.Background(), "my-great-org", "sup-keith", "123", "job-456", nil)
79+
if err != nil {
80+
t.Errorf("ListByJob returned error: %v", err)
81+
}
82+
83+
want := []Artifact{{ID: "art-1", JobID: "job-456", Path: "output.log"}}
84+
if diff := cmp.Diff(artifacts, want); diff != "" {
85+
t.Errorf("ListByJob diff: (-got +want)\n%s", diff)
86+
}
87+
}
88+
89+
func TestArtifactsService_ListByJob_WithPagination(t *testing.T) {
90+
t.Parallel()
91+
92+
server, client, teardown := newMockServerAndClient(t)
93+
t.Cleanup(teardown)
94+
95+
server.HandleFunc("/v2/organizations/my-great-org/pipelines/sup-keith/builds/123/jobs/job-456/artifacts", func(w http.ResponseWriter, r *http.Request) {
96+
testMethod(t, r, "GET")
97+
testFormValues(t, r, values{
98+
"page": "3",
99+
})
100+
_, _ = fmt.Fprint(w, `[{"id":"art-1"}]`)
101+
})
102+
103+
opt := &ArtifactListOptions{
104+
ListOptions: ListOptions{Page: 3},
105+
}
106+
artifacts, _, err := client.Artifacts.ListByJob(context.Background(), "my-great-org", "sup-keith", "123", "job-456", opt)
107+
if err != nil {
108+
t.Errorf("ListByJob returned error: %v", err)
109+
}
110+
111+
want := []Artifact{{ID: "art-1"}}
112+
if diff := cmp.Diff(artifacts, want); diff != "" {
113+
t.Errorf("ListByJob diff: (-got +want)\n%s", diff)
114+
}
115+
}
116+
117+
func TestArtifactsService_Get(t *testing.T) {
118+
t.Parallel()
119+
120+
server, client, teardown := newMockServerAndClient(t)
121+
t.Cleanup(teardown)
122+
123+
server.HandleFunc("/v2/organizations/my-great-org/pipelines/sup-keith/builds/123/jobs/job-456/artifacts/art-789", func(w http.ResponseWriter, r *http.Request) {
124+
testMethod(t, r, "GET")
125+
_, _ = fmt.Fprint(w, `{
126+
"id": "art-789",
127+
"job_id": "job-456",
128+
"url": "https://api.buildkite.com/v2/organizations/my-great-org/pipelines/sup-keith/builds/123/jobs/job-456/artifacts/art-789",
129+
"download_url": "https://api.buildkite.com/v2/organizations/my-great-org/pipelines/sup-keith/builds/123/jobs/job-456/artifacts/art-789/download",
130+
"state": "finished",
131+
"path": "logs/output.log",
132+
"dirname": "logs",
133+
"filename": "output.log",
134+
"mime_type": "text/plain",
135+
"file_size": 1024,
136+
"sha1sum": "abc123def456"
137+
}`)
138+
})
139+
140+
artifact, _, err := client.Artifacts.Get(context.Background(), "my-great-org", "sup-keith", "123", "job-456", "art-789")
141+
if err != nil {
142+
t.Errorf("Get returned error: %v", err)
143+
}
144+
145+
want := Artifact{
146+
ID: "art-789",
147+
JobID: "job-456",
148+
URL: "https://api.buildkite.com/v2/organizations/my-great-org/pipelines/sup-keith/builds/123/jobs/job-456/artifacts/art-789",
149+
DownloadURL: "https://api.buildkite.com/v2/organizations/my-great-org/pipelines/sup-keith/builds/123/jobs/job-456/artifacts/art-789/download",
150+
State: "finished",
151+
Path: "logs/output.log",
152+
Dirname: "logs",
153+
Filename: "output.log",
154+
MimeType: "text/plain",
155+
FileSize: 1024,
156+
SHA1: "abc123def456",
157+
}
158+
if diff := cmp.Diff(artifact, want); diff != "" {
159+
t.Errorf("Get diff: (-got +want)\n%s", diff)
160+
}
161+
}
162+
163+
func TestArtifactsService_DownloadArtifactByURL(t *testing.T) {
164+
t.Parallel()
165+
166+
server, client, teardown := newMockServerAndClient(t)
167+
t.Cleanup(teardown)
168+
169+
expectedContent := "This is the artifact content"
170+
server.HandleFunc("/v2/organizations/my-great-org/pipelines/sup-keith/builds/123/jobs/job-456/artifacts/art-789/download", func(w http.ResponseWriter, r *http.Request) {
171+
testMethod(t, r, "GET")
172+
w.Header().Set("Content-Type", "text/plain")
173+
_, _ = fmt.Fprint(w, expectedContent)
174+
})
175+
176+
var buf bytes.Buffer
177+
_, err := client.Artifacts.DownloadArtifactByURL(context.Background(), "v2/organizations/my-great-org/pipelines/sup-keith/builds/123/jobs/job-456/artifacts/art-789/download", &buf)
178+
if err != nil {
179+
t.Errorf("DownloadArtifactByURL returned error: %v", err)
180+
}
181+
182+
if got := buf.String(); got != expectedContent {
183+
t.Errorf("DownloadArtifactByURL content = %q, want %q", got, expectedContent)
184+
}
185+
}

0 commit comments

Comments
 (0)