vault/tools/pipeline/internal/cmd/github_check_commit_status.go
Vault Automation 0a163f449e
[VAULT-40165] pipeline(github): add check go-mod-diff command (#10369) (#10377)
* [VAULT-40165] pipeline(github): add `check go-mod-diff` command

Add `pipeline github check go-mod-diff` command that is capable of
creating a Go module diff between one-or-more go.mod files in two
different Github branches. There are flags for the owner, repo, and
branch for both the A and B sides of the diff, as well as the `--path`
or `-p` flag that can be specified any number of times with relative
paths in the repository of go.mod files to compare. We assume that the
path is the same in both repositories.

This work will be followed up with another PR that removes the
enterprise only go.mod file and enables Go module diff checking on pull
requests to CE branches that change the go toolchain.

Signed-off-by: Ryan Cragun <me@ryan.ec>
Co-authored-by: Ryan Cragun <me@ryan.ec>
2025-10-27 20:03:36 +00:00

65 lines
2.7 KiB
Go

// Copyright IBM Corp. 2016, 2025
// SPDX-License-Identifier: BUSL-1.1
package cmd
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/vault/tools/pipeline/internal/pkg/github"
"github.com/spf13/cobra"
)
var githubCheckCommitStatusReq = github.CheckCommitStatusReq{}
func newGithubCheckCommitStatusCmd() *cobra.Command {
checkCommitStatusCmd := &cobra.Command{
Use: "commit-status --context <context> --creator <creator> --state <state> [--pr 1234 | --commit 1a61dbe57a7538f22f7fa3fb65900d9d41c2ba14]",
Short: "Check a commit status for a PR or commit",
Long: "Check a commit status for a specific context and creator matches an expected state. When given provided the --pr flag the HEAD sha will be used as the commit",
RunE: runGithubCheckCommitStatusCmd,
}
checkCommitStatusCmd.PersistentFlags().StringVarP(&githubCheckCommitStatusReq.Owner, "owner", "o", "hashicorp", "The Github organization")
checkCommitStatusCmd.PersistentFlags().StringVarP(&githubCheckCommitStatusReq.Repo, "repo", "r", "vault", "The Github repository. Private repositories require auth via a GITHUB_TOKEN env var")
checkCommitStatusCmd.PersistentFlags().StringVarP(&githubCheckCommitStatusReq.Commit, "commit", "c", "", "The commit you wish to list the statuses of")
checkCommitStatusCmd.PersistentFlags().IntVarP(&githubCheckCommitStatusReq.PR, "pr", "p", 0, "The Pull Request number you wish to use as the source. The HEAD commit will be used")
checkCommitStatusCmd.PersistentFlags().StringVar(&githubCheckCommitStatusReq.Context, "context", "", "The context of the status. This usually maps to the name of the check that shows up in the Pull Request status box")
checkCommitStatusCmd.PersistentFlags().StringVar(&githubCheckCommitStatusReq.Creator, "creator", "", "The github login of the creator of the status")
checkCommitStatusCmd.PersistentFlags().StringVarP(&githubCheckCommitStatusReq.State, "state", "s", "success", "The expected state of the status. Can be one of 'error', 'failure', 'pending', 'success'")
err := checkCommitStatusCmd.MarkPersistentFlagRequired("context")
if err != nil {
panic(err)
}
return checkCommitStatusCmd
}
func runGithubCheckCommitStatusCmd(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true // Don't spam the usage on failure
res, err := githubCheckCommitStatusReq.Run(context.TODO(), githubCmdState.GithubV3)
if err != nil {
return err
}
switch rootCfg.format {
case "json":
b, err := json.Marshal(res)
if err != nil {
return fmt.Errorf("marshaling response to JSON: %w", err)
}
fmt.Println(string(b))
default:
fmt.Println(res.ToTable().Render())
}
if !res.CheckSuccessful {
return fmt.Errorf("no statuses matched expected criteria: %s", res.String())
}
return nil
}