fix: Getting the last pipeline is broken with merged results

This commit is contained in:
Gary Holtz 2024-04-09 19:14:27 +00:00
parent 26ac354ad5
commit 2ae39e1037
3 changed files with 104 additions and 1 deletions

View File

@ -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())

View File

@ -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:
`,
},
{

View File

@ -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
},
}
}