Skip to content

Commit fd3f0d2

Browse files
committed
fix some missing fields in Job
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.
1 parent 90bef1b commit fd3f0d2

File tree

3 files changed

+162
-7
lines changed

3 files changed

+162
-7
lines changed

fixtures_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,86 @@ const (
9393
"token": "6337ff461c94fd3fa32ba3b1ff4125"
9494
}`
9595

96+
// exampleRunnerJob provides fixture for ListRunnerJobs test
97+
exampleListRunnerJobs = `
98+
[
99+
{
100+
"id": 1,
101+
"status": "failed",
102+
"stage": "test",
103+
"name": "run_tests",
104+
"ref": "master",
105+
"tag": false,
106+
"coverage": null,
107+
"allow_failure": false,
108+
"created_at": "2021-10-22T11:59:25.201Z",
109+
"started_at": "2021-10-22T11:59:33.660Z",
110+
"finished_at": "2021-10-22T15:59:25.201Z",
111+
"duration": 171.540594,
112+
"queued_duration": 2.535766,
113+
"user": {
114+
"id": 368,
115+
"name": "John SMITH",
116+
"username": "john.smith",
117+
"state": "blocked",
118+
"avatar_url": "https://gitlab.example.com/uploads/-/system/user/avatar/368/avatar.png",
119+
"web_url": "https://gitlab.example.com/john.smith",
120+
"bio": "",
121+
"location": "",
122+
"public_email": "[email protected]",
123+
"skype": "",
124+
"linkedin": "",
125+
"twitter": "",
126+
"website_url": "",
127+
"organization": "",
128+
"job_title": "",
129+
"pronouns": null,
130+
"bot": false,
131+
"work_information": null,
132+
"bio_html": ""
133+
},
134+
"commit": {
135+
"id": "6c016b801a88f4bd31f927fc045b5c746a6f823e",
136+
"short_id": "6c016b80",
137+
"created_at": "2018-03-21T14:41:00.000+01:00",
138+
"parent_ids": [
139+
"6008b4902d40799ab11688e502d9f1f27f6d2e18"
140+
],
141+
"title": "Update env for specific runner",
142+
"message": "Update env for specific runner\n",
143+
"author_name": "John SMITH",
144+
"author_email": "[email protected]",
145+
"authored_date": "2018-03-21T14:41:00.000+01:00",
146+
"committer_name": "John SMITH",
147+
"committer_email": "[email protected]",
148+
"committed_date": "2018-03-21T14:41:00.000+01:00",
149+
"trailers": {},
150+
"web_url": "https://gitlab.example.com/awesome/packages/common/-/commit/6c016b801a88f4bd31f927fc045b5c746a6f823e"
151+
},
152+
"pipeline": {
153+
"id": 8777,
154+
"project_id": 3252,
155+
"sha": "6c016b801a88f4bd31f927fc045b5c746a6f823e",
156+
"ref": "master",
157+
"status": "failed",
158+
"source": "push",
159+
"created_at": "2018-03-21T13:41:15.356Z",
160+
"updated_at": "2018-03-21T15:12:52.021Z",
161+
"web_url": "https://gitlab.example.com/awesome/packages/common/-/pipelines/8777"
162+
},
163+
"web_url": "https://gitlab.example.com/awesome/packages/common/-/jobs/14606",
164+
"project": {
165+
"id": 3252,
166+
"description": "Common nodejs paquet for producer",
167+
"name": "common",
168+
"name_with_namespace": "awesome",
169+
"path": "common",
170+
"path_with_namespace": "awesome",
171+
"created_at": "2018-02-13T09:21:48.107Z"
172+
}
173+
}
174+
]`
175+
96176
// exampleReleaseLink provides fixture for Release Links tests.
97177
exampleReleaseLink = `{
98178
"id":1,

jobs.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ type Job struct {
7171
IsShared bool `json:"is_shared"`
7272
Name string `json:"name"`
7373
} `json:"runner"`
74-
Stage string `json:"stage"`
75-
Status string `json:"status"`
76-
Tag bool `json:"tag"`
77-
WebURL string `json:"web_url"`
78-
User *User `json:"user"`
74+
Stage string `json:"stage"`
75+
Status string `json:"status"`
76+
Tag bool `json:"tag"`
77+
WebURL string `json:"web_url"`
78+
User *User `json:"user"`
79+
Project *Project `json:"project"`
7980
}
8081

8182
// Bridge represents a pipeline bridge.

runners_test.go

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,87 @@ func TestDisableRunner(t *testing.T) {
4040
}
4141
}
4242

43+
func stringToTime(date string, t *testing.T) *time.Time {
44+
d, err := time.Parse(timeLayout, date)
45+
if err != nil {
46+
t.Errorf("Unable to parse date [%s]: %v", d, err)
47+
}
48+
return &d
49+
}
50+
51+
func expectedRunnerJobs(t *testing.T) []*Job {
52+
pipeline := struct {
53+
ID int `json:"id"`
54+
Ref string `json:"ref"`
55+
Sha string `json:"sha"`
56+
Status string `json:"status"`
57+
}{
58+
ID: 8777,
59+
Ref: "master",
60+
Sha: "6c016b801a88f4bd31f927fc045b5c746a6f823e",
61+
Status: "failed",
62+
}
63+
64+
return []*Job{
65+
&Job{
66+
ID: 1,
67+
Status: "failed",
68+
Stage: "test",
69+
Name: "run_tests",
70+
Ref: "master",
71+
Tag: false,
72+
Coverage: 0,
73+
AllowFailure: false,
74+
CreatedAt: stringToTime("2021-10-22T11:59:25.201Z", t),
75+
StartedAt: stringToTime("2021-10-22T11:59:33.660Z", t),
76+
FinishedAt: stringToTime("2021-10-22T15:59:25.201Z", t),
77+
Duration: 171.540594,
78+
QueuedDuration: 2.535766,
79+
User: &User{
80+
ID: 368,
81+
Name: "John SMITH",
82+
Username: "john.smith",
83+
AvatarURL: "https://gitlab.example.com/uploads/-/system/user/avatar/368/avatar.png",
84+
State: "blocked",
85+
WebURL: "https://gitlab.example.com/john.smith",
86+
PublicEmail: "[email protected]",
87+
},
88+
Commit: &Commit{
89+
ID: "6c016b801a88f4bd31f927fc045b5c746a6f823e",
90+
ShortID: "6c016b80",
91+
CreatedAt: stringToTime("2018-03-21T14:41:00.000+01:00", t),
92+
ParentIDs: []string{"6008b4902d40799ab11688e502d9f1f27f6d2e18"},
93+
Title: "Update env for specific runner",
94+
Message: "Update env for specific runner\n",
95+
AuthorName: "John SMITH",
96+
AuthorEmail: "[email protected]",
97+
AuthoredDate: stringToTime("2018-03-21T14:41:00.000+01:00", t),
98+
CommitterName: "John SMITH",
99+
CommitterEmail: "[email protected]",
100+
CommittedDate: stringToTime("2018-03-21T14:41:00.000+01:00", t),
101+
WebURL: "https://gitlab.example.com/awesome/packages/common/-/commit/6c016b801a88f4bd31f927fc045b5c746a6f823e",
102+
},
103+
Pipeline: pipeline,
104+
WebURL: "https://gitlab.example.com/awesome/packages/common/-/jobs/14606",
105+
Project: &Project{
106+
ID: 3252,
107+
Description: "Common nodejs paquet for producer",
108+
Name: "common",
109+
NameWithNamespace: "awesome",
110+
Path: "common",
111+
PathWithNamespace: "awesome",
112+
CreatedAt: stringToTime("2018-02-13T09:21:48.107Z", t),
113+
},
114+
},
115+
}
116+
}
43117
func TestListRunnersJobs(t *testing.T) {
44118
mux, server, client := setup(t)
45119
defer teardown(server)
46120

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

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

59-
want := []*Job{{ID: 1}, {ID: 2}}
133+
want := expectedRunnerJobs(t)
60134
if !reflect.DeepEqual(want, jobs) {
61135
t.Errorf("Runners.ListRunnersJobs returned %+v, want %+v", jobs, want)
62136
}

0 commit comments

Comments
 (0)