Skip to content

Commit

Permalink
feat: add direct output and debug for matrix cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddi committed Nov 29, 2024
1 parent 02e8e7b commit 462ac01
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 33 additions & 2 deletions contribs/github-bot/internal/matrix/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,52 @@ 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",
ShortUsage: "github-bot matrix [flags]",
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)
},

Check warning on line 32 in contribs/github-bot/internal/matrix/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/matrix/cmd.go#L18-L32

Added lines #L18 - L32 were not covered by tests
)
}

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

Check warning on line 44 in contribs/github-bot/internal/matrix/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/matrix/cmd.go#L36-L44

Added lines #L36 - L44 were not covered by tests
}

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)
}

Check warning on line 52 in contribs/github-bot/internal/matrix/cmd.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/matrix/cmd.go#L47-L52

Added lines #L47 - L52 were not covered by tests
}
37 changes: 33 additions & 4 deletions contribs/github-bot/internal/matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@ import (
"context"
"errors"
"fmt"
"os"
"strings"

"github.com/gnolang/gno/contribs/github-bot/internal/client"
"github.com/gnolang/gno/contribs/github-bot/internal/utils"
"github.com/sethvargo/go-githubactions"
)

func execMatrix() error {
func execMatrix(flags *matrixFlags) error {

Check warning on line 15 in contribs/github-bot/internal/matrix/matrix.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/matrix/matrix.go#L15

Added line #L15 was not covered by tests
// 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)
}

Check warning on line 25 in contribs/github-bot/internal/matrix/matrix.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/matrix/matrix.go#L23-L25

Added lines #L23 - L25 were not covered by tests

// 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,
})

Check warning on line 34 in contribs/github-bot/internal/matrix/matrix.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/matrix/matrix.go#L29-L34

Added lines #L29 - L34 were not covered by tests
if err != nil {
Expand All @@ -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)
}

Check warning on line 55 in contribs/github-bot/internal/matrix/matrix.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/matrix/matrix.go#L50-L55

Added lines #L50 - L55 were not covered by tests

// 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")
}

Check warning on line 61 in contribs/github-bot/internal/matrix/matrix.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/matrix/matrix.go#L58-L61

Added lines #L58 - L61 were not covered by tests

// 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)
}

Check warning on line 73 in contribs/github-bot/internal/matrix/matrix.go

View check run for this annotation

Codecov / codecov/patch

contribs/github-bot/internal/matrix/matrix.go#L64-L73

Added lines #L64 - L73 were not covered by tests

return nil
}
Expand Down

0 comments on commit 462ac01

Please sign in to comment.