Skip to content

Commit 35c4400

Browse files
author
Jake Klein
committed
Add checks for failed requests in history_builder
1 parent e4ec020 commit 35c4400

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

historybuilder/history_builder.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ type JSONBuild struct {
3333
}
3434

3535
func GetJobHistory(client api.Client, job api.Job) ([]Run, error) {
36-
var err error
37-
response, _ := client.Get(fmt.Sprintf("api/v1%s/builds", job.URL))
36+
response, err := client.Get(fmt.Sprintf("api/v1%s/builds", job.URL))
37+
if err != nil {
38+
return []Run{}, err
39+
}
3840

3941
builds := make([]JSONBuild, 0)
4042
err = json.Unmarshal(response, &builds)
@@ -43,7 +45,10 @@ func GetJobHistory(client api.Client, job api.Job) ([]Run, error) {
4345
}
4446
var history []Run
4547
for _, build := range builds {
46-
response, _ := client.Get(build.APIURL + "/resources")
48+
response, err := client.Get(build.APIURL + "/resources")
49+
if err != nil {
50+
return []Run{}, err
51+
}
4752

4853
inputs := Resource{}
4954
err = json.Unmarshal(response, &inputs)

historybuilder/history_builder_test.go

+44-15
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,40 @@ import (
55
"github.com/aclevername/concourse-flake-detector/api/clientfake"
66
"github.com/aclevername/concourse-flake-detector/historybuilder"
77

8+
"errors"
9+
"fmt"
810
. "github.com/onsi/ginkgo"
911
. "github.com/onsi/gomega"
1012
)
1113

12-
var _ = Describe("historybuilder", func() {
13-
Describe("GetJobHistory", func() {
14-
It("gets a history of the job", func() {
15-
client := new(clientfake.FakeClient)
16-
testJob := api.Job{Name: "test-job", URL: "/teams/main/pipelines/main/jobs/fly"}
17-
18-
buildList := `[{"status":"succeeded","api_url":"/api/v1/builds/1"},{"status":"failed","api_url":"/api/v1/builds/2"}]`
19-
client.GetReturnsOnCall(0, []byte(buildList), nil)
20-
21-
resources := `
14+
const gitResourcewithVersion = `
2215
{
2316
"inputs": [
2417
{
2518
"name": "concourse",
2619
"resource": "concourse",
2720
"type": "git",
2821
"version": {
29-
"ref": "d70a2b36579c7ea397d895ce7371f8cc9e044fc7"
22+
"ref": "%s"
3023
},
3124
"pipeline_id": 1
3225
}
3326
]
3427
}
3528
`
36-
client.GetReturnsOnCall(1, []byte(resources), nil)
3729

38-
client.GetReturnsOnCall(2, []byte(resources), nil)
30+
var _ = Describe("historybuilder", func() {
31+
Describe("GetJobHistory", func() {
32+
It("gets a history of the job", func() {
33+
client := new(clientfake.FakeClient)
34+
testJob := api.Job{Name: "test-job", URL: "/teams/main/pipelines/main/jobs/fly"}
35+
36+
buildList := `[{"status":"succeeded","api_url":"/api/v1/builds/1"},{"status":"failed","api_url":"/api/v1/builds/2"}]`
37+
client.GetReturnsOnCall(0, []byte(buildList), nil)
38+
39+
client.GetReturnsOnCall(1, []byte(fmt.Sprintf(gitResourcewithVersion, "version1")), nil)
40+
41+
client.GetReturnsOnCall(2, []byte(fmt.Sprintf(gitResourcewithVersion, "version1")), nil)
3942

4043
history, err := historybuilder.GetJobHistory(client, testJob)
4144

@@ -51,7 +54,7 @@ var _ = Describe("historybuilder", func() {
5154
Resource: "concourse",
5255
Type: "git",
5356
Version: historybuilder.Ref{
54-
Ref: "d70a2b36579c7ea397d895ce7371f8cc9e044fc7",
57+
Ref: "version1",
5558
},
5659
PipelineID: 1,
5760
}))
@@ -62,10 +65,36 @@ var _ = Describe("historybuilder", func() {
6265
Resource: "concourse",
6366
Type: "git",
6467
Version: historybuilder.Ref{
65-
Ref: "d70a2b36579c7ea397d895ce7371f8cc9e044fc7",
68+
Ref: "version1",
6669
},
6770
PipelineID: 1,
6871
}))
6972
})
73+
74+
Context("when getting the job builds fails", func() {
75+
It("returns an error", func() {
76+
client := new(clientfake.FakeClient)
77+
testJob := api.Job{Name: "test-job", URL: "/teams/main/pipelines/main/jobs/fly"}
78+
79+
buildList := `[{"status":"succeeded","api_url":"/api/v1/builds/1"},{"status":"failed","api_url":"/api/v1/builds/2"}]`
80+
client.GetReturnsOnCall(0, []byte(buildList), nil)
81+
client.GetReturnsOnCall(1, []byte{}, errors.New("failed"))
82+
83+
_, err := historybuilder.GetJobHistory(client, testJob)
84+
Expect(err).To(MatchError("failed"))
85+
})
86+
})
87+
88+
Context("when getting the a builds resources fails", func() {
89+
It("returns an error", func() {
90+
client := new(clientfake.FakeClient)
91+
testJob := api.Job{Name: "test-job", URL: "/teams/main/pipelines/main/jobs/fly"}
92+
93+
client.GetReturnsOnCall(0, []byte{}, errors.New("failed"))
94+
95+
_, err := historybuilder.GetJobHistory(client, testJob)
96+
Expect(err).To(MatchError("failed"))
97+
})
98+
})
7099
})
71100
})

0 commit comments

Comments
 (0)