nitter/internal/commands/gitea.go
Peter Kurfer d51a05fea1
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
test(gitea): delete previously created reviews
2023-03-09 14:39:55 +01:00

162 lines
4.3 KiB
Go

package commands
import (
"fmt"
"strings"
giteaSdk "code.gitea.io/sdk/gitea"
"github.com/spf13/cobra"
"code.icb4dc0.de/prskr/nitter/nitters/gitea"
)
type reviewStateTypeValue giteaSdk.ReviewStateType
func (r *reviewStateTypeValue) String() string {
return string(*r)
}
func (r *reviewStateTypeValue) Set(s string) error {
switch strings.ToUpper(s) {
case string(giteaSdk.ReviewStateApproved):
*r = reviewStateTypeValue(giteaSdk.ReviewStateApproved)
return nil
case string(giteaSdk.ReviewStateRequestChanges):
*r = reviewStateTypeValue(giteaSdk.ReviewStateRequestChanges)
return nil
case string(giteaSdk.ReviewStateComment):
*r = reviewStateTypeValue(giteaSdk.ReviewStateComment)
return nil
default:
return fmt.Errorf("value %s doesn't match any expected state type", s)
}
}
func (r *reviewStateTypeValue) Type() string {
return "string"
}
var errorReviewStateType = reviewStateTypeValue(giteaSdk.ReviewStateComment)
//nolint:lll //flag setup causes long lines
func Gitea() *cobra.Command {
giteaCmd := &cobra.Command{
Use: "gitea",
Short: "Parent command to Gitea related actions",
}
giteaCmd.PersistentFlags().StringP("base-address", "a", "", "Base URL of your Gitea/Forgejo instance [$NITTER_BASE_ADDRESS]")
giteaCmd.PersistentFlags().StringP("token", "t", "", "Access token to interact with Gitea/Forgejo API [$NITTER_TOKEN]")
pr := &cobra.Command{
Use: "pull-request",
Short: "Read golangci-lint result and create a PR review",
Aliases: []string{"merge-request", "pr", "pull"},
RunE: runGiteaPRNitting,
}
pr.Flags().Int64P(
"pull-index",
"i",
-1,
"PR index to add reviews to - note, this is not the ID of the PR but its number [$NITTER_PULL_INDEX]",
)
pr.Flags().Var(&errorReviewStateType, "review-state", "state in which to create the review if there are issues [comment,approved,]")
pr.Flags().StringP("result-file", "f", "", "path to the golangci-lint JSON output [$NITTER_RESULT_FILE]")
lintPr := &cobra.Command{
Use: "lint-pull-request",
Short: "Run golangci-lint directly and create a PR review based on the captured results - required golangci-lint being in $PATH",
Aliases: []string{"lint-pr", "lint-pull"},
RunE: runGiteaPRLintNitting,
}
lintPr.Flags().Var(&errorReviewStateType, "review-state", "state in which to create the review if there are issues [comment,approved,]")
lintPr.Flags().Int64P(
"pull-index",
"i",
-1,
"PR index to add reviews to - note, this is not the ID of the PR but its number [$NITTER_PULL_INDEX]",
)
lintPr.Flags().Bool(
"from-pr-base",
false,
"if enabled, nitter will get the PRs metadata and start golangci-lint with --new-from-rev=<PR base commit>",
)
lintPr.Flags().StringSlice(
"golangci-lint-extra-args",
nil,
"Extra args to append to golangci-lint run commands, keep in mind that out-format will be set to JSON and --new-from-rev if you enable --from-pr-base",
)
giteaCmd.AddCommand(pr, lintPr)
return giteaCmd
}
func runGiteaPRNitting(cmd *cobra.Command, args []string) error {
cfg, err := LoadConfig[gitea.GiteaPRConfig](cmd.Flags())
if err != nil {
return err
}
if err = cfg.Validate(); err != nil {
return err
}
report, issues, err := ReadResultsFile(cmd.Flag("result-file").Value.String())
if err != nil {
return err
}
giteaClient, err := giteaSdk.NewClient(cfg.BaseAddress, giteaSdk.SetContext(cmd.Context()), giteaSdk.SetToken(cfg.Token))
if err != nil {
return err
}
nitter := gitea.NewPRNitter(giteaClient, cfg)
return nitter.Report(report, issues)
}
func runGiteaPRLintNitting(cmd *cobra.Command, _ []string) error {
cfg, err := LoadConfig[gitea.GiteaPRLintConfig](cmd.Flags())
if err != nil {
return err
}
if err = cfg.Validate(); err != nil {
return err
}
giteaClient, err := giteaSdk.NewClient(
cfg.BaseAddress,
giteaSdk.SetContext(cmd.Context()),
giteaSdk.SetToken(cfg.Token),
)
if err != nil {
return err
}
if cfg.FromPRBase {
var pr *giteaSdk.PullRequest
if pr, _, err = giteaClient.GetPullRequest(cfg.Namespace, cfg.Repo, cfg.PRIndex); err != nil {
return err
} else {
cfg.ExtraArgs = append(cfg.ExtraArgs, fmt.Sprintf("--new-from-rev=%s", pr.Base.Sha))
}
}
report, issues, err := RunGolangCiLint(cmd.Context(), cfg.ExtraArgs)
if err != nil {
return err
}
nitter := gitea.NewPRNitter(giteaClient, &cfg.GiteaPRConfig)
return nitter.Report(report, issues)
}