Skip to content

Commit

Permalink
autoupdate: fix: status of previous HEAD commit is evaluated after ba…
Browse files Browse the repository at this point in the history
…se branch update (#109)

When a Pull-Request was updated with it's base branch by the
autoupdater, the GitHub Check status of the previous HEAD commit was
evaluated.
If the check status for it was negative, the PR was suspended wrongly.
This PR fixes the issue.

The autoupdater now also logs a warning message if a check status for a
different commit then the current HEAD commit returned by the update
operation was retrieved.
This could happen if the branch is updated between the both API
operations or GitHub returned old information (which can happen).
The autoupdater is not able to handle the situation correctly.
  • Loading branch information
fho committed Mar 13, 2024
2 parents 0f84840 + 9e08bb7 commit 91b9616
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 96 deletions.
2 changes: 1 addition & 1 deletion internal/autoupdate/autoupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const defPeriodicTriggerInterval = 30 * time.Minute
// GithubClient defines the methods of a GithubAPI Client that are used by the
// autoupdate implementation.
type GithubClient interface {
UpdateBranch(ctx context.Context, owner, repo string, pullRequestNumber int) (changed bool, scheduled bool, err error)
UpdateBranch(ctx context.Context, owner, repo string, pullRequestNumber int) (*githubclt.UpdateBranchResult, error)
CreateIssueComment(ctx context.Context, owner, repo string, issueOrPRNr int, comment string) error
ListPullRequests(ctx context.Context, owner, repo, state, sort, sortDirection string) githubclt.PRIterator
ReadyForMerge(ctx context.Context, owner, repo string, prNumber int) (*githubclt.ReadyForMergeStatus, error)
Expand Down
20 changes: 7 additions & 13 deletions internal/autoupdate/autoupdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,14 @@ func mockSuccessfulGithubUpdateBranchCall(clt *mocks.MockGithubClient, expectedP
return clt.
EXPECT().
UpdateBranch(gomock.Any(), gomock.Eq(repoOwner), gomock.Eq(repo), gomock.Eq(expectedPRNr)).
Return(branchChanged, false, nil)
Return(&githubclt.UpdateBranchResult{HeadCommitID: headCommitID, Changed: branchChanged}, nil)
}

func mockFailedGithubUpdateBranchCall(clt *mocks.MockGithubClient, expectedPRNr int) *gomock.Call {
return clt.
EXPECT().
UpdateBranch(gomock.Any(), gomock.Eq(repoOwner), gomock.Eq(repo), gomock.Eq(expectedPRNr)).
DoAndReturn(func(context.Context, string, string, int) (bool, bool, error) {
return false, false, errors.New("error mocked by mockFailedGithubUpdateBranchCall")
})
Return(nil, errors.New("error mocked by mockFailedGithubUpdateBranchCall"))
}

func mockSuccesssfulCreateIssueCommentCall(clt *mocks.MockGithubClient, expectedPRNr int) *gomock.Call {
Expand Down Expand Up @@ -718,9 +716,7 @@ func TestSuccessStatusOrCheckEventResumesPRs(t *testing.T) {
ghClient.
EXPECT().
UpdateBranch(gomock.Any(), gomock.Eq(repoOwner), gomock.Eq(repo), gomock.Any()).
DoAndReturn(func(context.Context, string, string, int) (bool, bool, error) {
return false, false, nil
}).
Return(&githubclt.UpdateBranchResult{HeadCommitID: headCommitID}, nil).
MinTimes(3)

mergeStatusPr1 := mockReadyForMergeStatus(
Expand Down Expand Up @@ -905,9 +901,7 @@ func TestFailedStatusEventSuspendsFirstPR(t *testing.T) {
ghClient.
EXPECT().
UpdateBranch(gomock.Any(), gomock.Eq(repoOwner), gomock.Eq(repo), gomock.Any()).
DoAndReturn(func(context.Context, string, string, int) (bool, bool, error) {
return false, false, nil
}).
Return(&githubclt.UpdateBranchResult{HeadCommitID: headCommitID}, nil).
AnyTimes()

retryer := goordinator.NewRetryer()
Expand Down Expand Up @@ -1163,7 +1157,7 @@ func TestInitialSync(t *testing.T) {
ghClient.
EXPECT().
UpdateBranch(gomock.Any(), gomock.Eq(repoOwner), gomock.Eq(repo), gomock.Any()).
Return(true, false, nil).
Return(&githubclt.UpdateBranchResult{Changed: true, HeadCommitID: headCommitID}, nil).
AnyTimes()

prIterNone := mocks.NewMockPRIterator(mockctrl)
Expand Down Expand Up @@ -1541,9 +1535,9 @@ func TestBaseBranchUpdatesBlockUntilFinished(t *testing.T) {
ghClient.
EXPECT().
UpdateBranch(gomock.Any(), gomock.Eq(repoOwner), gomock.Eq(repo), gomock.Any()).
DoAndReturn(func(context.Context, string, string, int) (bool, bool, error) {
DoAndReturn(func(context.Context, string, string, int) (*githubclt.UpdateBranchResult, error) {
atomic.AddInt64(&updateBranchCalls, 1)
return true, scheduledReturnVal.Load(), nil
return &githubclt.UpdateBranchResult{Changed: true, HeadCommitID: headCommitID, Scheduled: scheduledReturnVal.Load()}, nil
}).MinTimes(1)

mockSuccessfulGithubAddLabelQueueHeadCall(ghClient, prNumber).AnyTimes()
Expand Down
7 changes: 5 additions & 2 deletions internal/autoupdate/drygithubclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/simplesurance/goordinator/internal/githubclt"
)

const headCommitID = "32d4ff96ea72412277bbfd22ff1bab3a5263b415"

// DryGithubClient is a github-client that does not do any changes on github.
// All operations that could cause a change are simulated and always succeed.
// All all other operations are forwarded to a wrapped GithubClient.
Expand All @@ -23,9 +25,9 @@ func NewDryGithubClient(clt GithubClient, logger *zap.Logger) *DryGithubClient {
}
}

func (c *DryGithubClient) UpdateBranch(context.Context, string, string, int) (bool, bool, error) {
func (c *DryGithubClient) UpdateBranch(context.Context, string, string, int) (*githubclt.UpdateBranchResult, error) {
c.logger.Info("simulated updating of github branch, returning is uptodate")
return false, false, nil
return &githubclt.UpdateBranchResult{HeadCommitID: headCommitID}, nil
}

func (c *DryGithubClient) ReadyForMerge(context.Context, string, string, int) (*githubclt.ReadyForMergeStatus, error) {
Expand All @@ -34,6 +36,7 @@ func (c *DryGithubClient) ReadyForMerge(context.Context, string, string, int) (*
return &githubclt.ReadyForMergeStatus{
ReviewDecision: githubclt.ReviewDecisionApproved,
CIStatus: githubclt.CIStatusSuccess,
Commit: headCommitID,
}, nil
}

Expand Down
9 changes: 4 additions & 5 deletions internal/autoupdate/mocks/autoupdate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 91b9616

Please sign in to comment.