Skip to content

Commit

Permalink
🐛 Parse Gitlab Status fields to align w/Github Status and Conclusion (#…
Browse files Browse the repository at this point in the history
…3706)

* fix: parse gitlab pipeline status to their GitHub equivalent

Signed-off-by: Allen Shearin <allen.p.shearin@gmail.com>

* change completed string to const

Signed-off-by: Allen Shearin <allen.p.shearin@gmail.com>

---------

Signed-off-by: Allen Shearin <allen.p.shearin@gmail.com>
  • Loading branch information
ashearin committed Dec 4, 2023
1 parent d882fc7 commit e4fc815
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 5 deletions.
40 changes: 36 additions & 4 deletions clients/gitlabrepo/checkruns.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,42 @@ func checkRunsFrom(data []*gitlab.PipelineInfo) []clients.CheckRun {
for _, pipelineInfo := range data {
// TODO: Can get more info from GitLab API here (e.g. pipeline name, URL)
// https://docs.gitlab.com/ee/api/pipelines.html#get-a-pipelines-test-report
checkRuns = append(checkRuns, clients.CheckRun{
Status: pipelineInfo.Status,
URL: pipelineInfo.WebURL,
})
checkRuns = append(checkRuns, parseGitlabStatus(pipelineInfo))
}
return checkRuns
}

// Conclusion does not exist in the pipelines for gitlab,
// so we parse the status to determine the conclusion if it exists.
func parseGitlabStatus(info *gitlab.PipelineInfo) clients.CheckRun {
checkrun := clients.CheckRun{
URL: info.WebURL,
}
const completed = "completed"

switch info.Status {
case "created", "waiting_for_resource", "preparing", "pending", "scheduled":
checkrun.Status = "queued"
case "running":
checkrun.Status = "in_progress"
case "failed":
checkrun.Status = completed
checkrun.Conclusion = "failure"
case "success":
checkrun.Status = completed
checkrun.Conclusion = "success"
case "canceled":
checkrun.Status = completed
checkrun.Conclusion = "cancelled"
case "skipped":
checkrun.Status = completed
checkrun.Conclusion = "skipped"
case "manual":
checkrun.Status = completed
checkrun.Conclusion = "action_required"
default:
checkrun.Status = info.Status
}

return checkrun
}
124 changes: 123 additions & 1 deletion clients/gitlabrepo/checkruns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Test_CheckRuns(t *testing.T) {
responsePath: "./testdata/valid-checkruns",
want: []clients.CheckRun{
{
Status: "pending",
Status: "queued",
URL: "https://example.com/foo/bar/pipelines/48",
Conclusion: "",
},
Expand Down Expand Up @@ -88,3 +88,125 @@ func Test_CheckRuns(t *testing.T) {
})
}
}

func TestParseGitlabStatus(t *testing.T) {
t.Parallel()
tests := []struct {
status string
want clients.CheckRun
}{
{
status: "created",
want: clients.CheckRun{
Status: "queued",
URL: "https://example.com/foo/bar/pipelines/48",
Conclusion: "",
},
},
{
status: "waiting_for_resource",
want: clients.CheckRun{
Status: "queued",
URL: "https://example.com/foo/bar/pipelines/48",
Conclusion: "",
},
},
{
status: "preparing",
want: clients.CheckRun{
Status: "queued",
URL: "https://example.com/foo/bar/pipelines/48",
Conclusion: "",
},
},
{
status: "pending",
want: clients.CheckRun{
Status: "queued",
URL: "https://example.com/foo/bar/pipelines/48",
Conclusion: "",
},
},
{
status: "scheduled",
want: clients.CheckRun{
Status: "queued",
URL: "https://example.com/foo/bar/pipelines/48",
Conclusion: "",
},
},
{
status: "running",
want: clients.CheckRun{
Status: "in_progress",
URL: "https://example.com/foo/bar/pipelines/48",
Conclusion: "",
},
},
{
status: "failed",
want: clients.CheckRun{
Status: "completed",
URL: "https://example.com/foo/bar/pipelines/48",
Conclusion: "failure",
},
},
{
status: "success",
want: clients.CheckRun{
Status: "completed",
URL: "https://example.com/foo/bar/pipelines/48",
Conclusion: "success",
},
},
{
status: "canceled",
want: clients.CheckRun{
Status: "completed",
URL: "https://example.com/foo/bar/pipelines/48",
Conclusion: "cancelled",
},
},
{
status: "skipped",
want: clients.CheckRun{
Status: "completed",
URL: "https://example.com/foo/bar/pipelines/48",
Conclusion: "skipped",
},
},
{
status: "manual",
want: clients.CheckRun{
Status: "completed",
URL: "https://example.com/foo/bar/pipelines/48",
Conclusion: "action_required",
},
},
{
status: "invalid_status",
want: clients.CheckRun{
Status: "invalid_status",
URL: "https://example.com/foo/bar/pipelines/48",
Conclusion: "",
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.status, func(t *testing.T) {
t.Parallel()

info := gitlab.PipelineInfo{
WebURL: "https://example.com/foo/bar/pipelines/48",
Status: tt.status,
}

got := parseGitlabStatus(&info)

if !cmp.Equal(got, tt.want) {
t.Errorf("parseGitlabStatus() = %v, want %v", got, cmp.Diff(got, tt.want))
}
})
}
}

0 comments on commit e4fc815

Please sign in to comment.