-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
126 lines (110 loc) · 2.85 KB
/
Copy pathhandler.go
File metadata and controls
126 lines (110 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/google/go-github/v85/github"
"github.com/palantir/go-githubapp/githubapp"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type PRHandler struct {
githubapp.ClientCreator
cfg *Config
}
func (h *PRHandler) Handles() []string {
return []string{"pull_request"}
}
func (h *PRHandler) Handle(ctx context.Context, eventType, deliveryID string, payload []byte) error {
var event github.PullRequestEvent
if err := json.Unmarshal(payload, &event); err != nil {
return err
}
action := event.GetAction()
if action != "opened" && action != "synchronize" && action != "reopened" {
return nil
}
repo := event.GetRepo()
pr := event.GetPullRequest()
owner := repo.GetOwner().GetLogin()
repoName := repo.GetName()
prNum := pr.GetNumber()
logger := log.With().
Str("repo", fmt.Sprintf("%s/%s", owner, repoName)).
Int("pr", prNum).
Logger()
client, err := h.NewInstallationClient(event.GetInstallation().GetID())
if err != nil {
return err
}
aiAuthor, err := h.findaiCoAuthor(ctx, client, owner, repoName, prNum, logger)
if err != nil {
return err
}
if aiAuthor == "" {
return nil
}
logger.Info().Str("co_author", aiAuthor).Msg("ai co-author detected, declining pr")
return h.declinePR(ctx, client, owner, repoName, prNum, aiAuthor)
}
func (h *PRHandler) findaiCoAuthor(
ctx context.Context,
client *github.Client,
owner, repo string,
prNum int,
logger zerolog.Logger,
) (string, error) {
opts := &github.ListOptions{PerPage: 100}
for {
commits, resp, err := client.PullRequests.ListCommits(ctx, owner, repo, prNum, opts)
if err != nil {
return "", err
}
for _, c := range commits {
msg := c.GetCommit().GetMessage()
for _, line := range strings.Split(msg, "\n") {
line = strings.TrimSpace(line)
if !strings.HasPrefix(strings.ToLower(line), "co-authored-by:") {
continue
}
coAuthor := strings.TrimSpace(line[len("co-authored-by:"):])
if matchesAI(coAuthor, h.cfg.ExtraPatterns) {
logger.Debug().
Str("commit", c.GetSHA()).
Str("co_author", coAuthor).
Msg("matched ai co-author")
return coAuthor, nil
}
}
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return "", nil
}
func (h *PRHandler) declinePR(
ctx context.Context,
client *github.Client,
owner, repo string,
prNum int,
SlopGeneratorAuthor string,
) error {
body := h.cfg.DeclineMessage + fmt.Sprintf("\n\n**detected:** `%s`", SlopGeneratorAuthor)
_, _, err := client.Issues.CreateComment(ctx, owner, repo, prNum, &github.IssueComment{
Body: &body,
})
if err != nil {
return fmt.Errorf("post comment: %w", err)
}
state := "closed"
_, _, err = client.PullRequests.Edit(ctx, owner, repo, prNum, &github.PullRequest{
State: &state,
})
if err != nil {
return fmt.Errorf("close pr: %w", err)
}
return nil
}