Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont set commit status for old commits to new pipelines #4784

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,13 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
if mr.HeadPipeline != nil {
logger.Debug("Head pipeline found for merge request %d, source '%s'. refTarget '%s'",
pull.Num, mr.HeadPipeline.Source, mr.HeadPipeline.Ref)

// if these are different then there is already a new commit so let's stop setting any statuses and return
if mr.HeadPipeline.SHA != pull.HeadCommit {
logger.Debug("mr.HeadPipeline.SHA: '%s' does not match pull.HeadCommit '%s'", mr.HeadPipeline.SHA, pull.HeadCommit)
return nil
}

// set pipeline ID for the req once found
pipelineID = gitlab.Ptr(mr.HeadPipeline.ID)
break
Expand Down
89 changes: 80 additions & 9 deletions server/events/vcs/gitlab_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import (

var projectID = 4580910

const gitlabPipelineSuccessMrID = 488598
const (
gitlabPipelineSuccessSha = "67cb91d3f6198189f433c045154a885784ba6977"
gitlabPipelineSuccessMrID = 488598
)

// Test that the base url gets set properly.
func TestNewGitlabClient_BaseURL(t *testing.T) {
Expand Down Expand Up @@ -299,7 +302,7 @@ func TestGitlabClient_UpdateStatus(t *testing.T) {
testServer := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
case "/api/v4/projects/runatlantis%2Fatlantis/statuses/sha":
case fmt.Sprintf("/api/v4/projects/runatlantis%satlantis/statuses/%s", "%2F", gitlabPipelineSuccessSha):
gotRequest = true

body, err := io.ReadAll(r.Body)
Expand Down Expand Up @@ -338,7 +341,7 @@ func TestGitlabClient_UpdateStatus(t *testing.T) {
models.PullRequest{
Num: 1,
BaseRepo: repo,
HeadCommit: "sha",
HeadCommit: gitlabPipelineSuccessSha,
HeadBranch: "test",
}, c.status, "src", "description", "https://google.com")
Ok(t, err)
Expand Down Expand Up @@ -389,7 +392,7 @@ func TestGitlabClient_UpdateStatusRetryable(t *testing.T) {
testServer := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
case "/api/v4/projects/runatlantis%2Fatlantis/statuses/sha":
case fmt.Sprintf("/api/v4/projects/runatlantis%satlantis/statuses/%s", "%2F", gitlabPipelineSuccessSha):
handledNumberOfRequests++
shouldSendConflict := handledNumberOfRequests <= c.numberOfConflicts

Expand Down Expand Up @@ -438,12 +441,12 @@ func TestGitlabClient_UpdateStatusRetryable(t *testing.T) {
models.PullRequest{
Num: 1,
BaseRepo: repo,
HeadCommit: "sha",
HeadCommit: gitlabPipelineSuccessSha,
HeadBranch: "test",
}, c.status, "src", "description", "https://google.com")

if c.expError {
ErrContains(t, "failed to update commit status for 'runatlantis/atlantis' @ 'sha' to 'src' after 10 attempts", err)
ErrContains(t, fmt.Sprintf("failed to update commit status for 'runatlantis/atlantis' @ '%s' to 'src' after 10 attempts", gitlabPipelineSuccessSha), err)
ErrContains(t, "409", err)
} else {
Ok(t, err)
Expand All @@ -454,6 +457,74 @@ func TestGitlabClient_UpdateStatusRetryable(t *testing.T) {
}
}

func TestGitlabClient_UpdateStatusSkipNewCommit(t *testing.T) {
logger := logging.NewNoopLogger(t)
pipelineSuccess, err := os.ReadFile("testdata/gitlab-pipeline-success.json")
Ok(t, err)

cases := []struct {
status models.CommitStatus
expState string
}{
{
models.SuccessCommitStatus,
"success",
},
}
for _, c := range cases {
t.Run(c.expState, func(t *testing.T) {
gotRequest := false
testServer := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
case "/api/v4/projects/runatlantis%2Fatlantis/statuses/newsha":
gotRequest = true

body, err := io.ReadAll(r.Body)
Ok(t, err)
exp := fmt.Sprintf(`{"state":"%s","ref":"patch-1-merger","context":"src","target_url":"https://google.com","description":"description"}`, c.expState)
Equals(t, exp, string(body))
defer r.Body.Close() // nolint: errcheck
w.Write([]byte("{}")) // nolint: errcheck
case "/api/v4/projects/runatlantis%2Fatlantis/merge_requests/1":
w.WriteHeader(http.StatusOK)
w.Write(pipelineSuccess) // nolint: errcheck
case "/api/v4/":
// Rate limiter requests.
w.WriteHeader(http.StatusOK)
default:
t.Errorf("got unexpected request at %q", r.RequestURI)
http.Error(w, "not found", http.StatusNotFound)
}
}))

internalClient, err := gitlab.NewClient("token", gitlab.WithBaseURL(testServer.URL))
Ok(t, err)
client := &GitlabClient{
Client: internalClient,
Version: nil,
}

repo := models.Repo{
FullName: "runatlantis/atlantis",
Owner: "runatlantis",
Name: "atlantis",
}
err = client.UpdateStatus(
logger,
repo,
models.PullRequest{
Num: 1,
BaseRepo: repo,
HeadCommit: "newsha",
HeadBranch: "test",
}, c.status, "src", "description", "https://google.com")
Ok(t, err)
Assert(t, gotRequest == false, "expected to get the request")
})
}
}

func TestGitlabClient_PullIsMergeable(t *testing.T) {
logger := logging.NewNoopLogger(t)
gitlabClientUnderTest = true
Expand Down Expand Up @@ -616,9 +687,9 @@ func TestGitlabClient_PullIsMergeable(t *testing.T) {
case fmt.Sprintf("/api/v4/projects/%v", projectID):
w.WriteHeader(http.StatusOK)
w.Write(projectSuccess) // nolint: errcheck
case fmt.Sprintf("/api/v4/projects/%v/repository/commits/67cb91d3f6198189f433c045154a885784ba6977/statuses", projectID):
case fmt.Sprintf("/api/v4/projects/%v/repository/commits/%s/statuses", projectID, gitlabPipelineSuccessSha):
w.WriteHeader(http.StatusOK)
response := fmt.Sprintf(`[{"id":133702594,"sha":"67cb91d3f6198189f433c045154a885784ba6977","ref":"patch-1","status":"%s","name":"%s","target_url":null,"description":"ApplySuccess","created_at":"2018-12-12T18:31:57.957Z","started_at":null,"finished_at":"2018-12-12T18:31:58.480Z","allow_failure":false,"coverage":null,"author":{"id":1755902,"username":"lkysow","name":"LukeKysow","state":"active","avatar_url":"https://secure.gravatar.com/avatar/25fd57e71590fe28736624ff24d41c5f?s=80&d=identicon","web_url":"https://gitlab.com/lkysow"}}]`, c.status, c.statusName)
response := fmt.Sprintf(`[{"id":133702594,"sha":"%s","ref":"patch-1","status":"%s","name":"%s","target_url":null,"description":"ApplySuccess","created_at":"2018-12-12T18:31:57.957Z","started_at":null,"finished_at":"2018-12-12T18:31:58.480Z","allow_failure":false,"coverage":null,"author":{"id":1755902,"username":"lkysow","name":"LukeKysow","state":"active","avatar_url":"https://secure.gravatar.com/avatar/25fd57e71590fe28736624ff24d41c5f?s=80&d=identicon","web_url":"https://gitlab.com/lkysow"}}]`, gitlabPipelineSuccessSha, c.status, c.statusName)
w.Write([]byte(response)) // nolint: errcheck
case "/api/v4/version":
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -658,7 +729,7 @@ func TestGitlabClient_PullIsMergeable(t *testing.T) {
models.PullRequest{
Num: c.mrID,
BaseRepo: repo,
HeadCommit: "67cb91d3f6198189f433c045154a885784ba6977",
HeadCommit: gitlabPipelineSuccessSha,
}, vcsStatusName)

Ok(t, err)
Expand Down