feat: Merge and patch reviews
Fixes #6 Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
90800cfb67
commit
af943fabba
2 changed files with 99 additions and 0 deletions
98
src/cmd/merge.go
Normal file
98
src/cmd/merge.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
// "sort"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"git.mfocko.xyz/mfocko/frag-review/core"
|
||||
)
|
||||
|
||||
var (
|
||||
mergeCmd = &cobra.Command{
|
||||
Use: "merge",
|
||||
Short: "Merge already done reviews.",
|
||||
PreRun: TaskRequiredPreRun,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
client, err := gitea.NewClient(config.Gitea.InstanceURL, gitea.SetToken(config.Gitea.Token))
|
||||
core.ExitOnError("Couldn't create gitea client", err)
|
||||
|
||||
milestone, _, err := client.GetMilestoneByName(config.Gitea.Owner, config.Gitea.Repository, task)
|
||||
core.ExitOnError("Couldn't find the milestone", err)
|
||||
|
||||
fmt.Printf("Task %s found (deadline %s)\n", milestone.Title, milestone.Deadline)
|
||||
|
||||
prs, _, err := client.ListRepoPullRequests(config.Gitea.Owner, config.Gitea.Repository, gitea.ListPullRequestsOptions{
|
||||
State: gitea.StateOpen,
|
||||
Milestone: milestone.ID,
|
||||
})
|
||||
core.ExitOnError("Couldn't list the pull requests", err)
|
||||
|
||||
mergedPRs := Merge(client, prs)
|
||||
CheckoutAndPull("main")
|
||||
PatchPRs(client, mergedPRs)
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func HasGrade(pr *gitea.PullRequest) bool {
|
||||
for _, label := range pr.Labels {
|
||||
if strings.HasPrefix(label.Name, "grade:") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func Merge(client *gitea.Client, prs []*gitea.PullRequest) []*gitea.PullRequest {
|
||||
mergedPRs := make([]*gitea.PullRequest, 0)
|
||||
|
||||
for _, pr := range prs {
|
||||
if !HasGrade(pr) {
|
||||
fmt.Printf("Skipping PR #%d (%s) because of missing grade.\n", pr.Index, pr.Title)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("Merging PR #%d (%s): ", pr.Index, pr.Title)
|
||||
|
||||
result, response, err := client.MergePullRequest(config.Gitea.Owner, config.Gitea.Repository, pr.Index, gitea.MergePullRequestOption{
|
||||
Style: gitea.MergeStyleRebaseMerge,
|
||||
Title: fmt.Sprintf("Merge pull request '%s' (#%d) from %s into %s", pr.Title, pr.Index, pr.Head.Name, pr.Base.Name),
|
||||
Message: fmt.Sprintf("Reviewed-on: %s", pr.HTMLURL),
|
||||
})
|
||||
core.ExitOnError("Couldn't merge the pull request", err)
|
||||
|
||||
if result {
|
||||
fmt.Println("[PASS]")
|
||||
mergedPRs = append(mergedPRs, pr)
|
||||
} else {
|
||||
fmt.Println("[FAIL]")
|
||||
fmt.Print("\t", result, response.StatusCode, response.Body)
|
||||
}
|
||||
}
|
||||
|
||||
return mergedPRs
|
||||
}
|
||||
|
||||
func CheckoutAndPull(branch string) {
|
||||
// checkout
|
||||
gitCmd := exec.Command("git", "checkout", branch)
|
||||
err := gitCmd.Run()
|
||||
core.ExitOnError("Couldn't list git branches", err)
|
||||
|
||||
// pull
|
||||
gitCmd = exec.Command("git", "pull")
|
||||
err = gitCmd.Run()
|
||||
core.ExitOnError("Couldn't pull", err)
|
||||
}
|
||||
|
||||
func PatchPRs(client *gitea.Client, prs []*gitea.PullRequest) {
|
||||
for _, pr := range prs {
|
||||
WriteReview(client, pr.Index)
|
||||
}
|
||||
}
|
|
@ -42,4 +42,5 @@ func init() {
|
|||
rootCmd.AddCommand(patchCmd)
|
||||
rootCmd.AddCommand(openCmd)
|
||||
rootCmd.AddCommand(statsCmd)
|
||||
rootCmd.AddCommand(mergeCmd)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue