2023-03-08 13:40:04 +00:00
package commands
import (
2023-03-09 11:04:11 +00:00
"fmt"
"strings"
giteaSdk "code.gitea.io/sdk/gitea"
2023-03-08 13:40:04 +00:00
"github.com/spf13/cobra"
"code.icb4dc0.de/prskr/nitter/nitters/gitea"
)
2023-03-09 11:04:11 +00:00
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
2023-03-08 13:40:04 +00:00
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" ,
2023-03-09 11:04:11 +00:00
Short : "Read golangci-lint result and create a PR review" ,
Aliases : [ ] string { "merge-request" , "pr" , "pull" } ,
RunE : runGiteaPRNitting ,
}
2023-03-08 13:40:04 +00:00
2023-03-09 11:04:11 +00:00
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]" ,
)
2023-03-08 13:40:04 +00:00
2023-03-09 11:04:11 +00:00
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]" )
2023-03-08 13:40:04 +00:00
2023-03-09 11:04:11 +00:00
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 ,
2023-03-08 13:40:04 +00:00
}
2023-03-09 11:04:11 +00:00
lintPr . Flags ( ) . Var ( & errorReviewStateType , "review-state" , "state in which to create the review if there are issues [comment,approved,]" )
lintPr . Flags ( ) . Int64P (
2023-03-08 15:13:49 +00:00
"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]" ,
)
2023-03-08 13:40:04 +00:00
2023-03-09 11:04:11 +00:00
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 )
2023-03-08 13:40:04 +00:00
return giteaCmd
}
2023-03-09 11:04:11 +00:00
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 )
}
2023-03-09 13:28:33 +00:00
func runGiteaPRLintNitting ( cmd * cobra . Command , _ [ ] string ) error {
2023-03-09 11:04:11 +00:00
cfg , err := LoadConfig [ gitea . GiteaPRLintConfig ] ( cmd . Flags ( ) )
if err != nil {
return err
}
if err = cfg . Validate ( ) ; err != nil {
return err
}
2023-03-09 13:28:33 +00:00
giteaClient , err := giteaSdk . NewClient (
cfg . BaseAddress ,
giteaSdk . SetContext ( cmd . Context ( ) ) ,
giteaSdk . SetToken ( cfg . Token ) ,
)
2023-03-09 11:04:11 +00:00
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 )
}