changes #7

Merged
mfocko merged 4 commits from changes into main 2021-12-11 17:17:09 +01:00
2 changed files with 59 additions and 0 deletions
Showing only changes of commit c9ec7c865c - Show all commits

58
src/cmd/open.go Normal file
View file

@ -0,0 +1,58 @@
package cmd
import (
"fmt"
"strings"
"code.gitea.io/sdk/gitea"
"github.com/spf13/cobra"
"git.mfocko.xyz/mfocko/frag-review/core"
)
var (
openMilestone string
openCmd = &cobra.Command{
Use: "open",
Short: "Opens pull requests for review.",
PreRun: InitializeConfig,
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, openMilestone)
core.ExitOnError("Couldn't find the milestone", err)
fmt.Printf("Milestone found %s (deadline %s)\n", milestone.Title, milestone.Deadline)
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 init() {
openCmd.Flags().StringVarP(&openMilestone, "milestone", "m", "", "specifies milestone, i.e. the task")
}
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)
}
}

View file

@ -32,4 +32,5 @@ func Execute() error {
func init() {
rootCmd.PersistentFlags().StringVarP(&configFilepath, "conf", "c", ".frag_review.yml", "path to the config file")
rootCmd.AddCommand(patchCmd)
rootCmd.AddCommand(openCmd)
}