Skip to content
Snippets Groups Projects
Commit 79f90ef3 authored by Anatoli Babenia's avatar Anatoli Babenia 🛠️ Committed by Tomas Vik
Browse files

fix: replace `GetLastPipeline` calls with `GetLatestPipeline`

parent 7242cf3e
No related branches found
No related tags found
1 merge request!1917fix: replace `GetLastPipeline` calls with `GetLatestPipeline`
......@@ -2,13 +2,10 @@ package api
import (
"bytes"
"errors"
"io"
"sort"
"strconv"
gitlab "gitlab.com/gitlab-org/api/client-go"
"gitlab.com/gitlab-org/cli/pkg/dbg"
"gitlab.com/gitlab-org/cli/pkg/git"
)
......@@ -132,45 +129,6 @@ var GetLatestPipeline = func(client *gitlab.Client, repo string, ref string) (*g
return pipeline, nil
}
// XXX Deprecate and use GetLatestPipeline
// Commit.LastPipeline may return pipeline from a wrong branch
// See https://gitlab.com/gitlab-org/gitlab/-/issues/515375
var GetLastPipeline = func(client *gitlab.Client, repo string, ref string) (*gitlab.PipelineInfo, error) {
if client == nil {
client = apiClient.Lab()
}
c, _, err := client.Commits.GetCommit(repo, ref, nil)
if err != nil {
dbg.Debug("go-client GetCommit error:", err.Error())
return nil, err
}
if c.LastPipeline != nil {
dbg.Debug("GetCommit LastPipeline:", strconv.Itoa(c.LastPipeline.ID))
return c.LastPipeline, nil
}
l := &gitlab.ListProjectPipelinesOptions{
Ref: gitlab.Ptr(ref),
Sort: gitlab.Ptr("desc"),
ListOptions: gitlab.ListOptions{
Page: 1,
PerPage: 1,
},
}
pipes, err := GetPipelines(client, l, repo)
if err != nil {
return nil, err
}
if len(pipes) == 0 {
return nil, errors.New("No pipeline running or available for ref " + ref)
}
return pipes[0], nil
}
var GetPipelines = func(client *gitlab.Client, l *gitlab.ListProjectPipelinesOptions, repo interface{}) ([]*gitlab.PipelineInfo, error) {
if client == nil {
client = apiClient.Lab()
......@@ -282,7 +240,7 @@ var GetPipelineFromBranch = func(client *gitlab.Client, ref, repo string) ([]*gi
}
}
pipeline, err := GetLastPipeline(client, repo, ref)
pipeline, err := GetLatestPipeline(client, repo, ref)
if err != nil {
return nil, err
}
......
......@@ -169,12 +169,12 @@ func getPipelineId(inputs *JobInputs, opts *JobOptions) (int, error) {
return inputs.PipelineId, nil
}
branch, err := getBranch(inputs.Branch, opts)
branch, err := getBranch(inputs.Branch)
if err != nil {
return 0, fmt.Errorf("get branch: %w", err)
}
pipeline, err := api.GetLastPipeline(opts.ApiClient, opts.Repo.FullName(), branch)
pipeline, err := api.GetLatestPipeline(opts.ApiClient, opts.Repo.FullName(), branch)
if err != nil {
return 0, fmt.Errorf("get last pipeline: %w", err)
}
......@@ -202,7 +202,7 @@ func GetDefaultBranch(f *cmdutils.Factory) string {
return branch
}
func getBranch(branch string, opts *JobOptions) (string, error) {
func getBranch(branch string) (string, error) {
if branch != "" {
return branch, nil
}
......
......@@ -78,12 +78,10 @@ func TestGetJobId(t *testing.T) {
httpMocks: []httpMock{
{
http.MethodGet,
"/api/v4/projects/OWNER/REPO/repository/commits/main",
"/api/v4/projects/OWNER%2FREPO/pipelines/latest?ref=main",
http.StatusOK,
`{
"last_pipeline": {
"id": 123
}
"id": 123
}`,
},
{
......@@ -106,12 +104,12 @@ func TestGetJobId(t *testing.T) {
name: "when getJobId with name and last pipeline is requested and getCommits throws error",
jobName: "lint",
pipelineId: 0,
expectedError: "get pipeline: get last pipeline: GET https://gitlab.com/api/v4/projects/OWNER/REPO/repository/commits/main: 403",
expectedError: "get pipeline: get last pipeline: GET https://gitlab.com/api/v4/projects/OWNER/REPO/pipelines/latest: 403",
expectedOut: 0,
httpMocks: []httpMock{
{
http.MethodGet,
"/api/v4/projects/OWNER/REPO/repository/commits/main",
"/api/v4/projects/OWNER%2FREPO/pipelines/latest?ref=main",
http.StatusForbidden,
`{}`,
},
......@@ -125,12 +123,10 @@ func TestGetJobId(t *testing.T) {
httpMocks: []httpMock{
{
http.MethodGet,
"/api/v4/projects/OWNER/REPO/repository/commits/main",
"/api/v4/projects/OWNER%2FREPO/pipelines/latest?ref=main",
http.StatusOK,
`{
"last_pipeline": {
"id": 123
}
"id": 123
}`,
},
{
......
......@@ -154,12 +154,10 @@ func TestCiRetry(t *testing.T) {
},
{
http.MethodGet,
"/api/v4/projects/OWNER%2FREPO/repository/commits/main",
"/api/v4/projects/OWNER%2FREPO/pipelines/latest?ref=main",
http.StatusOK,
`{
"last_pipeline": {
"id": 123
}
"id": 123
}`,
},
{
......
......@@ -160,12 +160,10 @@ func TestCiTrace(t *testing.T) {
},
{
http.MethodGet,
"/api/v4/projects/OWNER/REPO/repository/commits/main",
"/api/v4/projects/OWNER%2FREPO/pipelines/latest?ref=main",
http.StatusOK,
`{
"last_pipeline": {
"id": 123
}
"id": 123
}`,
},
{
......
......@@ -154,12 +154,10 @@ func TestCiTrigger(t *testing.T) {
},
{
http.MethodGet,
"/api/v4/projects/OWNER%2FREPO/repository/commits/main",
"/api/v4/projects/OWNER%2FREPO/pipelines/latest?ref=main",
http.StatusOK,
`{
"last_pipeline": {
"id": 123
}
"id": 123
}`,
},
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment