diff --git a/CHANGELOG.md b/CHANGELOG.md index cf6da613..f720d6b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [0ver](https://0ver.org) (more or less). ### Changed - Fixed the error handling when comparing 2 refs which resulted into nil pointer dereferences +- Fixed the pulling of merge-request based pipelines - Bumped all dependencies ## [v0.4.9] - 2021-05-05 diff --git a/pkg/exporter/pipelines.go b/pkg/exporter/pipelines.go index f0ea68d7..a83cc725 100644 --- a/pkg/exporter/pipelines.go +++ b/pkg/exporter/pipelines.go @@ -36,13 +36,21 @@ func pullRefMetrics(ref schemas.Ref) error { } } + // We need a different syntax if the ref is a merge-request + var refName string + if ref.Kind == schemas.RefKindMergeRequest { + refName = fmt.Sprintf("refs/merge-requests/%s/head", ref.Name) + } else { + refName = ref.Name + } + pipelines, err := gitlabClient.GetProjectPipelines(ref.ProjectName, &goGitlab.ListProjectPipelinesOptions{ // We only need the most recent pipeline ListOptions: goGitlab.ListOptions{ PerPage: 1, Page: 1, }, - Ref: goGitlab.String(ref.Name), + Ref: &refName, }) if err != nil { return fmt.Errorf("error fetching project pipelines for %s: %v", ref.ProjectName, err) diff --git a/pkg/exporter/pipelines_test.go b/pkg/exporter/pipelines_test.go index 1323ae93..834795be 100644 --- a/pkg/exporter/pipelines_test.go +++ b/pkg/exporter/pipelines_test.go @@ -16,6 +16,7 @@ func TestPullRefMetricsSucceed(t *testing.T) { mux.HandleFunc("/api/v4/projects/foo/pipelines", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "bar", r.URL.Query().Get("ref")) fmt.Fprint(w, `[{"id":1}]`) }) @@ -87,6 +88,37 @@ func TestPullRefMetricsSucceed(t *testing.T) { } func TestPullRefMetricsMergeRequestPipeline(t *testing.T) { + resetGlobalValues() + mux, server := configureMockedGitlabClient() + defer server.Close() + + mux.HandleFunc("/api/v4/projects/foo/pipelines", + func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "refs/merge-requests/1234/head", r.URL.Query().Get("ref")) + fmt.Fprint(w, `[{"id":1}]`) + }) + + mux.HandleFunc("/api/v4/projects/foo/pipelines/1", + func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `{"id":1,"updated_at":"2016-08-11T11:28:34.085Z","duration":300,"status":"running","coverage":"30.2"}`) + }) + + mux.HandleFunc(fmt.Sprintf("/api/v4/projects/foo/pipelines/1/variables"), + func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "GET", r.Method) + fmt.Fprint(w, `[{"key":"foo","value":"bar"}]`) + }) + + // Metrics pull shall succeed + assert.NoError(t, pullRefMetrics(schemas.Ref{ + Kind: schemas.RefKindMergeRequest, + ProjectName: "foo", + Name: "1234", + PullPipelineVariablesEnabled: true, + })) +} + +func TestPullRefMetricsMergeRequestPipelineAlreadyLoaded(t *testing.T) { resetGlobalValues() ref := schemas.Ref{ Kind: schemas.RefKindMergeRequest,