Skip to content

Commit 4f7d134

Browse files
author
Hiroki Takatsuka
authored
feat(31): Get build pages with org, repo and job name (#32)
* use /v4/jobs instead of /v4/events * feat(31): get build page url with org/repo, job name * gofmt
1 parent e01974b commit 4f7d134

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

pkg/sdapi/sdapi.go

+54-5
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,23 @@ type tokenResponse struct {
4848
JWT string `json:"token"`
4949
}
5050

51+
type pipelineResponse struct {
52+
Name string `json:"name"`
53+
SCMRepo struct {
54+
Name string `json:"name"`
55+
Branch string `json:"branch"`
56+
URL string `json:"url"`
57+
} `json:"scmRepo"`
58+
}
59+
5160
type buildResponse struct {
5261
EventID int `json:"eventId"`
62+
JobID int `json:"jobId"`
63+
}
64+
65+
type jobResponse struct {
66+
PipelineID int `json:"pipelineId"`
67+
Name string `json:"name"`
5368
}
5469

5570
type eventResponse struct {
@@ -308,6 +323,7 @@ func (sd *SDAPI) ValidatorTemplate(yaml string, retried bool) error {
308323
func (sd *SDAPI) GetPipelinePageFromBuildID(buildID string) error {
309324
buildIDList := strings.Split(strings.Replace(strings.TrimSpace(buildID), "\n", " ", -1), " ")
310325
buildIDLength := len(buildIDList)
326+
basePipelineURL := strings.Replace(sd.sdctx.APIURL, "api-cd", "cd", 1) + "/pipelines/"
311327

312328
var wg sync.WaitGroup
313329
wg.Add(buildIDLength)
@@ -318,17 +334,22 @@ func (sd *SDAPI) GetPipelinePageFromBuildID(buildID string) error {
318334
go func(b string) {
319335
defer wg.Done()
320336

321-
br, err := sd.getBuilds(b)
337+
br, err := sd.getBuild(b)
338+
if err != nil {
339+
exit <- err
340+
return
341+
}
342+
jr, err := sd.getJob(br.JobID)
322343
if err != nil {
323344
exit <- err
324345
return
325346
}
326-
er, err := sd.getEvents(br.EventID)
347+
pr, err := sd.getPipeline(jr.PipelineID)
327348
if err != nil {
328349
exit <- err
329350
return
330351
}
331-
println(strings.Replace(sd.sdctx.APIURL, "api-cd", "cd", 1) + "/pipelines/" + strconv.Itoa(er.PipelineID) + "/builds/" + b)
352+
fmt.Println(basePipelineURL + strconv.Itoa(jr.PipelineID) + "/builds/" + b + "\t" + pr.SCMRepo.Name + "(" + jr.Name + ")")
332353
}(b)
333354
}
334355

@@ -342,7 +363,21 @@ func (sd *SDAPI) GetPipelinePageFromBuildID(buildID string) error {
342363
}
343364
}
344365

345-
func (sd *SDAPI) getBuilds(buildID string) (*buildResponse, error) {
366+
func (sd *SDAPI) getPipeline(pipelineID int) (*pipelineResponse, error) {
367+
path := "/v4/pipelines/" + strconv.Itoa(pipelineID) + "?token=" + sd.sdctx.SDJWT
368+
res, err := sd.request(context.TODO(), http.MethodGet, path, nil)
369+
if err != nil {
370+
return nil, err
371+
}
372+
defer res.Body.Close()
373+
374+
pipelineResponse := new(pipelineResponse)
375+
err = json.NewDecoder(res.Body).Decode(pipelineResponse)
376+
377+
return pipelineResponse, err
378+
}
379+
380+
func (sd *SDAPI) getBuild(buildID string) (*buildResponse, error) {
346381
path := "/v4/builds/" + buildID + "?token=" + sd.sdctx.SDJWT
347382
res, err := sd.request(context.TODO(), http.MethodGet, path, nil)
348383
if err != nil {
@@ -356,7 +391,21 @@ func (sd *SDAPI) getBuilds(buildID string) (*buildResponse, error) {
356391
return buildResponse, err
357392
}
358393

359-
func (sd *SDAPI) getEvents(eventID int) (*eventResponse, error) {
394+
func (sd *SDAPI) getJob(jobID int) (*jobResponse, error) {
395+
path := "/v4/jobs/" + strconv.Itoa(jobID) + "?token=" + sd.sdctx.SDJWT
396+
res, err := sd.request(context.TODO(), http.MethodGet, path, nil)
397+
if err != nil {
398+
return nil, err
399+
}
400+
defer res.Body.Close()
401+
402+
jobResponse := new(jobResponse)
403+
err = json.NewDecoder(res.Body).Decode(jobResponse)
404+
405+
return jobResponse, err
406+
}
407+
408+
func (sd *SDAPI) getEvent(eventID int) (*eventResponse, error) {
360409
path := "/v4/events/" + strconv.Itoa(eventID) + "?token=" + sd.sdctx.SDJWT
361410
res, err := sd.request(context.TODO(), http.MethodGet, path, nil)
362411
if err != nil {

0 commit comments

Comments
 (0)