diff --git a/.github/workflows/bot.yml b/.github/workflows/bot.yml index 21950459ae8..1ebe8b8f424 100644 --- a/.github/workflows/bot.yml +++ b/.github/workflows/bot.yml @@ -55,7 +55,7 @@ jobs: working-directory: contribs/github-bot env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: echo "pr-numbers=$(go run . matrix)" >> "$GITHUB_OUTPUT" + run: go run . matrix -matrix-key 'pr-numbers' -verbose # This job processes each pull request in the matrix individually while ensuring # that a same PR cannot be processed concurrently by mutliple runners diff --git a/contribs/github-bot/internal/matrix/cmd.go b/contribs/github-bot/internal/matrix/cmd.go index fb8095bae50..e1cd37a824f 100644 --- a/contribs/github-bot/internal/matrix/cmd.go +++ b/contribs/github-bot/internal/matrix/cmd.go @@ -2,11 +2,22 @@ package matrix import ( "context" + "flag" + "fmt" + "os" "github.com/gnolang/gno/tm2/pkg/commands" ) +type matrixFlags struct { + Verbose bool + MatrixKey string + flagSet *flag.FlagSet +} + func NewMatrixCmd(verbose bool) *commands.Command { + flags := &matrixFlags{Verbose: verbose} + return commands.NewCommand( commands.Metadata{ Name: "matrix", @@ -14,9 +25,29 @@ func NewMatrixCmd(verbose bool) *commands.Command { ShortHelp: "parses GitHub Actions event and defines matrix accordingly", LongHelp: "This tool retrieves the GitHub Actions context, parses the attached event, and defines the matrix with the pull request numbers to be processed accordingly", }, - commands.NewEmptyConfig(), + flags, func(_ context.Context, _ []string) error { - return execMatrix() + flags.validateFlags() + return execMatrix(flags) }, ) } + +func (flags *matrixFlags) RegisterFlags(fs *flag.FlagSet) { + fs.StringVar( + &flags.MatrixKey, + "matrix-key", + "", + "key of the matrix to set in Github Actions output (required)", + ) + + flags.flagSet = fs +} + +func (flags *matrixFlags) validateFlags() { + if flags.MatrixKey == "" { + fmt.Fprintf(flags.flagSet.Output(), "Error: no matrix-key provided\n\n") + flags.flagSet.Usage() + os.Exit(1) + } +} diff --git a/contribs/github-bot/internal/matrix/matrix.go b/contribs/github-bot/internal/matrix/matrix.go index fb5f3b876f9..3e3db4ea789 100644 --- a/contribs/github-bot/internal/matrix/matrix.go +++ b/contribs/github-bot/internal/matrix/matrix.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "os" "strings" "github.com/gnolang/gno/contribs/github-bot/internal/client" @@ -11,19 +12,24 @@ import ( "github.com/sethvargo/go-githubactions" ) -func execMatrix() error { +func execMatrix(flags *matrixFlags) error { // Get GitHub Actions context to retrieve event. actionCtx, err := githubactions.Context() if err != nil { return fmt.Errorf("unable to get GitHub Actions context: %w", err) } + // If verbose is set, print the Github Actions event for debugging purpose. + if flags.Verbose { + fmt.Println("Event:", actionCtx.Event) + } + // Init Github client using only GitHub Actions context. owner, repo := actionCtx.Repo() gh, err := client.New(context.Background(), &client.Config{ Owner: owner, Repo: repo, - Verbose: false, + Verbose: flags.Verbose, DryRun: true, }) if err != nil { @@ -36,12 +42,35 @@ func execMatrix() error { return err } - // Print PR list for GitHub Actions matrix definition. + // Format PR list for GitHub Actions matrix definition. bytes, err := prList.MarshalText() if err != nil { return fmt.Errorf("unable to marshal PR list: %w", err) } - fmt.Printf("[%s]", string(bytes)) + matrix := fmt.Sprintf("%s=[%s]", flags.MatrixKey, string(bytes)) + + // If verbose is set, print the matrix for debugging purpose. + if flags.Verbose { + fmt.Printf("Matrix: %s\n", matrix) + } + + // Get the path of the GitHub Actions environment file used for output. + output, ok := os.LookupEnv("GITHUB_OUTPUT") + if !ok { + return errors.New("unable to get GITHUB_OUTPUT var") + } + + // Open GitHub Actions output file + file, err := os.OpenFile(output, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return fmt.Errorf("unable to open GitHub Actions output file: %w", err) + } + defer file.Close() + + // Append matrix to GitHub Actions output file + if _, err := fmt.Fprintf(file, "%s\n", matrix); err != nil { + return fmt.Errorf("unable to write matrix in GitHub Actions output file: %w", err) + } return nil }