-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
194 additions
and
155 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
73 changes: 43 additions & 30 deletions
73
...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 | ||
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} | ||
} | ||
} | ||
} |
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
contribs/github-bot/comment_test.go → ...github-bot/internal/check/comment_test.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,4 +1,4 @@ | ||
package main | ||
package check | ||
|
||
import ( | ||
"context" | ||
|
Oops, something went wrong.