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 {
type Endpoint string
// Eval JSON output of hydra (partial).
type Eval struct {
type EvalResponse struct {
Evals []struct {
// unique id of that eval
Id int
......@@ -45,32 +45,94 @@ type Eval struct {
}
}
// Build JSON output of hydra (partial).
type Build struct {
// nix derivation `outputs` field, keys "out", "man" …
BuildOutputs map[string]struct {
// nix storepath of output
Path string
type Eval struct {
HasNewBuilds bool
Builds []int
}
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
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
}
func responseToEvals(resp *EvalResponse) (*Evals, error) {
evals := make(Evals, len(resp.Evals))
for _, eval := range resp.Evals {
hnb, err := intToBool(fmt.Sprintf("eval %d, HasNewBuilds", eval.Id), eval.HasNewBuilds)
if err != nil {
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 int
// finished, 1 if finished
Finished int
// unique id of that build
Id int
// nix derivation `outputs` field, keys "out", "man" …
BuildOutputs map[string]BuildOutput
// hydra outputs of build
BuildProducts map[string]BuildProduct
// hydra job attribute (nix attribute in hydra attrset, e.g. "pkgs.sangha")
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.
func MkEndpoint(rawEndpoint string) (Endpoint, error) {
url, err := url.Parse(rawEndpoint)
......@@ -132,16 +194,16 @@ func (h *Hydra) FetchBuild(buildId int) (*Build, error) {
}
defer response.Close()
var result Build
var result BuildResponse
err = json.NewDecoder(response).Decode(&result)
if err != nil {
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))
response, err := h.fetchJson(endp)
if err != nil {
......@@ -149,11 +211,11 @@ func (h *Hydra) FetchEval(project string, jobset string) (*Eval, error) {
}
defer response.Close()
var result Eval
var result EvalResponse
err = json.NewDecoder(response).Decode(&result)
if err != nil {
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