Commit 3ed5a169 authored by Profpatsch's avatar Profpatsch

tools/hydra-to-docker-repo: further unmarshal API responses

parent 480ea7ad
...@@ -35,7 +35,7 @@ func New(baseUrl url.URL) *Hydra { ...@@ -35,7 +35,7 @@ func New(baseUrl url.URL) *Hydra {
type Endpoint string type Endpoint string
// Eval JSON output of hydra (partial). // Eval JSON output of hydra (partial).
type Eval struct { type EvalResponse struct {
Evals []struct { Evals []struct {
// unique id of that eval // unique id of that eval
Id int Id int
...@@ -45,32 +45,94 @@ type Eval struct { ...@@ -45,32 +45,94 @@ type Eval struct {
} }
} }
// Build JSON output of hydra (partial). type Eval struct {
type Build struct { HasNewBuilds bool
// nix derivation `outputs` field, keys "out", "man" … Builds []int
BuildOutputs map[string]struct { }
// nix storepath of output
Path string type Evals map[int]Eval
func intToBool(fieldName string, i int) (bool, error) {
switch i {
case 0:
return false, nil
case 1:
return true, nil
default:
return false, fmt.Errorf("intToBool: %d is not a valid boolean, for %s", i, fieldName)
} }
// hydra outputs of build }
BuildProducts map[string]struct {
// name of build output func responseToEvals(resp *EvalResponse) (*Evals, error) {
Name string evals := make(Evals, len(resp.Evals))
// nix storepath of output, can be used to associate to buildoutput for _, eval := range resp.Evals {
Path string hnb, err := intToBool(fmt.Sprintf("eval %d, HasNewBuilds", eval.Id), eval.HasNewBuilds)
// sha256hash of output file, null if no output file if err != nil {
Sha256Hash string return nil, err
}
evals[eval.Id] = Eval{
HasNewBuilds: hnb,
Builds: eval.Builds,
}
} }
return &evals, nil
}
type BuildOutput struct {
// nix storepath of output
Path string
}
type BuildProduct struct {
// name of build output
Name string
// nix storepath of output, can be used to associate to buildoutput
Path string
// sha256hash of output file, null if no output file
Sha256Hash string
}
// Build JSON output of hydra (partial).
type BuildResponse struct {
// unique id of that build
Id int
// buildstatus, 0 if successful // buildstatus, 0 if successful
BuildStatus int BuildStatus int
// finished, 1 if finished // finished, 1 if finished
Finished int Finished int
// unique id of that build // nix derivation `outputs` field, keys "out", "man" …
Id int BuildOutputs map[string]BuildOutput
// hydra outputs of build
BuildProducts map[string]BuildProduct
// hydra job attribute (nix attribute in hydra attrset, e.g. "pkgs.sangha") // hydra job attribute (nix attribute in hydra attrset, e.g. "pkgs.sangha")
Job string Job string
} }
type Build struct {
Id int
BuildSuccessful bool
BuildOutputs map[string]BuildOutput
BuildProducts map[string]BuildProduct
Job string
}
func responseToBuild(resp *BuildResponse) (*Build, error) {
finished, err := intToBool(fmt.Sprintf("build %d", resp.Id), resp.Finished)
if err != nil {
return nil, err
}
buildSuccessful := false
if finished && (resp.BuildStatus == 0) {
buildSuccessful = true
}
return &Build{
Id: resp.Id,
BuildSuccessful: buildSuccessful,
BuildOutputs: resp.BuildOutputs,
BuildProducts: resp.BuildProducts,
}, nil
}
// MkEndpoint creates an Endpoint, sanity checking it. // MkEndpoint creates an Endpoint, sanity checking it.
func MkEndpoint(rawEndpoint string) (Endpoint, error) { func MkEndpoint(rawEndpoint string) (Endpoint, error) {
url, err := url.Parse(rawEndpoint) url, err := url.Parse(rawEndpoint)
...@@ -132,16 +194,16 @@ func (h *Hydra) FetchBuild(buildId int) (*Build, error) { ...@@ -132,16 +194,16 @@ func (h *Hydra) FetchBuild(buildId int) (*Build, error) {
} }
defer response.Close() defer response.Close()
var result Build var result BuildResponse
err = json.NewDecoder(response).Decode(&result) err = json.NewDecoder(response).Decode(&result)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &result, nil return responseToBuild(&result)
} }
func (h *Hydra) FetchEval(project string, jobset string) (*Eval, error) { func (h *Hydra) FetchEval(project string, jobset string) (*Evals, error) {
endp, _ := MkEndpoint(fmt.Sprintf("jobset/%s/%s/evals", project, jobset)) endp, _ := MkEndpoint(fmt.Sprintf("jobset/%s/%s/evals", project, jobset))
response, err := h.fetchJson(endp) response, err := h.fetchJson(endp)
if err != nil { if err != nil {
...@@ -149,11 +211,11 @@ func (h *Hydra) FetchEval(project string, jobset string) (*Eval, error) { ...@@ -149,11 +211,11 @@ func (h *Hydra) FetchEval(project string, jobset string) (*Eval, error) {
} }
defer response.Close() defer response.Close()
var result Eval var result EvalResponse
err = json.NewDecoder(response).Decode(&result) err = json.NewDecoder(response).Decode(&result)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &result, nil return responseToEvals(&result)
} }
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