Skip to content
Merged
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
51 changes: 51 additions & 0 deletions internal/githubclt/readyformergestatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package githubclt
import (
"context"
"fmt"
"time"

"github.com/shurcooL/githubv4"
"go.uber.org/zap"
)

// CIStatus is the result or current status of a CI job.
Expand Down Expand Up @@ -39,14 +41,32 @@ type CIJobStatus struct {

// ReadyForMergeStatus represent the information deciding if a pull request is
// ready to be merged. It contains the review decision and CI job results.
// It also contains the information that determines if the pull request is
// still eligible to be in a merge queue: if it is open, if auto-merge is
// enabled and its labels.
type ReadyForMergeStatus struct {
ReviewDecision ReviewDecision
CIStatus CIStatus
Statuses []*CIJobStatus
Commit string

// Closed is true if the pull request is closed or merged.
Closed bool
// AutoMergeEnabled is true if auto-merge is enabled for the pull
// request.
AutoMergeEnabled bool
// Labels contains the names of the labels of the pull request.
// At most the first [maxLabels] labels are retrieved.
Labels []string
}

// maxLabels is the max. number of pull request labels that are retrieved.
// It must be kept in sync with the labels(first: N) argument in the query.
const maxLabels = 100

// ReadyForMerge returns the [review decision] and [status check rollup] for a PR.
// It additionally returns if the PR is closed, if auto-merge is enabled and the
// labels of the PR.
//
// The status of checks that are not required is ignored.
// If a required check failed the returned [ReadyForMerge.CIStatus] is
Expand All @@ -73,6 +93,10 @@ func (clt *Client) ReadyForMerge(ctx context.Context, owner, repo string, prNumb
CIStatus: overallCIStatus(queryResult.StatusCheckRollupState, statuses),
Statuses: statuses,
Commit: queryResult.Commit,

Closed: queryResult.Closed,
AutoMergeEnabled: queryResult.AutoMergeEnabled,
Labels: queryResult.Labels,
}, nil
}

Expand Down Expand Up @@ -244,13 +268,27 @@ type queryCIStatusResult struct {
CheckRuns []*queryCheckStatus
StatusContext []*queryStatusContext
Commit string
Closed bool
AutoMergeEnabled bool
Labels []string
}

func (clt *Client) reviewAndCIStatus(ctx context.Context, owner, repo string, prNumber int) (*queryCIStatusResult, error) {
type graphQLQueryCIStatus struct {
Repository struct {
PullRequest struct {
ReviewDecision githubv4.PullRequestReviewDecision
// Closed is also true for merged pull requests.
Closed bool
AutoMergeRequest struct {
EnabledAt time.Time
}
Labels struct {
PageInfo PageInfo
Nodes []struct {
Name string
}
} `graphql:"labels(first: 100)"`

BaseRef struct {
BranchProtectionRule struct {
Expand Down Expand Up @@ -336,6 +374,19 @@ func (clt *Client) reviewAndCIStatus(ctx context.Context, owner, repo string, pr
result.StatusCheckRollupState = commitsNode.StatusCheckRollup.State
result.RequiredStatusCheckContexts = q.Repository.PullRequest.BaseRef.BranchProtectionRule.RequiredStatusCheckContexts
result.Commit = prHEADCommitID
result.Closed = q.Repository.PullRequest.Closed
result.AutoMergeEnabled = !q.Repository.PullRequest.AutoMergeRequest.EnabledAt.IsZero()
result.Labels = make([]string, 0, len(q.Repository.PullRequest.Labels.Nodes))
for _, label := range q.Repository.PullRequest.Labels.Nodes {
result.Labels = append(result.Labels, label.Name)
}

if q.Repository.PullRequest.Labels.PageInfo.HasNextPage {
clt.logger.Warn("incomplete pr labels retrieved, more label pages are available but only the first page was fetched, pagination is not implemented",
zap.Int("github.pull_request", prNumber),
zap.Int("github.labels_fetched", maxLabels),
)
}

return &result, nil
}
Expand Down
114 changes: 114 additions & 0 deletions internal/githubclt/readyformergestatus_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package githubclt

import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/shurcooL/githubv4"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

func TestOverallCIStatus_optionalFailedChecksAreIgnored(t *testing.T) {
Expand Down Expand Up @@ -142,3 +147,112 @@ func TestOverallCIStatus_Expected(t *testing.T) {

require.Equal(t, CIStatusExpected, status)
}

// readyForMergeResponse returns the response of the GraphQL query of
// reviewAndCIStatus for a PR that has a single successful required check.
// prFields contains additional pullRequest fields in JSON format.
func readyForMergeResponse(prFields string) string {
return `{"data":{"repository":{"pullRequest":{
"reviewDecision":"APPROVED",
` + prFields + `,
"baseRef":{"branchProtectionRule":{"requiredStatusCheckContexts":["build"]}},
"commits":{"nodes":[{"commit":{
"oid":"abc123",
"statusCheckRollup":{
"state":"SUCCESS",
"contexts":{
"pageInfo":{"endCursor":"","hasNextPage":false},
"edges":[{"node":{
"name":"build",
"conclusion":"SUCCESS",
"status":"COMPLETED",
"detailsUrl":"http://localhost/job/build/1"
}}]
}
}
}}]}
}}}}`
}

func newReadyForMergeTestClient(t *testing.T, response string) (clt *Client, requestBody func() string) {
t.Helper()

var body string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("reading request body failed: %v", err)
}
body = string(b)

w.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(w, response)
}))
t.Cleanup(srv.Close)

return &Client{
logger: zaptest.NewLogger(t),
graphQLClt: githubv4.NewEnterpriseClient(srv.URL, srv.Client()),
}, func() string { return body }
}

func TestReadyForMerge_ReturnsAutoMergeLabelsAndOpenState(t *testing.T) {
clt, requestBody := newReadyForMergeTestClient(t, readyForMergeResponse(`
"closed":false,
"autoMergeRequest":{"enabledAt":"2026-01-02T03:04:05Z"},
"labels":{
"pageInfo":{"endCursor":"","hasNextPage":false},
"nodes":[{"name":"queue-add"},{"name":"bug"}]
}`,
))

status, err := clt.ReadyForMerge(context.Background(), "owner", "repo", 1)
require.NoError(t, err)

require.False(t, status.Closed)
require.True(t, status.AutoMergeEnabled)
require.Equal(t, []string{"queue-add", "bug"}, status.Labels)
require.Equal(t, ReviewDecisionApproved, status.ReviewDecision)
require.Equal(t, CIStatusSuccess, status.CIStatus)
require.Equal(t, "abc123", status.Commit)

// the fields must be part of the query
for _, field := range []string{"closed", "autoMergeRequest{enabledAt}", "labels(first: 100)"} {
require.Contains(t, requestBody(), field)
}
}

func TestReadyForMerge_AutoMergeDisabledAndNoLabels(t *testing.T) {
// GitHub returns null as autoMergeRequest if auto-merge is not enabled
clt, _ := newReadyForMergeTestClient(t, readyForMergeResponse(`
"closed":false,
"autoMergeRequest":null,
"labels":{
"pageInfo":{"endCursor":"","hasNextPage":false},
"nodes":[]
}`,
))

status, err := clt.ReadyForMerge(context.Background(), "owner", "repo", 1)
require.NoError(t, err)

require.False(t, status.Closed)
require.False(t, status.AutoMergeEnabled)
require.Empty(t, status.Labels)
}

func TestReadyForMerge_ClosedPR(t *testing.T) {
clt, _ := newReadyForMergeTestClient(t, readyForMergeResponse(`
"closed":true,
"autoMergeRequest":null,
"labels":{
"pageInfo":{"endCursor":"","hasNextPage":false},
"nodes":[]
}`,
))

status, err := clt.ReadyForMerge(context.Background(), "owner", "repo", 1)
require.NoError(t, err)

require.True(t, status.Closed)
}
40 changes: 40 additions & 0 deletions internal/mergequeue/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ type Coordinator struct {
// shutdownChan can be closed to communicate to the event loop go-routine to terminate.
shutdownChan chan struct{}

// emptiedQueues receives the ID of queues that dequeued their last pull
// request on their own, because it was not eligible to be queued
// anymore. The event loop removes them.
// Queues can not remove themselves, they run in the context of their own
// action pool, that the removal has to wait for.
emptiedQueues chan BranchID

// processedEventCnt counts the number of events that were processed.
// It is currently only used in testcase to delay checks until a send
// event was processed.
Expand All @@ -83,6 +90,7 @@ func NewCoordinator(cfg Config) *Coordinator {
wg: sync.WaitGroup{},
periodicTriggerIntv: defPeriodicTriggerInterval,
shutdownChan: make(chan struct{}, 1),
emptiedQueues: make(chan BranchID, 16),
}

cfg.setDefaults()
Expand Down Expand Up @@ -166,6 +174,9 @@ func (a *Coordinator) eventLoop() {

a.processEvent(context.Background(), event)

case branchID := <-a.emptiedQueues:
a.removeQueueIfEmpty(branchID)

case <-periodicTrigger.C:
a.queuesLock.Lock()
for branchID, q := range a.queues {
Expand All @@ -187,6 +198,33 @@ func (a *Coordinator) eventLoop() {
}
}

// removeQueueIfEmpty removes the queue for the branch, if it is empty.
// It must not be called from a queue operation, it waits for the queue's
// running operations to finish.
func (a *Coordinator) removeQueueIfEmpty(branchID BranchID) {
a.queuesLock.Lock()
defer a.queuesLock.Unlock()

q, exist := a.queues[branchID]
if !exist || !q.IsEmpty() {
return
}

q.Wait()
delete(a.queues, branchID)
a.Logger.With(q.baseBranch.Logfields...).Debug("removed empty queue for base branch")
}

// queueEmptied is called by a queue when it became empty because it dequeued
// a pull request on its own. It does not block, if the notification can not be
// delivered the periodic run of the event loop removes the empty queue.
func (a *Coordinator) queueEmptied(branchID BranchID) {
select {
case a.emptiedQueues <- branchID:
default:
}
}

// processEvent processes GitHub webhook events.
//
// Events for repositories that are not listed in
Expand Down Expand Up @@ -921,6 +959,8 @@ func (a *Coordinator) Enqueue(_ context.Context, baseBranch *BaseBranch, pr *Pul
a.Retryer,
a.CI,
a.HeadLabel,
withEnqueueTrigger(a.TriggerOnAutomerge, a.TriggerLabels),
withOnEmpty(func() { a.queueEmptied(baseBranch.BranchID) }),
)

a.queues[baseBranch.BranchID] = q
Expand Down
17 changes: 15 additions & 2 deletions internal/mergequeue/coordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"slices"
"sync"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -140,11 +141,22 @@ type mergeStatusMock struct {
mu sync.Mutex
}

// mockedTriggerLabel is the trigger label that tests configure for the
// coordinator. mockReadyForMergeStatus returns it as label of the pull request.
const mockedTriggerLabel = "queue-add"

// mockReadyForMergeStatus mocks ReadyForMerge to return an open pull request
// that has auto-merge enabled and the mockedTriggerLabel, so that it is
// eligible to be in a queue regardless of the configured trigger.
// To simulate that it is not eligible anymore, change AutoMergeEnabled and
// Labels of the returned mock while holding its mutex.
func mockReadyForMergeStatus(clt *mocks.MockGithubClient, prNumber int, reviewDecision githubclt.ReviewDecision, checkState githubclt.CIStatus) *mergeStatusMock {
res := &mergeStatusMock{
ReadyForMergeStatus: githubclt.ReadyForMergeStatus{
ReviewDecision: reviewDecision,
CIStatus: checkState,
ReviewDecision: reviewDecision,
CIStatus: checkState,
AutoMergeEnabled: true,
Labels: []string{mockedTriggerLabel},
Statuses: []*githubclt.CIJobStatus{{
Name: ciJobExpectedName,
Status: checkState,
Expand All @@ -163,6 +175,7 @@ func mockReadyForMergeStatus(clt *mocks.MockGithubClient, prNumber int, reviewDe

// return a copy of the object to avoid race conditions
status := res.ReadyForMergeStatus
status.Labels = slices.Clone(res.Labels)
status.Statuses = make([]*githubclt.CIJobStatus, len(res.Statuses))
for i, s := range res.Statuses {
sCopy := *s
Expand Down
16 changes: 15 additions & 1 deletion internal/mergequeue/drygithubclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,27 @@ func (c *DryGithubClient) UpdateBranch(context.Context, string, string, int) (*g
return &githubclt.UpdateBranchResult{HeadCommitID: dryGitHubClientHeadCommitID}, nil
}

func (c *DryGithubClient) ReadyForMerge(context.Context, string, string, int) (*githubclt.ReadyForMergeStatus, error) {
// ReadyForMerge simulates that the pull request is approved and all checks are
// successful.
// The information about whether the pull request is closed, has auto-merge
// enabled and its labels is retrieved from GitHub. The queue uses them to decide
// if the pull request must stay queued, simulating them would dequeue it.
func (c *DryGithubClient) ReadyForMerge(ctx context.Context, owner, repo string, prNumber int) (*githubclt.ReadyForMergeStatus, error) {
status, err := c.clt.ReadyForMerge(ctx, owner, repo, prNumber)
if err != nil {
return nil, err
}

c.logger.Info("simulated fetching ready for merge status, pr is approved, all checks successful")

return &githubclt.ReadyForMergeStatus{
ReviewDecision: githubclt.ReviewDecisionApproved,
CIStatus: githubclt.CIStatusSuccess,
Commit: dryGitHubClientHeadCommitID,

Closed: status.Closed,
AutoMergeEnabled: status.AutoMergeEnabled,
Labels: status.Labels,
}, nil
}

Expand Down
Loading
Loading