From dcbfa3cd618ccd55740f790f907a32bf36409dae Mon Sep 17 00:00:00 2001 From: Allen Shearin Date: Wed, 29 Nov 2023 17:01:11 -0700 Subject: [PATCH] fix: parse gitlab pipeline status to their GitHub equivalent Signed-off-by: Allen Shearin --- clients/gitlabrepo/checkruns.go | 40 ++++++++- clients/gitlabrepo/checkruns_test.go | 124 ++++++++++++++++++++++++++- 2 files changed, 159 insertions(+), 5 deletions(-) diff --git a/clients/gitlabrepo/checkruns.go b/clients/gitlabrepo/checkruns.go index 5744b1df052..fde55fbc046 100644 --- a/clients/gitlabrepo/checkruns.go +++ b/clients/gitlabrepo/checkruns.go @@ -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, + } + 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 +} diff --git a/clients/gitlabrepo/checkruns_test.go b/clients/gitlabrepo/checkruns_test.go index 2ff87a4925e..c11ff2b5408 100644 --- a/clients/gitlabrepo/checkruns_test.go +++ b/clients/gitlabrepo/checkruns_test.go @@ -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: "", }, @@ -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)) + } + }) + } +}