Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
fix some missing fields in Job
Browse files Browse the repository at this point in the history
Jobs could be listed from many API endpoints:
* from `projects` (see
  https://docs.gitlab.com/ee/api/jobs.html#list-project-jobs)
* from `pipelines` (see
  https://docs.gitlab.com/ee/api/jobs.html#list-pipeline-jobs)
* from `runners`(see
  https://docs.gitlab.com/ee/api/runners.html#list-runners-jobs)

While listing jobs from `runners` endpoint, some informations related to
the Project for which the job has been run are added to the response.
This fix adds optional `Project` field to the `Job` struct to manage
this use case.
  • Loading branch information
apendragon committed Nov 5, 2021
1 parent 90bef1b commit fd3f0d2
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 7 deletions.
80 changes: 80 additions & 0 deletions fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,86 @@ const (
"token": "6337ff461c94fd3fa32ba3b1ff4125"
}`

// exampleRunnerJob provides fixture for ListRunnerJobs test
exampleListRunnerJobs = `
[
{
"id": 1,
"status": "failed",
"stage": "test",
"name": "run_tests",
"ref": "master",
"tag": false,
"coverage": null,
"allow_failure": false,
"created_at": "2021-10-22T11:59:25.201Z",
"started_at": "2021-10-22T11:59:33.660Z",
"finished_at": "2021-10-22T15:59:25.201Z",
"duration": 171.540594,
"queued_duration": 2.535766,
"user": {
"id": 368,
"name": "John SMITH",
"username": "john.smith",
"state": "blocked",
"avatar_url": "https://gitlab.example.com/uploads/-/system/user/avatar/368/avatar.png",
"web_url": "https://gitlab.example.com/john.smith",
"bio": "",
"location": "",
"public_email": "[email protected]",
"skype": "",
"linkedin": "",
"twitter": "",
"website_url": "",
"organization": "",
"job_title": "",
"pronouns": null,
"bot": false,
"work_information": null,
"bio_html": ""
},
"commit": {
"id": "6c016b801a88f4bd31f927fc045b5c746a6f823e",
"short_id": "6c016b80",
"created_at": "2018-03-21T14:41:00.000+01:00",
"parent_ids": [
"6008b4902d40799ab11688e502d9f1f27f6d2e18"
],
"title": "Update env for specific runner",
"message": "Update env for specific runner\n",
"author_name": "John SMITH",
"author_email": "[email protected]",
"authored_date": "2018-03-21T14:41:00.000+01:00",
"committer_name": "John SMITH",
"committer_email": "[email protected]",
"committed_date": "2018-03-21T14:41:00.000+01:00",
"trailers": {},
"web_url": "https://gitlab.example.com/awesome/packages/common/-/commit/6c016b801a88f4bd31f927fc045b5c746a6f823e"
},
"pipeline": {
"id": 8777,
"project_id": 3252,
"sha": "6c016b801a88f4bd31f927fc045b5c746a6f823e",
"ref": "master",
"status": "failed",
"source": "push",
"created_at": "2018-03-21T13:41:15.356Z",
"updated_at": "2018-03-21T15:12:52.021Z",
"web_url": "https://gitlab.example.com/awesome/packages/common/-/pipelines/8777"
},
"web_url": "https://gitlab.example.com/awesome/packages/common/-/jobs/14606",
"project": {
"id": 3252,
"description": "Common nodejs paquet for producer",
"name": "common",
"name_with_namespace": "awesome",
"path": "common",
"path_with_namespace": "awesome",
"created_at": "2018-02-13T09:21:48.107Z"
}
}
]`

// exampleReleaseLink provides fixture for Release Links tests.
exampleReleaseLink = `{
"id":1,
Expand Down
11 changes: 6 additions & 5 deletions jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ type Job struct {
IsShared bool `json:"is_shared"`
Name string `json:"name"`
} `json:"runner"`
Stage string `json:"stage"`
Status string `json:"status"`
Tag bool `json:"tag"`
WebURL string `json:"web_url"`
User *User `json:"user"`
Stage string `json:"stage"`
Status string `json:"status"`
Tag bool `json:"tag"`
WebURL string `json:"web_url"`
User *User `json:"user"`
Project *Project `json:"project"`
}

// Bridge represents a pipeline bridge.
Expand Down
78 changes: 76 additions & 2 deletions runners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,87 @@ func TestDisableRunner(t *testing.T) {
}
}

func stringToTime(date string, t *testing.T) *time.Time {
d, err := time.Parse(timeLayout, date)
if err != nil {
t.Errorf("Unable to parse date [%s]: %v", d, err)
}
return &d
}

func expectedRunnerJobs(t *testing.T) []*Job {
pipeline := struct {
ID int `json:"id"`
Ref string `json:"ref"`
Sha string `json:"sha"`
Status string `json:"status"`
}{
ID: 8777,
Ref: "master",
Sha: "6c016b801a88f4bd31f927fc045b5c746a6f823e",
Status: "failed",
}

return []*Job{
&Job{
ID: 1,
Status: "failed",
Stage: "test",
Name: "run_tests",
Ref: "master",
Tag: false,
Coverage: 0,
AllowFailure: false,
CreatedAt: stringToTime("2021-10-22T11:59:25.201Z", t),
StartedAt: stringToTime("2021-10-22T11:59:33.660Z", t),
FinishedAt: stringToTime("2021-10-22T15:59:25.201Z", t),
Duration: 171.540594,
QueuedDuration: 2.535766,
User: &User{
ID: 368,
Name: "John SMITH",
Username: "john.smith",
AvatarURL: "https://gitlab.example.com/uploads/-/system/user/avatar/368/avatar.png",
State: "blocked",
WebURL: "https://gitlab.example.com/john.smith",
PublicEmail: "[email protected]",
},
Commit: &Commit{
ID: "6c016b801a88f4bd31f927fc045b5c746a6f823e",
ShortID: "6c016b80",
CreatedAt: stringToTime("2018-03-21T14:41:00.000+01:00", t),
ParentIDs: []string{"6008b4902d40799ab11688e502d9f1f27f6d2e18"},
Title: "Update env for specific runner",
Message: "Update env for specific runner\n",
AuthorName: "John SMITH",
AuthorEmail: "[email protected]",
AuthoredDate: stringToTime("2018-03-21T14:41:00.000+01:00", t),
CommitterName: "John SMITH",
CommitterEmail: "[email protected]",
CommittedDate: stringToTime("2018-03-21T14:41:00.000+01:00", t),
WebURL: "https://gitlab.example.com/awesome/packages/common/-/commit/6c016b801a88f4bd31f927fc045b5c746a6f823e",
},
Pipeline: pipeline,
WebURL: "https://gitlab.example.com/awesome/packages/common/-/jobs/14606",
Project: &Project{
ID: 3252,
Description: "Common nodejs paquet for producer",
Name: "common",
NameWithNamespace: "awesome",
Path: "common",
PathWithNamespace: "awesome",
CreatedAt: stringToTime("2018-02-13T09:21:48.107Z", t),
},
},
}
}
func TestListRunnersJobs(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api/v4/runners/1/jobs", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
fmt.Fprint(w, exampleListRunnerJobs)
})

opt := &ListRunnerJobsOptions{}
Expand All @@ -56,7 +130,7 @@ func TestListRunnersJobs(t *testing.T) {
t.Fatalf("Runners.ListRunnersJobs returns an error: %v", err)
}

want := []*Job{{ID: 1}, {ID: 2}}
want := expectedRunnerJobs(t)
if !reflect.DeepEqual(want, jobs) {
t.Errorf("Runners.ListRunnersJobs returned %+v, want %+v", jobs, want)
}
Expand Down

0 comments on commit fd3f0d2

Please sign in to comment.