|
| 1 | +// Copyright (c) 2025 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 git provides a changeprovider.ChangeProvider that reads change |
| 16 | +// metadata out of a git repository, for a remote that offers no API to ask. |
| 17 | +// |
| 18 | +// Where the GitHub and Phabricator providers query a service that already knows |
| 19 | +// what a change contains, this one derives it: it keeps its own copy of the |
| 20 | +// remote and computes each change's files, line counts and author from the |
| 21 | +// commits themselves. That makes a plain git remote — an internal host, a |
| 22 | +// mirror, a bare repository on disk — a first-class source of change metadata |
| 23 | +// with no service in front of it. |
| 24 | +// |
| 25 | +// # What a change is measured against |
| 26 | +// |
| 27 | +// A git:// change URI names a commit and the ref it lives on, and nothing else. |
| 28 | +// Unlike a pull request it carries no base, so the baseline has to be derived, |
| 29 | +// and for a stack it cannot be the target branch: a stack's changes are cut one |
| 30 | +// from the next, so measuring each against the target would report the second |
| 31 | +// change as containing the first as well. Each change is therefore measured |
| 32 | +// from where it diverged from the change before it, and only the first from the |
| 33 | +// target. Callers get per-change numbers that sum, which is what any consumer |
| 34 | +// aggregating over a batch depends on. |
| 35 | +package git |
| 36 | + |
| 37 | +import ( |
| 38 | + "context" |
| 39 | + "fmt" |
| 40 | + |
| 41 | + "github.com/uber-go/tally" |
| 42 | + "go.uber.org/zap" |
| 43 | + |
| 44 | + changegit "github.com/uber/submitqueue/platform/base/change/git" |
| 45 | + coremetrics "github.com/uber/submitqueue/platform/metrics" |
| 46 | + "github.com/uber/submitqueue/submitqueue/entity" |
| 47 | + "github.com/uber/submitqueue/submitqueue/extension/changeprovider" |
| 48 | +) |
| 49 | + |
| 50 | +const opName = "git_changeprovider" |
| 51 | + |
| 52 | +// Params carries what a provider needs. The Repo is built once per repository |
| 53 | +// and shared by every queue reading it. |
| 54 | +type Params struct { |
| 55 | + Config changeprovider.Config |
| 56 | + Repo *Repo |
| 57 | + Logger *zap.SugaredLogger |
| 58 | + MetricsScope tally.Scope |
| 59 | +} |
| 60 | + |
| 61 | +// provider reads change metadata from a local copy of a git remote. |
| 62 | +type provider struct { |
| 63 | + cfg changeprovider.Config |
| 64 | + repo *Repo |
| 65 | + logger *zap.SugaredLogger |
| 66 | + metricsScope tally.Scope |
| 67 | +} |
| 68 | + |
| 69 | +// New returns a changeprovider.ChangeProvider reading from repo. |
| 70 | +func New(params Params) changeprovider.ChangeProvider { |
| 71 | + return &provider{ |
| 72 | + cfg: params.Config, |
| 73 | + repo: params.Repo, |
| 74 | + logger: params.Logger.Named(opName), |
| 75 | + metricsScope: params.MetricsScope.SubScope(opName), |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Get returns one ChangeInfo per URI, in the order the URIs were given. |
| 80 | +// |
| 81 | +// The order is load-bearing: it is the stack order, and each change after the |
| 82 | +// first is measured from the one before it. |
| 83 | +func (p *provider) Get(ctx context.Context, request entity.Request) (_ []entity.ChangeInfo, retErr error) { |
| 84 | + op := coremetrics.Begin(p.metricsScope, "get", coremetrics.LongLatencyBuckets) |
| 85 | + defer func() { op.Complete(retErr) }() |
| 86 | + |
| 87 | + uris := request.Change.URIs |
| 88 | + infos := make([]entity.ChangeInfo, 0, len(uris)) |
| 89 | + |
| 90 | + p.repo.mu.Lock() |
| 91 | + defer p.repo.mu.Unlock() |
| 92 | + |
| 93 | + if err := p.repo.fetchTarget(ctx); err != nil { |
| 94 | + coremetrics.NamedCounter(p.metricsScope, "get", "fetch_errors", 1) |
| 95 | + return nil, fmt.Errorf("failed to update target branch %s: %w", p.repo.cfg.Target, err) |
| 96 | + } |
| 97 | + |
| 98 | + previous := "" |
| 99 | + for _, uri := range uris { |
| 100 | + id, err := changegit.ParseChangeID(uri) |
| 101 | + if err != nil { |
| 102 | + return nil, fmt.Errorf("failed to parse change URI: %w", err) |
| 103 | + } |
| 104 | + if err := p.repo.ensureCommit(ctx, id.CommitSHA, id.Ref); err != nil { |
| 105 | + coremetrics.NamedCounter(p.metricsScope, "get", "commit_unavailable", 1) |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + |
| 109 | + // The first change stands on the target; each one after it stands on the |
| 110 | + // change before it. |
| 111 | + against := p.repo.cfg.Remote + "/" + p.repo.cfg.Target |
| 112 | + if previous != "" { |
| 113 | + against = previous |
| 114 | + } |
| 115 | + |
| 116 | + details, err := p.describe(ctx, against, id.CommitSHA) |
| 117 | + if err != nil { |
| 118 | + return nil, fmt.Errorf("failed to describe change %s: %w", uri, err) |
| 119 | + } |
| 120 | + |
| 121 | + infos = append(infos, entity.ChangeInfo{URI: uri, Details: details}) |
| 122 | + previous = id.CommitSHA |
| 123 | + } |
| 124 | + return infos, nil |
| 125 | +} |
| 126 | + |
| 127 | +// describe reports what sha changed relative to where it diverged from against. |
| 128 | +func (p *provider) describe(ctx context.Context, against, sha string) (entity.ChangeDetails, error) { |
| 129 | + base, err := p.repo.mergeBase(ctx, against, sha) |
| 130 | + if err != nil { |
| 131 | + return entity.ChangeDetails{}, err |
| 132 | + } |
| 133 | + |
| 134 | + // -M so a rename reads as one moved file rather than a whole file deleted |
| 135 | + // and another added; the scrubbed environment leaves git's own default off. |
| 136 | + raw, err := p.repo.output(ctx, "diff", "--numstat", "-M", "-z", base, sha) |
| 137 | + if err != nil { |
| 138 | + return entity.ChangeDetails{}, err |
| 139 | + } |
| 140 | + files, err := parseNumstat(raw) |
| 141 | + if err != nil { |
| 142 | + return entity.ChangeDetails{}, err |
| 143 | + } |
| 144 | + |
| 145 | + author, err := p.author(ctx, sha) |
| 146 | + if err != nil { |
| 147 | + return entity.ChangeDetails{}, err |
| 148 | + } |
| 149 | + return entity.ChangeDetails{Author: author, ChangedFiles: files}, nil |
| 150 | +} |
| 151 | + |
| 152 | +// author reads the commit's author, NUL-separated because a display name can |
| 153 | +// contain anything a friendlier separator would collide with. |
| 154 | +func (p *provider) author(ctx context.Context, sha string) (entity.Author, error) { |
| 155 | + out, err := p.repo.output(ctx, "show", "--no-patch", "--format=%an%x00%ae", sha) |
| 156 | + if err != nil { |
| 157 | + return entity.Author{}, err |
| 158 | + } |
| 159 | + name, email, found := cut(out) |
| 160 | + if !found { |
| 161 | + return entity.Author{}, fmt.Errorf("unreadable author for commit %s", sha) |
| 162 | + } |
| 163 | + return entity.Author{Name: name, Email: email}, nil |
| 164 | +} |
| 165 | + |
| 166 | +// cut splits the author format's two fields, trimming the newline git appends. |
| 167 | +func cut(out string) (name, email string, found bool) { |
| 168 | + for i := 0; i < len(out); i++ { |
| 169 | + if out[i] == 0 { |
| 170 | + return out[:i], trimNewline(out[i+1:]), true |
| 171 | + } |
| 172 | + } |
| 173 | + return "", "", false |
| 174 | +} |
| 175 | + |
| 176 | +func trimNewline(s string) string { |
| 177 | + for len(s) > 0 && (s[len(s)-1] == '\n' || s[len(s)-1] == '\r') { |
| 178 | + s = s[:len(s)-1] |
| 179 | + } |
| 180 | + return s |
| 181 | +} |
0 commit comments