|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + "sync/atomic" |
| 10 | + |
| 11 | + "github.com/gnolang/gno/contribs/github-bot/internal/client" |
| 12 | + "github.com/gnolang/gno/contribs/github-bot/internal/logger" |
| 13 | + p "github.com/gnolang/gno/contribs/github-bot/internal/params" |
| 14 | + "github.com/gnolang/gno/contribs/github-bot/internal/utils" |
| 15 | + "github.com/gnolang/gno/tm2/pkg/commands" |
| 16 | + "github.com/google/go-github/v64/github" |
| 17 | + "github.com/sethvargo/go-githubactions" |
| 18 | + "github.com/xlab/treeprint" |
| 19 | +) |
| 20 | + |
| 21 | +func newCheckCmd() *commands.Command { |
| 22 | + params := &p.Params{} |
| 23 | + |
| 24 | + return commands.NewCommand( |
| 25 | + commands.Metadata{ |
| 26 | + Name: "check", |
| 27 | + ShortUsage: "github-bot check [flags]", |
| 28 | + ShortHelp: "checks requirements for a pull request to be merged", |
| 29 | + LongHelp: "This tool checks if the requirements for a pull request to be merged are satisfied (defined in config.go) and displays PR status checks accordingly.\nA valid GitHub Token must be provided by setting the GITHUB_TOKEN environment variable.", |
| 30 | + }, |
| 31 | + params, |
| 32 | + func(_ context.Context, _ []string) error { |
| 33 | + params.ValidateFlags() |
| 34 | + return execCheck(params) |
| 35 | + }, |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +func execCheck(params *p.Params) error { |
| 40 | + // Create context with timeout if specified in the parameters. |
| 41 | + ctx := context.Background() |
| 42 | + if params.Timeout > 0 { |
| 43 | + var cancel context.CancelFunc |
| 44 | + ctx, cancel = context.WithTimeout(context.Background(), params.Timeout) |
| 45 | + defer cancel() |
| 46 | + } |
| 47 | + |
| 48 | + // Init GitHub API client. |
| 49 | + gh, err := client.New(ctx, params) |
| 50 | + if err != nil { |
| 51 | + return fmt.Errorf("comment update handling failed: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + // Get GitHub Actions context to retrieve comment update. |
| 55 | + actionCtx, err := githubactions.Context() |
| 56 | + if err != nil { |
| 57 | + gh.Logger.Debugf("Unable to retrieve GitHub Actions context: %v", err) |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + // Handle comment update, if any. |
| 62 | + if err := handleCommentUpdate(gh, actionCtx); errors.Is(err, errTriggeredByBot) { |
| 63 | + return nil // Ignore if this run was triggered by a previous run. |
| 64 | + } else if err != nil { |
| 65 | + return fmt.Errorf("comment update handling failed: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + // Retrieve a slice of pull requests to process. |
| 69 | + var prs []*github.PullRequest |
| 70 | + |
| 71 | + // If requested, retrieve all open pull requests. |
| 72 | + if params.PRAll { |
| 73 | + prs, err = gh.ListPR(utils.PRStateOpen) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("unable to list all PR: %w", err) |
| 76 | + } |
| 77 | + } else { |
| 78 | + // Otherwise, retrieve only specified pull request(s) |
| 79 | + // (flag or GitHub Action context). |
| 80 | + prs = make([]*github.PullRequest, len(params.PRNums)) |
| 81 | + for i, prNum := range params.PRNums { |
| 82 | + pr, _, err := gh.Client.PullRequests.Get(gh.Ctx, gh.Owner, gh.Repo, prNum) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("unable to retrieve specified pull request (%d): %w", prNum, err) |
| 85 | + } |
| 86 | + prs[i] = pr |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return processPRList(gh, prs) |
| 91 | +} |
| 92 | + |
| 93 | +func processPRList(gh *client.GitHub, prs []*github.PullRequest) error { |
| 94 | + if len(prs) > 1 { |
| 95 | + prNums := make([]int, len(prs)) |
| 96 | + for i, pr := range prs { |
| 97 | + prNums[i] = pr.GetNumber() |
| 98 | + } |
| 99 | + |
| 100 | + gh.Logger.Infof("%d pull requests to process: %v\n", len(prNums), prNums) |
| 101 | + } |
| 102 | + |
| 103 | + // Process all pull requests in parallel. |
| 104 | + autoRules, manualRules := config(gh) |
| 105 | + var wg sync.WaitGroup |
| 106 | + |
| 107 | + // Used in dry-run mode to log cleanly from different goroutines. |
| 108 | + logMutex := sync.Mutex{} |
| 109 | + |
| 110 | + // Used in regular-run mode to return an error if one PR processing failed. |
| 111 | + var failed atomic.Bool |
| 112 | + |
| 113 | + for _, pr := range prs { |
| 114 | + wg.Add(1) |
| 115 | + go func(pr *github.PullRequest) { |
| 116 | + defer wg.Done() |
| 117 | + commentContent := CommentContent{} |
| 118 | + commentContent.allSatisfied = true |
| 119 | + |
| 120 | + // Iterate over all automatic rules in config. |
| 121 | + for _, autoRule := range autoRules { |
| 122 | + ifDetails := treeprint.NewWithRoot(fmt.Sprintf("%s Condition met", utils.Success)) |
| 123 | + |
| 124 | + // Check if conditions of this rule are met by this PR. |
| 125 | + if !autoRule.ifC.IsMet(pr, ifDetails) { |
| 126 | + continue |
| 127 | + } |
| 128 | + |
| 129 | + c := AutoContent{Description: autoRule.description, Satisfied: false} |
| 130 | + thenDetails := treeprint.NewWithRoot(fmt.Sprintf("%s Requirement not satisfied", utils.Fail)) |
| 131 | + |
| 132 | + // Check if requirements of this rule are satisfied by this PR. |
| 133 | + if autoRule.thenR.IsSatisfied(pr, thenDetails) { |
| 134 | + thenDetails.SetValue(fmt.Sprintf("%s Requirement satisfied", utils.Success)) |
| 135 | + c.Satisfied = true |
| 136 | + } else { |
| 137 | + commentContent.allSatisfied = false |
| 138 | + } |
| 139 | + |
| 140 | + c.ConditionDetails = ifDetails.String() |
| 141 | + c.RequirementDetails = thenDetails.String() |
| 142 | + commentContent.AutoRules = append(commentContent.AutoRules, c) |
| 143 | + } |
| 144 | + |
| 145 | + // Retrieve manual check states. |
| 146 | + checks := make(map[string]manualCheckDetails) |
| 147 | + if comment, err := gh.GetBotComment(pr.GetNumber()); err == nil { |
| 148 | + checks = getCommentManualChecks(comment.GetBody()) |
| 149 | + } |
| 150 | + |
| 151 | + // Iterate over all manual rules in config. |
| 152 | + for _, manualRule := range manualRules { |
| 153 | + ifDetails := treeprint.NewWithRoot(fmt.Sprintf("%s Condition met", utils.Success)) |
| 154 | + |
| 155 | + // Check if conditions of this rule are met by this PR. |
| 156 | + if !manualRule.ifC.IsMet(pr, ifDetails) { |
| 157 | + continue |
| 158 | + } |
| 159 | + |
| 160 | + // Get check status from current comment, if any. |
| 161 | + checkedBy := "" |
| 162 | + check, ok := checks[manualRule.description] |
| 163 | + if ok { |
| 164 | + checkedBy = check.checkedBy |
| 165 | + } |
| 166 | + |
| 167 | + commentContent.ManualRules = append( |
| 168 | + commentContent.ManualRules, |
| 169 | + ManualContent{ |
| 170 | + Description: manualRule.description, |
| 171 | + ConditionDetails: ifDetails.String(), |
| 172 | + CheckedBy: checkedBy, |
| 173 | + Teams: manualRule.teams, |
| 174 | + }, |
| 175 | + ) |
| 176 | + |
| 177 | + if checkedBy == "" { |
| 178 | + commentContent.allSatisfied = false |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + // Logs results or write them in bot PR comment. |
| 183 | + if gh.DryRun { |
| 184 | + logMutex.Lock() |
| 185 | + logResults(gh.Logger, pr.GetNumber(), commentContent) |
| 186 | + logMutex.Unlock() |
| 187 | + } else { |
| 188 | + if err := updatePullRequest(gh, pr, commentContent); err != nil { |
| 189 | + gh.Logger.Errorf("unable to update pull request: %v", err) |
| 190 | + failed.Store(true) |
| 191 | + } |
| 192 | + } |
| 193 | + }(pr) |
| 194 | + } |
| 195 | + wg.Wait() |
| 196 | + |
| 197 | + if failed.Load() { |
| 198 | + return errors.New("error occurred while processing pull requests") |
| 199 | + } |
| 200 | + |
| 201 | + return nil |
| 202 | +} |
| 203 | + |
| 204 | +// logResults is called in dry-run mode and outputs the status of each check |
| 205 | +// and a conclusion. |
| 206 | +func logResults(logger logger.Logger, prNum int, commentContent CommentContent) { |
| 207 | + logger.Infof("Pull request #%d requirements", prNum) |
| 208 | + if len(commentContent.AutoRules) > 0 { |
| 209 | + logger.Infof("Automated Checks:") |
| 210 | + } |
| 211 | + |
| 212 | + for _, rule := range commentContent.AutoRules { |
| 213 | + status := utils.Fail |
| 214 | + if rule.Satisfied { |
| 215 | + status = utils.Success |
| 216 | + } |
| 217 | + logger.Infof("%s %s", status, rule.Description) |
| 218 | + logger.Debugf("If:\n%s", rule.ConditionDetails) |
| 219 | + logger.Debugf("Then:\n%s", rule.RequirementDetails) |
| 220 | + } |
| 221 | + |
| 222 | + if len(commentContent.ManualRules) > 0 { |
| 223 | + logger.Infof("Manual Checks:") |
| 224 | + } |
| 225 | + |
| 226 | + for _, rule := range commentContent.ManualRules { |
| 227 | + status := utils.Fail |
| 228 | + checker := "any user with comment edit permission" |
| 229 | + if rule.CheckedBy != "" { |
| 230 | + status = utils.Success |
| 231 | + } |
| 232 | + if len(rule.Teams) == 0 { |
| 233 | + checker = fmt.Sprintf("a member of one of these teams: %s", strings.Join(rule.Teams, ", ")) |
| 234 | + } |
| 235 | + logger.Infof("%s %s", status, rule.Description) |
| 236 | + logger.Debugf("If:\n%s", rule.ConditionDetails) |
| 237 | + logger.Debugf("Can be checked by %s", checker) |
| 238 | + } |
| 239 | + |
| 240 | + logger.Infof("Conclusion:") |
| 241 | + if commentContent.allSatisfied { |
| 242 | + logger.Infof("%s All requirements are satisfied\n", utils.Success) |
| 243 | + } else { |
| 244 | + logger.Infof("%s Not all requirements are satisfied\n", utils.Fail) |
| 245 | + } |
| 246 | +} |
0 commit comments