Skip to content
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
37 changes: 37 additions & 0 deletions extension/landprovider/github/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
load("@rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "github",
srcs = [
"land.go",
"merge.go",
"validate.go",
],
importpath = "github.com/uber/submitqueue/extension/landprovider/github",
visibility = ["//visibility:public"],
deps = [
"//entity",
"//entity/github",
"//extension/landprovider",
"@com_github_uber_go_tally_v4//:tally",
"@org_uber_go_zap//:zap",
],
)

go_test(
name = "github_test",
srcs = [
"land_test.go",
"merge_test.go",
"validate_test.go",
],
embed = [":github"],
deps = [
"//entity",
"//extension/landprovider",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
"@com_github_uber_go_tally_v4//:tally",
"@org_uber_go_zap//zaptest",
],
)
94 changes: 94 additions & 0 deletions extension/landprovider/github/land.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package github

import (
"context"
"fmt"
"net/http"

"github.com/uber-go/tally/v4"
"github.com/uber/submitqueue/entity"
entitygithub "github.com/uber/submitqueue/entity/github"
"github.com/uber/submitqueue/extension/landprovider"
"go.uber.org/zap"
)

// Params holds the dependencies for the GitHub LandProvider.
type Params struct {
// HTTPClient is a pre-configured HTTP client with auth (bearer token, GitHub App JWT, etc.).
// Auth is the caller's responsibility via HTTP transport/round-tripper.
HTTPClient *http.Client
// APIURL is the GitHub REST API base URL
// (e.g., "https://api.github.com" or "https://ghe.example.com/api/v3").
APIURL string
Comment on lines +21 to +22
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need it in the input? can this be encapsulated in httpClient?

// Logger is the structured logger.
Logger *zap.SugaredLogger
// MetricsScope is the metrics scope for instrumentation.
MetricsScope tally.Scope
}

// landProvider implements the landprovider.LandProvider interface using the GitHub REST API.
//
// Limitation: this implementation supports landing exactly one PR per call.
// Landing multiple PRs in a single call is not idempotent — if PR N merges
// successfully but PR N+1 fails, a retry would fail on the already-merged PR.
// Callers needing multi-PR landing should use an implementation that provides
// atomic or idempotent batch semantics.
type landProvider struct {
httpClient *http.Client
apiURL string
logger *zap.SugaredLogger
metricsScope tally.Scope
}

// Verify landProvider implements landprovider.LandProvider at compile time.
var _ landprovider.LandProvider = (*landProvider)(nil)

// NewLandProvider creates a new GitHub LandProvider.
func NewLandProvider(params Params) landprovider.LandProvider {
return &landProvider{
httpClient: params.HTTPClient,
apiURL: params.APIURL,
logger: params.Logger.Named("github_landprovider"),
metricsScope: params.MetricsScope.SubScope("github_landprovider"),
}
}

// Land merges a single PR into the target branch using the GitHub merge PR REST API.
// Returns an error if entries contain more than one PR, since merging multiple PRs
// is not idempotent — a partial failure leaves already-merged PRs in a state that
// cannot be retried.
//
// For single-PR landing, idempotency is ensured by checking if the PR is already
// merged before attempting the merge. Returns ErrAlreadyLanded if so.
func (l *landProvider) Land(ctx context.Context, queue string, entries []entity.LandEntry) error {
l.metricsScope.Counter("land_started").Inc(1)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you update to use the core/metrics pkg?


if err := validateSinglePR(entries); err != nil {
l.metricsScope.Counter("validation_errors").Inc(1)
return err
}

entry := entries[0]
uri := entry.Change.URIs[0]

cid, err := entitygithub.ParseChangeID(uri)
if err != nil {
l.metricsScope.Counter("parse_errors").Inc(1)
return fmt.Errorf("failed to parse change ID %q: %w", uri, err)
}

if err := l.mergePR(ctx, cid, entry.Strategy); err != nil {
switch {
case landprovider.IsAlreadyLanded(err):
l.metricsScope.Counter("already_landed").Inc(1)
case landprovider.IsLandRejected(err):
l.metricsScope.Counter("land_rejected").Inc(1)
default:
l.metricsScope.Counter("api_errors").Inc(1)
}
return err
}

l.metricsScope.Counter("land_succeeded").Inc(1)
return nil
}
162 changes: 162 additions & 0 deletions extension/landprovider/github/land_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package github

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber-go/tally/v4"
"github.com/uber/submitqueue/entity"
"github.com/uber/submitqueue/extension/landprovider"
"go.uber.org/zap/zaptest"
)

func newTestLandProvider(t *testing.T, serverURL string) landprovider.LandProvider {
logger := zaptest.NewLogger(t).Sugar()
scope := tally.NoopScope

return NewLandProvider(Params{
HTTPClient: &http.Client{},
APIURL: serverURL,
Logger: logger,
MetricsScope: scope,
})
}

func mergeHandler(t *testing.T, statusCode int, message string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
t.Helper()

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
resp := mergeResponse{Message: message}
err := json.NewEncoder(w).Encode(resp)
require.NoError(t, err)
}
}

// prMergedHandler returns a handler for GET /repos/{owner}/{repo}/pulls/{number}/merge.
// Returns 204 if merged, 404 if not (empty body, matching the GitHub API).
func prMergedHandler(merged bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if merged {
w.WriteHeader(http.StatusNoContent)
} else {
w.WriteHeader(http.StatusNotFound)
}
}
}

// routingHandler routes PUT (merge) and GET (PR state) requests to separate handlers.
func routingHandler(putHandler, getHandler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPut:
putHandler(w, r)
case http.MethodGet:
getHandler(w, r)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
}

func TestLandProvider_Land(t *testing.T) {
singleEntry := func(uri string) []entity.LandEntry {
return []entity.LandEntry{{
Strategy: entity.RequestLandStrategyRebase,
Change: entity.Change{URIs: []string{uri}},
}}
}

tests := []struct {
name string
handler http.HandlerFunc
entries []entity.LandEntry
wantErr bool
rejected bool
alreadyLanded bool
}{
{
// isPRMerged → not merged → mergePR → 200 OK
name: "success",
handler: routingHandler(mergeHandler(t, http.StatusOK, "Pull Request successfully merged"), prMergedHandler(false)),
entries: singleEntry("github://uber/repo/1/abc123"),
},
{
// Land → ParseChangeID fails (no server call)
name: "invalid change ID",
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Fail(t, "should not reach server")
}),
entries: singleEntry("invalid-id"),
wantErr: true,
},
{
// isPRMerged → not merged → mergePR → 409 → WrapLandRejected
name: "land rejected",
handler: routingHandler(mergeHandler(t, http.StatusConflict, "Head branch was modified"), prMergedHandler(false)),
entries: singleEntry("github://uber/repo/1/abc123"),
wantErr: true,
rejected: true,
},
{
// isPRMerged → already merged → ErrAlreadyLanded (no merge attempt)
name: "already merged - idempotent retry",
handler: routingHandler(mergeHandler(t, http.StatusOK, "should not be called"), prMergedHandler(true)),
entries: singleEntry("github://uber/repo/1/abc123"),
wantErr: true,
alreadyLanded: true,
},
{
// isPRMerged → not merged → mergePR → 500 → plain error
name: "server error",
handler: routingHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("internal server error"))
}),
prMergedHandler(false),
),
entries: singleEntry("github://uber/repo/1/abc123"),
wantErr: true,
},
{
// isPRMerged → unexpected status code (500) → error propagated
name: "merge status check error",
handler: routingHandler(
mergeHandler(t, http.StatusOK, "should not be called"),
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}),
),
entries: singleEntry("github://uber/repo/1/abc123"),
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(tt.handler)
defer server.Close()

lp := newTestLandProvider(t, server.URL)
err := lp.Land(context.Background(), "test-queue", tt.entries)
if tt.wantErr {
require.Error(t, err)
if tt.rejected {
assert.True(t, landprovider.IsLandRejected(err))
}
if tt.alreadyLanded {
assert.True(t, landprovider.IsAlreadyLanded(err))
}
return
}
require.NoError(t, err)
})
}
}
Loading