|
| 1 | +// Copyright (c) 2026 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package github exercises the GitHub validator against a real repository on |
| 16 | +// github.com, the one place its GraphQL query and its reading of the response |
| 17 | +// meet the real API rather than a fake. The test repository is the one the |
| 18 | +// merger lands pull requests in without approvals or required checks, so a |
| 19 | +// fresh pull request there must pass. |
| 20 | +// |
| 21 | +// It skips unless a test repository is configured; see test/testutil/githubtestrepo. |
| 22 | +package github |
| 23 | + |
| 24 | +import ( |
| 25 | + "context" |
| 26 | + "fmt" |
| 27 | + "testing" |
| 28 | + |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | + "github.com/stretchr/testify/require" |
| 31 | + "github.com/uber-go/tally" |
| 32 | + "go.uber.org/zap/zaptest" |
| 33 | + |
| 34 | + "github.com/uber/submitqueue/platform/base/change" |
| 35 | + "github.com/uber/submitqueue/submitqueue/entity" |
| 36 | + "github.com/uber/submitqueue/submitqueue/extension/validator" |
| 37 | + githubvalidator "github.com/uber/submitqueue/submitqueue/extension/validator/github" |
| 38 | + "github.com/uber/submitqueue/test/testutil/githubtestrepo" |
| 39 | +) |
| 40 | + |
| 41 | +func newValidator(t *testing.T, repo *githubtestrepo.TestRepo) validator.Validator { |
| 42 | + t.Helper() |
| 43 | + v, err := githubvalidator.New(githubvalidator.Params{ |
| 44 | + Config: validator.Config{QueueName: "sq-it"}, |
| 45 | + HTTPClient: repo.HTTPClient(), |
| 46 | + Host: githubtestrepo.Host, |
| 47 | + Logger: zaptest.NewLogger(t).Sugar(), |
| 48 | + MetricsScope: tally.NoopScope, |
| 49 | + }) |
| 50 | + require.NoError(t, err) |
| 51 | + return v |
| 52 | +} |
| 53 | + |
| 54 | +func landRequest(repo *githubtestrepo.TestRepo, uris ...string) entity.Request { |
| 55 | + return entity.Request{ID: "sq-it/" + repo.RunID(), Queue: "sq-it", Change: change.Change{URIs: uris}} |
| 56 | +} |
| 57 | + |
| 58 | +func TestGitHubValidator(t *testing.T) { |
| 59 | + ctx := context.Background() |
| 60 | + |
| 61 | + t.Run("open pull request passes", func(t *testing.T) { |
| 62 | + repo := githubtestrepo.New(t) |
| 63 | + v := newValidator(t, repo) |
| 64 | + p := repo.OpenPull("pass", repo.Trunk()) |
| 65 | + |
| 66 | + assert.NoError(t, v.Validate(ctx, landRequest(repo, repo.URI(p)))) |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("stack passes", func(t *testing.T) { |
| 70 | + repo := githubtestrepo.New(t) |
| 71 | + v := newValidator(t, repo) |
| 72 | + pulls := repo.OpenStack("stack", 2) |
| 73 | + |
| 74 | + assert.NoError(t, v.Validate(ctx, landRequest(repo, repo.URIs(pulls...)...))) |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("pull request whose head moved is rejected", func(t *testing.T) { |
| 78 | + repo := githubtestrepo.New(t) |
| 79 | + v := newValidator(t, repo) |
| 80 | + p := repo.OpenPull("stale", repo.Trunk()) |
| 81 | + req := landRequest(repo, repo.URI(p)) |
| 82 | + // Passing first pins the failure below on the moved head: the query |
| 83 | + // reads this pull request fine, and nothing else changes in between. |
| 84 | + require.NoError(t, v.Validate(ctx, req)) |
| 85 | + repo.AwaitPullHead(p.Number, repo.CommitFile(p.Branch, repo.FilePath("stale-followup"), "followup")) |
| 86 | + |
| 87 | + require.Error(t, v.Validate(ctx, req)) |
| 88 | + }) |
| 89 | + |
| 90 | + t.Run("pull request that does not exist fails", func(t *testing.T) { |
| 91 | + repo := githubtestrepo.New(t) |
| 92 | + v := newValidator(t, repo) |
| 93 | + p := repo.OpenPull("missing", repo.Trunk()) |
| 94 | + missing := fmt.Sprintf("github://%s/%s/%s/pull/%d/%s", githubtestrepo.Host, repo.Owner(), repo.Repo(), 1<<30, p.Head) |
| 95 | + |
| 96 | + require.Error(t, v.Validate(ctx, landRequest(repo, missing))) |
| 97 | + }) |
| 98 | +} |
0 commit comments