forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: add debug on github-bot matrix subcommand + fixes (gnolang#3244)
This PR will allow debugging errors of [this type](https://github.com/gnolang/gno/actions/runs/12072757244) that unfortunately cannot be tested locally since they rely on the context of GitHub Actions. Since I also had to add flags to the matrix subcommand, I moved the two matrix and check subcommands into subfolders. This PR also modify the comment to stick to moul's request and fixes several Github Actions errors. Related to gnolang#3238 Changes: - gnolang@d11ad5a moves matrix and check subcommands to their own packages in internal - gnolang@462ac01 gnolang@5c1edda gnolang@ffdce93 adds a debug to matrix subcommand (print event input / matrix output) + direct output of matrix to GitHub Actions using a matrix-key flag - gnolang@6af501d embed comment template file as a string at compile time instead of opening it at runtime - gnolang@59c3ad6 modifies bot comment to meet [this requirements](gnolang#3238 (comment)) - gnolang@241a755 filter out from the matrix generation and the PR processing all issues or closed PRs (process / list only opened PRs) <details><summary>Contributors' checklist...</summary> - [x] Added new tests, or not needed, or not feasible - [x] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [x] Updated the official documentation or not needed - [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [x] Added references to related issues and PRs - [x] Provided any useful hints for running manual tests </details> --------- Co-authored-by: Morgan <[email protected]>
- Loading branch information
Showing
21 changed files
with
490 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 44 additions & 31 deletions
75
...ribs/github-bot/internal/params/params.go → contribs/github-bot/internal/check/cmd.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,131 @@ | ||
package params | ||
package check | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/gnolang/gno/contribs/github-bot/internal/utils" | ||
"github.com/gnolang/gno/tm2/pkg/commands" | ||
"github.com/sethvargo/go-githubactions" | ||
) | ||
|
||
type Params struct { | ||
type checkFlags struct { | ||
Owner string | ||
Repo string | ||
PRAll bool | ||
PRNums PRList | ||
Verbose bool | ||
PRNums utils.PRList | ||
Verbose *bool | ||
DryRun bool | ||
Timeout time.Duration | ||
flagSet *flag.FlagSet | ||
} | ||
|
||
func (p *Params) RegisterFlags(fs *flag.FlagSet) { | ||
func NewCheckCmd(verbose *bool) *commands.Command { | ||
flags := &checkFlags{Verbose: verbose} | ||
|
||
return commands.NewCommand( | ||
commands.Metadata{ | ||
Name: "check", | ||
ShortUsage: "github-bot check [flags]", | ||
ShortHelp: "checks requirements for a pull request to be merged", | ||
LongHelp: "This tool checks if the requirements for a pull request to be merged are satisfied (defined in ./internal/config/config.go) and displays PR status checks accordingly.\nA valid GitHub Token must be provided by setting the GITHUB_TOKEN environment variable.", | ||
}, | ||
flags, | ||
func(_ context.Context, _ []string) error { | ||
flags.validateFlags() | ||
return execCheck(flags) | ||
}, | ||
) | ||
} | ||
|
||
func (flags *checkFlags) RegisterFlags(fs *flag.FlagSet) { | ||
fs.StringVar( | ||
&p.Owner, | ||
&flags.Owner, | ||
"owner", | ||
"", | ||
"owner of the repo to process, if empty, will be retrieved from GitHub Actions context", | ||
) | ||
|
||
fs.StringVar( | ||
&p.Repo, | ||
&flags.Repo, | ||
"repo", | ||
"", | ||
"repo to process, if empty, will be retrieved from GitHub Actions context", | ||
) | ||
|
||
fs.BoolVar( | ||
&p.PRAll, | ||
&flags.PRAll, | ||
"pr-all", | ||
false, | ||
"process all opened pull requests", | ||
) | ||
|
||
fs.TextVar( | ||
&p.PRNums, | ||
&flags.PRNums, | ||
"pr-numbers", | ||
PRList(nil), | ||
utils.PRList(nil), | ||
"pull request(s) to process, must be a comma separated list of PR numbers, e.g '42,1337,7890'. If empty, will be retrieved from GitHub Actions context", | ||
) | ||
|
||
fs.BoolVar( | ||
&p.Verbose, | ||
"verbose", | ||
false, | ||
"set logging level to debug", | ||
) | ||
|
||
fs.BoolVar( | ||
&p.DryRun, | ||
&flags.DryRun, | ||
"dry-run", | ||
false, | ||
"print if pull request requirements are satisfied without updating anything on GitHub", | ||
) | ||
|
||
fs.DurationVar( | ||
&p.Timeout, | ||
&flags.Timeout, | ||
"timeout", | ||
0, | ||
"timeout after which the bot execution is interrupted", | ||
) | ||
|
||
p.flagSet = fs | ||
flags.flagSet = fs | ||
} | ||
|
||
func (p *Params) ValidateFlags() { | ||
func (flags *checkFlags) validateFlags() { | ||
// Helper to display an error + usage message before exiting. | ||
errorUsage := func(err string) { | ||
fmt.Fprintf(p.flagSet.Output(), "Error: %s\n\n", err) | ||
p.flagSet.Usage() | ||
fmt.Fprintf(flags.flagSet.Output(), "Error: %s\n\n", err) | ||
flags.flagSet.Usage() | ||
os.Exit(1) | ||
} | ||
|
||
// Check if flags are coherent. | ||
if p.PRAll && len(p.PRNums) != 0 { | ||
if flags.PRAll && len(flags.PRNums) != 0 { | ||
errorUsage("You can specify only one of the '-pr-all' and '-pr-numbers' flags.") | ||
} | ||
|
||
// If one of these values is empty, it must be retrieved | ||
// from GitHub Actions context. | ||
if p.Owner == "" || p.Repo == "" || (len(p.PRNums) == 0 && !p.PRAll) { | ||
if flags.Owner == "" || flags.Repo == "" || (len(flags.PRNums) == 0 && !flags.PRAll) { | ||
actionCtx, err := githubactions.Context() | ||
if err != nil { | ||
errorUsage(fmt.Sprintf("Unable to get GitHub Actions context: %v.", err)) | ||
} | ||
|
||
if p.Owner == "" { | ||
if p.Owner, _ = actionCtx.Repo(); p.Owner == "" { | ||
if flags.Owner == "" { | ||
if flags.Owner, _ = actionCtx.Repo(); flags.Owner == "" { | ||
errorUsage("Unable to retrieve owner from GitHub Actions context, you may want to set it using -onwer flag.") | ||
} | ||
} | ||
if p.Repo == "" { | ||
if _, p.Repo = actionCtx.Repo(); p.Repo == "" { | ||
if flags.Repo == "" { | ||
if _, flags.Repo = actionCtx.Repo(); flags.Repo == "" { | ||
errorUsage("Unable to retrieve repo from GitHub Actions context, you may want to set it using -repo flag.") | ||
} | ||
} | ||
|
||
if len(p.PRNums) == 0 && !p.PRAll { | ||
if len(flags.PRNums) == 0 && !flags.PRAll { | ||
prNum, err := utils.GetPRNumFromActionsCtx(actionCtx) | ||
if err != nil { | ||
errorUsage(fmt.Sprintf("Unable to retrieve pull request number from GitHub Actions context: %s\nYou may want to set it using -pr-numbers flag.", err.Error())) | ||
} | ||
|
||
p.PRNums = PRList{prNum} | ||
flags.PRNums = utils.PRList{prNum} | ||
} | ||
} | ||
} |
Oops, something went wrong.