···
type InternalHandle struct {
34
-
n *notifier.Notifier
34
+
n *notifier.Notifier
35
+
res *idresolver.Resolver
func (h *InternalHandle) PushAllowed(w http.ResponseWriter, r *http.Request) {
···
124
-
if (line.NewSha.String() != line.OldSha.String()) && line.OldSha.IsZero() {
125
-
msg, err := h.replyCompare(line, repoDid, gitRelativeDir, repoName, r.Context())
127
-
l.Error("failed to reply with compare link", "err", err, "line", line, "did", gitUserDid, "repo", gitRelativeDir)
130
-
for msgLine := range msg {
131
-
resp.Messages = append(resp.Messages, msg[msgLine])
125
+
err = h.emitCompareLink(&resp.Messages, line, repoDid, repoName)
127
+
l.Error("failed to reply with compare link", "err", err, "line", line, "did", gitUserDid, "repo", gitRelativeDir)
err = h.triggerPipeline(&resp.Messages, line, gitUserDid, repoDid, repoName, pushOptions)
···
146
-
func (h *InternalHandle) replyCompare(line git.PostReceiveLine, repoOwner string, gitRelativeDir string, repoName string, ctx context.Context) ([]string, error) {
147
-
l := h.l.With("handler", "replyCompare")
148
-
userIdent, err := idresolver.DefaultResolver().ResolveIdent(ctx, repoOwner)
151
-
l.Error("Failed to fetch user identity", "err", err)
154
-
user = userIdent.Handle.String()
156
-
gr, err := git.PlainOpen(gitRelativeDir)
158
-
l.Error("Failed to open git repository", "err", err)
159
-
return []string{}, err
161
-
defaultBranch, err := gr.FindMainBranch()
163
-
l.Error("Failed to fetch default branch", "err", err)
164
-
return []string{}, err
166
-
if line.Ref == plumbing.NewBranchReferenceName(defaultBranch).String() {
167
-
return []string{}, nil
171
-
msg = append(msg, ZWS)
172
-
msg = append(msg, fmt.Sprintf("Create a PR pointing to %s", defaultBranch))
173
-
msg = append(msg, fmt.Sprintf("\t%s/%s/%s/compare/%s...%s", h.c.AppViewEndpoint, user, repoName, defaultBranch, strings.TrimPrefix(line.Ref, "refs/heads/")))
174
-
msg = append(msg, ZWS)
func (h *InternalHandle) insertRefUpdate(line git.PostReceiveLine, gitUserDid, repoDid, repoName string) error {
didSlashRepo, err := securejoin.SecureJoin(repoDid, repoName)
···
return errors.Join(errs, h.db.InsertEvent(event, h.n))
223
-
func (h *InternalHandle) triggerPipeline(clientMsgs *[]string, line git.PostReceiveLine, gitUserDid, repoDid, repoName string, pushOptions PushOptions) error {
186
+
func (h *InternalHandle) triggerPipeline(
187
+
clientMsgs *[]string,
188
+
line git.PostReceiveLine,
192
+
pushOptions PushOptions,
···
return h.db.InsertEvent(event, h.n)
288
+
func (h *InternalHandle) emitCompareLink(
289
+
clientMsgs *[]string,
290
+
line git.PostReceiveLine,
294
+
// this is a second push to a branch, don't reply with the link again
295
+
if !line.OldSha.IsZero() {
299
+
// the ref was not updated to a new hash, don't reply with the link
301
+
// NOTE: do we need this?
302
+
if line.NewSha.String() == line.OldSha.String() {
306
+
pushedRef := plumbing.ReferenceName(line.Ref)
308
+
userIdent, err := h.res.ResolveIdent(context.Background(), repoDid)
311
+
user = userIdent.Handle.String()
314
+
didSlashRepo, err := securejoin.SecureJoin(repoDid, repoName)
319
+
repoPath, err := securejoin.SecureJoin(h.c.Repo.ScanPath, didSlashRepo)
324
+
gr, err := git.PlainOpen(repoPath)
329
+
defaultBranch, err := gr.FindMainBranch()
334
+
// pushing to default branch
335
+
if pushedRef == plumbing.NewBranchReferenceName(defaultBranch) {
339
+
// pushing a tag, don't prompt the user the open a PR
340
+
if pushedRef.IsTag() {
345
+
*clientMsgs = append(*clientMsgs, ZWS)
346
+
*clientMsgs = append(*clientMsgs, fmt.Sprintf("Create a PR pointing to %s", defaultBranch))
347
+
*clientMsgs = append(*clientMsgs, fmt.Sprintf("\t%s/%s/%s/compare/%s...%s", h.c.AppViewEndpoint, user, repoName, defaultBranch, strings.TrimPrefix(line.Ref, "refs/heads/")))
348
+
*clientMsgs = append(*clientMsgs, ZWS)
func Internal(ctx context.Context, c *config.Config, db *db.DB, e *rbac.Enforcer, n *notifier.Notifier) http.Handler {
l := log.FromContext(ctx)
l = log.SubLogger(l, "internal")
356
+
res := idresolver.DefaultResolver()
···
r.Get("/push-allowed", h.PushAllowed)