2021-12-07 00:26:04 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"git.mfocko.xyz/mfocko/frag-review/core"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
openCmd = &cobra.Command{
|
|
|
|
Use: "open",
|
|
|
|
Short: "Opens pull requests for review.",
|
2021-12-11 17:24:28 +01:00
|
|
|
PreRun: TaskRequiredPreRun,
|
2021-12-07 00:26:04 +01:00
|
|
|
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)
|
|
|
|
|
2021-12-11 17:24:28 +01:00
|
|
|
milestone, _, err := client.GetMilestoneByName(config.Gitea.Owner, config.Gitea.Repository, task)
|
2021-12-07 00:26:04 +01:00
|
|
|
core.ExitOnError("Couldn't find the milestone", err)
|
|
|
|
|
2021-12-11 17:24:28 +01:00
|
|
|
fmt.Printf("Task %s found (deadline %s)\n", milestone.Title, milestone.Deadline)
|
2021-12-07 00:26:04 +01:00
|
|
|
|
|
|
|
branches, _, err := client.ListRepoBranches(config.Gitea.Owner, config.Gitea.Repository, gitea.ListRepoBranchesOptions{})
|
|
|
|
core.ExitOnError("Couldn't list all the branches", err)
|
|
|
|
|
|
|
|
ProcessBranches(client, milestone, branches)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func ProcessBranches(client *gitea.Client, milestone *gitea.Milestone, branches []*gitea.Branch) {
|
|
|
|
for _, branch := range branches {
|
|
|
|
if !strings.HasPrefix(branch.Name, milestone.Title) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("- Processing %s\n", branch.Name)
|
|
|
|
pr, _, err := client.CreatePullRequest(config.Gitea.Owner, config.Gitea.Repository, gitea.CreatePullRequestOption{
|
|
|
|
Head: branch.Name,
|
|
|
|
Base: "main",
|
|
|
|
Title: branch.Name,
|
|
|
|
Milestone: milestone.ID,
|
|
|
|
})
|
|
|
|
core.ExitOnError("Couldn't create PR", err)
|
|
|
|
|
|
|
|
fmt.Printf("Created PR #%s\n", pr.ID)
|
|
|
|
}
|
|
|
|
}
|