...
1 package pr
2
3 import (
4 "github.com/google/go-github/v47/github"
5
6 "edge-infra.dev/pkg/f8n/devinfra/jack/constants"
7
8 "edge-infra.dev/pkg/f8n/devinfra/jack/plugin"
9 )
10
11 func init() {
12 plugin.RegisterReviewEventHandler(constants.PluginPR, handleReviewEvent)
13 }
14
15 func handleReviewEvent(hp plugin.HandlerParams, event github.PullRequestReviewEvent) {
16 hp.Log.WithName(constants.PluginPR)
17
18 log := hp.Log
19 ctx := hp.Ctx
20 client := hp.Client
21
22
23 sender := []string{event.GetReview().GetUser().GetLogin()}
24 senderString := event.GetReview().GetUser().GetLogin()
25 log.Info("Running PR plugin")
26
27 if event.GetPullRequest().GetUser().GetLogin() == senderString {
28 log.Info("reviewer is author")
29 return
30 }
31
32
33 isDraft := event.GetPullRequest().GetDraft()
34 if isDraft {
35 log.Info("PR is draft, not assigning users")
36 return
37 }
38
39 authorAssociation := event.GetReview().GetAuthorAssociation()
40 requestedReviewers := event.GetPullRequest().RequestedReviewers
41 repo := event.GetRepo()
42 repoName := repo.GetName()
43 repoOwner := repo.GetOwner().GetLogin()
44 issueNumber := event.GetPullRequest().GetNumber()
45
46 inRequestedReviewers := false
47
48
49 if len(requestedReviewers) != 0 {
50 for _, v := range requestedReviewers {
51 if senderString == v.GetLogin() {
52 inRequestedReviewers = true
53 }
54 }
55 if !inRequestedReviewers {
56 log.Info("Reviewer is not in requested review list")
57 return
58 }
59 } else if authorAssociation != "CONTRIBUTOR" && authorAssociation != "OWNER" && authorAssociation != "COLLABORATOR" {
60 log.Info("Ignoring review from non contributor, non owner, or non collaborator")
61 return
62 }
63
64 _, _, err := client.Issues().AddAssignees(ctx, repoOwner, repoName, issueNumber, sender)
65 if err != nil {
66 log.Error(err, "Failed to add assigner")
67
68 }
69 log.Info("Added assignment from review")
70
71 }
72
View as plain text