Commit 464cf148 authored by Profpatsch's avatar Profpatsch

tools/hydra-to-docker-repo: fetch latest finished build for job

Digging deeper into the Hydra API, a way to fetch the latest succeeding job
already exists, which provides all the information we need.
parent 3ed5a169
...@@ -24,6 +24,7 @@ type Hydra struct { ...@@ -24,6 +24,7 @@ type Hydra struct {
client *http.Client client *http.Client
} }
// New initializes a Hydra connection.
func New(baseUrl url.URL) *Hydra { func New(baseUrl url.URL) *Hydra {
return &Hydra{ return &Hydra{
baseUrl: baseUrl, baseUrl: baseUrl,
...@@ -31,6 +32,18 @@ func New(baseUrl url.URL) *Hydra { ...@@ -31,6 +32,18 @@ func New(baseUrl url.URL) *Hydra {
} }
} }
// JobsetRef references a hydra Jobset.
type JobsetRef struct {
Project string
Name string
}
// JobRef references a hydra Job.
type JobRef struct {
Jobset JobsetRef
Name string
}
// Endpoint of hydra that can be queried. // Endpoint of hydra that can be queried.
type Endpoint string type Endpoint string
...@@ -45,6 +58,7 @@ type EvalResponse struct { ...@@ -45,6 +58,7 @@ type EvalResponse struct {
} }
} }
// Eval the result of a Hydra evaluation.
type Eval struct { type Eval struct {
HasNewBuilds bool HasNewBuilds bool
Builds []int Builds []int
...@@ -78,11 +92,14 @@ func responseToEvals(resp *EvalResponse) (*Evals, error) { ...@@ -78,11 +92,14 @@ func responseToEvals(resp *EvalResponse) (*Evals, error) {
return &evals, nil return &evals, nil
} }
// BuildOutput is a nix derivation output, like "doc".
type BuildOutput struct { type BuildOutput struct {
// nix storepath of output // nix storepath of output
Path string Path string
} }
// BuildProduct is a Hydra build product
// (in the magic `nix-support/hydra-build-products` file of an output).
type BuildProduct struct { type BuildProduct struct {
// name of build output // name of build output
Name string Name string
...@@ -108,6 +125,7 @@ type BuildResponse struct { ...@@ -108,6 +125,7 @@ type BuildResponse struct {
Job string Job string
} }
// Build is a Hydra Build.
type Build struct { type Build struct {
Id int Id int
BuildSuccessful bool BuildSuccessful bool
...@@ -186,8 +204,7 @@ func (h *Hydra) fetchJson(e Endpoint) (io.ReadCloser, error) { ...@@ -186,8 +204,7 @@ func (h *Hydra) fetchJson(e Endpoint) (io.ReadCloser, error) {
} }
func (h *Hydra) FetchBuild(buildId int) (*Build, error) { func (h *Hydra) FetchBuildFromEndpoint(endp Endpoint) (*Build, error) {
endp, _ := MkEndpoint("build/" + strconv.Itoa(buildId))
response, err := h.fetchJson(endp) response, err := h.fetchJson(endp)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -203,8 +220,23 @@ func (h *Hydra) FetchBuild(buildId int) (*Build, error) { ...@@ -203,8 +220,23 @@ func (h *Hydra) FetchBuild(buildId int) (*Build, error) {
return responseToBuild(&result) return responseToBuild(&result)
} }
func (h *Hydra) FetchEval(project string, jobset string) (*Evals, error) { // FetchBuild by Hydra build id.
endp, _ := MkEndpoint(fmt.Sprintf("jobset/%s/%s/evals", project, jobset)) func (h *Hydra) FetchBuild(buildId int) (*Build, error) {
endp, _ := MkEndpoint("build/" + strconv.Itoa(buildId))
return h.FetchBuildFromEndpoint(endp)
}
// FetchLatestFinishedBuild for a Hydra job.
func (h *Hydra) FetchLatestFinishedBuild(job JobRef) (*Build, error) {
endp, _ := MkEndpoint(fmt.Sprintf("job/%s/%s/%s/latest-finished",
job.Jobset.Project, job.Jobset.Name, job.Name))
return h.FetchBuildFromEndpoint(endp)
}
// FetchEvals for a Hydra jobset.
func (h *Hydra) FetchEvals(jobset JobsetRef) (*Evals, error) {
endp, _ := MkEndpoint(
fmt.Sprintf("jobset/%s/%s/evals", jobset.Project, jobset.Name))
response, err := h.fetchJson(endp) response, err := h.fetchJson(endp)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -17,9 +17,23 @@ func main() { ...@@ -17,9 +17,23 @@ func main() {
} }
fmt.Printf("%+v\n", build) fmt.Printf("%+v\n", build)
eval, err := h.FetchEval("techcultivation", "sangha-deployment") eval, err := h.FetchEvals(hydra.JobsetRef{
Project: "techcultivation",
Name: "sangha-deployment"})
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
fmt.Printf("%+v\n", eval) fmt.Printf("%+v\n", eval)
build, err = h.FetchLatestFinishedBuild(hydra.JobRef{
Name: "downloads.dockerImages.sangha.postgres",
Jobset: hydra.JobsetRef{
Project: "techcultivation",
Name: "sangha-deployment",
},
})
if err != nil {
fmt.Println(err)
}
fmt.Printf("%+v\n", build)
} }
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