diff --git a/commands/ci/get/get.go b/commands/ci/get/get.go index 4f18b538..ad23bc89 100644 --- a/commands/ci/get/get.go +++ b/commands/ci/get/get.go @@ -9,6 +9,7 @@ import ( "github.com/xanzy/go-gitlab" "gitlab.com/gitlab-org/cli/api" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/commands/mr/mrutils" "gitlab.com/gitlab-org/cli/pkg/git" "gitlab.com/gitlab-org/cli/pkg/tableprinter" @@ -63,7 +64,24 @@ func NewCmdGet(f *cmdutils.Factory) *cobra.Command { if err != nil { return err } - pipelineId = commit.LastPipeline.ID + + // The latest commit on the branch won't work with a merged + // result pipeline + if commit.LastPipeline == nil { + mr, _, err := mrutils.MRFromArgs(f, args, "any") + if err != nil { + return err + } + + if mr.HeadPipeline == nil { + return fmt.Errorf("no pipeline found. It might not exist yet. If this problem continues, check your pipeline configuration.") + } else { + pipelineId = mr.HeadPipeline.ID + } + + } else { + pipelineId = commit.LastPipeline.ID + } } pipeline, err := api.GetPipeline(apiClient, pipelineId, nil, repo.FullName()) diff --git a/commands/ci/get/get_test.go b/commands/ci/get/get_test.go index af0ad524..4498f6e5 100644 --- a/commands/ci/get/get_test.go +++ b/commands/ci/get/get_test.go @@ -392,6 +392,88 @@ updated: 2023-10-10 00:00:00 +0000 UTC # Variables: No variables found in pipeline. +`, + }, + { + name: "when there is a merged result pipeline and no commit pipeline", + args: "-b=main", + httpMocks: []httpMock{ + { + http.MethodGet, + "/api/v4/projects/OWNER%2FREPO/pipelines/123", + http.StatusOK, + `{ + "id": 123, + "iid": 123, + "project_id": 5, + "status": "pending", + "source": "push", + "ref": "main", + "sha": "0ff3ae198f8601a285adcf5c0fff204ee6fba5fd", + "user": { + "username": "test" + }, + "yaml_errors": "-", + "created_at": "2023-10-10T00:00:00Z", + "started_at": "2023-10-10T00:00:00Z", + "updated_at": "2023-10-10T00:00:00Z" + }`, + InlineBody, + }, + { + http.MethodGet, + "/api/v4/projects/OWNER%2FREPO/pipelines/123/jobs?per_page=100", + http.StatusOK, + `[]`, + InlineBody, + }, + { + http.MethodGet, + "/api/v4/projects/OWNER%2FREPO/merge_requests/1", + http.StatusOK, + `{ + "head_pipeline": { + "id": 123 + } + }`, + InlineBody, + }, + { + http.MethodGet, + "/api/v4/projects/OWNER%2FREPO/merge_requests?per_page=30&source_branch=main", + http.StatusOK, + `[ + { + "iid": 1 + } + ]`, + InlineBody, + }, + { + http.MethodGet, + "/api/v4/projects/OWNER%2FREPO/repository/commits/main", + http.StatusOK, + `{ + "last_pipeline": null + }`, + InlineBody, + }, + }, + expectedOut: `# Pipeline: +id: 123 +status: pending +source: push +ref: main +sha: 0ff3ae198f8601a285adcf5c0fff204ee6fba5fd +tag: false +yaml Errors: - +user: test +created: 2023-10-10 00:00:00 +0000 UTC +started: 2023-10-10 00:00:00 +0000 UTC +updated: 2023-10-10 00:00:00 +0000 UTC + +# Jobs: + `, }, { diff --git a/commands/cmdtest/helper.go b/commands/cmdtest/helper.go index 01bdf124..4fc7f723 100644 --- a/commands/cmdtest/helper.go +++ b/commands/cmdtest/helper.go @@ -162,6 +162,9 @@ func InitFactory(ios *iostreams.IOStreams, rt http.RoundTripper) *cmdutils.Facto BaseRepo: func() (glrepo.Interface, error) { return glrepo.New("OWNER", "REPO"), nil }, + Branch: func() (string, error) { + return "main", nil + }, } }