-
Notifications
You must be signed in to change notification settings - Fork 0
feat(landprovider): basic github implementation #109
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| // 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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?