Skip to content

Commit ee82d19

Browse files
author
jake klein
committed
Move api endpoints into concourse and setup history builder
- merged in from a wip branch
1 parent 35c4400 commit ee82d19

24 files changed

+1006
-260
lines changed

api/client.go

-6
This file was deleted.

api/clientfake/fake_client.go

-99
This file was deleted.

cmd/flake-detector/flake-detector.go

+63-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package main
22

33
import (
4+
"bytes"
45
"flag"
56
"fmt"
6-
"github.com/aclevername/concourse-flake-detector/api"
7-
"github.com/aclevername/concourse-flake-detector/httpclient"
7+
8+
"net/http"
9+
10+
"github.com/aclevername/concourse-flake-detector/concourse"
11+
"github.com/aclevername/concourse-flake-detector/flakedetector"
12+
"github.com/aclevername/concourse-flake-detector/historybuilder"
813
)
914

1015
func main() {
@@ -15,10 +20,62 @@ func main() {
1520
if *url == "" || *name == "" {
1621
panic("please configure correctly using -url and -pipeline")
1722
}
18-
var client api.Client
19-
client = new(httpclient.Client)
2023

21-
pipeline, _ := api.GetPipeline(*url, *name, client)
24+
//client := &realClient{
25+
// BaseURL: *url,
26+
//}
27+
//
28+
//pipeline, _ := concourse.GetPipeline(*url, *name, client)
29+
//
30+
//fmt.Println("------------------")
31+
//
32+
//fmt.Println(pipeline.Jobs()[0].URL)
33+
//fmt.Println(pipeline.Jobs()[0].Name)
34+
//jobHistory, err := historybuilder.GetJobHistory(client, pipeline.Jobs()[0])
35+
//if err != nil {
36+
// panic(err)
37+
//}
38+
//
39+
//jobFlakeCount, err := flakedetector.Detect(jobHistory)
40+
//if err != nil {
41+
// panic(err)
42+
//}
43+
//
44+
//fmt.Printf("\n----Result-----\nPipeline: %s\n", *name)
45+
//fmt.Printf("Job: %s, flakeyness: %d\n", pipeline.Jobs()[0].Name, jobFlakeCount)
46+
47+
client := concourse.NewClient(func(url string) ([]byte, error) {
48+
response, err := http.Get(url)
49+
if err != nil {
50+
return nil, err
51+
}
52+
buffer := new(bytes.Buffer)
53+
buffer.ReadFrom(response.Body)
54+
55+
return buffer.Bytes(), err
56+
}, *url, "")
57+
58+
pipeline, err := client.GetPipeline(*name)
59+
60+
if err != nil {
61+
panic(err)
62+
}
63+
64+
fmt.Println("------------------")
65+
66+
fmt.Println(pipeline.Jobs()[0].URL)
67+
fmt.Println(pipeline.Jobs()[0].Name)
68+
jobHistory, err := historybuilder.GetJobHistory(client, pipeline.Jobs()[0])
69+
if err != nil {
70+
panic(err)
71+
}
72+
73+
jobFlakeCount, err := flakedetector.Detect(jobHistory)
74+
if err != nil {
75+
panic(err)
76+
}
77+
2278
fmt.Printf("\n----Result-----\nPipeline: %s\n", *name)
23-
fmt.Printf("Job: %s, flakeyness: \n", pipeline.Jobs()[0].Name)
79+
fmt.Printf("Job: %s, flakeyness: %d\n", pipeline.Jobs()[0].Name, jobFlakeCount)
80+
2481
}

api/api_suite_test.go concourse/api_suite_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package api_test
1+
package concourse_test
22

33
import (
44
. "github.com/onsi/ginkgo"

concourse/builds.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package concourse
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
type Build struct {
9+
Status string `json:"status"`
10+
URL string `json:"api_url"`
11+
}
12+
13+
func (c *client) GetBuilds(job Job) ([]Build, error) {
14+
fmt.Println("+++++++++++++++++++++++++++++", job.URL)
15+
response, err := c.get(fmt.Sprintf("%s/builds", job.URL))
16+
if err != nil {
17+
return []Build{}, err
18+
}
19+
20+
builds := make([]Build, 0)
21+
err = json.Unmarshal(response, &builds)
22+
if err != nil {
23+
return []Build{}, err
24+
}
25+
26+
return builds, nil
27+
}

concourse/builds_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package concourse_test
2+
3+
import (
4+
"errors"
5+
6+
"github.com/aclevername/concourse-flake-detector/concourse"
7+
"github.com/aclevername/concourse-flake-detector/concourse/fake"
8+
. "github.com/onsi/ginkgo"
9+
. "github.com/onsi/gomega"
10+
)
11+
12+
var _ = Describe("GetBuilds", func() {
13+
14+
It("returns the builds for a job", func() {
15+
fakeGet := new(fake.FakeGetter)
16+
client := concourse.NewClient(fakeGet.Spy, "fake.com", "")
17+
18+
buildList := `[{"status":"succeeded","api_url":"/concourse/v1/builds/1"},{"status":"failed","api_url":"/concourse/v1/builds/2"}]`
19+
fakeGet.Returns([]byte(buildList), nil)
20+
builds, err := client.GetBuilds(concourse.Job{URL: "example.com"})
21+
Expect(err).NotTo(HaveOccurred())
22+
Expect(fakeGet.ArgsForCall(0)).To(Equal("example.com/builds"))
23+
Expect(builds).To(Equal([]concourse.Build{
24+
{
25+
URL: "/concourse/v1/builds/1",
26+
Status: "succeeded",
27+
},
28+
{
29+
URL: "/concourse/v1/builds/2",
30+
Status: "failed",
31+
},
32+
}))
33+
})
34+
35+
Context("when the get request fails", func() {
36+
It("returns an error", func() {
37+
fakeGet := new(fake.FakeGetter)
38+
client := concourse.NewClient(fakeGet.Spy, "fake.com", "")
39+
fakeGet.Returns([]byte(""), errors.New("failed"))
40+
_, err := client.GetBuilds(concourse.Job{URL: "example.com"})
41+
Expect(err).To(MatchError("failed"))
42+
})
43+
})
44+
45+
Context("when the get returns invalid json", func() {
46+
It("returns an error", func() {
47+
fakeGet := new(fake.FakeGetter)
48+
client := concourse.NewClient(fakeGet.Spy, "fake.com", "")
49+
fakeGet.Returns([]byte("not valid json"), nil)
50+
_, err := client.GetBuilds(concourse.Job{URL: "example.com"})
51+
Expect(err).To(HaveOccurred())
52+
})
53+
})
54+
})

concourse/client.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package concourse
2+
3+
import "fmt"
4+
5+
//go:generate counterfeiter -o fake/fake_client.go . ClientInterface
6+
type ClientInterface interface {
7+
GetPipeline(string) (Pipeline, error)
8+
GetBuilds(Job) ([]Build, error)
9+
GetResources(Build) (Run, error)
10+
}
11+
12+
//go:generate counterfeiter -o fake/fake_get.go . Getter
13+
type Getter func(string) ([]byte, error)
14+
15+
type client struct {
16+
get func(string) ([]byte, error)
17+
url string
18+
teamURL string
19+
}
20+
21+
func NewClient(get Getter, baseURL, team string) *client {
22+
url := fmt.Sprintf("%s/api/v1", baseURL)
23+
24+
teamURL := url
25+
if team != "" {
26+
teamURL = fmt.Sprintf("%s/teams/%s", teamURL, team)
27+
}
28+
29+
return &client{
30+
get: get,
31+
url: url,
32+
teamURL: teamURL,
33+
}
34+
}
35+
36+
// example team URL api/v1/teams/main/pipelines/main/jobs/fly/builds

0 commit comments

Comments
 (0)