-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync milestones between repositories (#3)
Closes #3 This took so long I hate go
- Loading branch information
Showing
12 changed files
with
249 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,6 @@ go.work.sum | |
|
||
# env file | ||
.env | ||
|
||
# JetBrains IDEs | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package github | ||
|
||
import "github.com/google/go-github/v67/github" | ||
|
||
var client = github.NewClient(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"github.com/google/go-github/v67/github" | ||
"propromo/utils" | ||
) | ||
|
||
func ListGitHubMilestones(repo utils.GitRepo) ([]*github.Milestone, error) { | ||
ctx := context.Background() | ||
|
||
milestones, _, err := client.Issues.ListMilestones(ctx, repo.Owner, repo.Repository, nil) | ||
return milestones, err | ||
} | ||
|
||
func CreateGitHubMilestone(token string, repo utils.GitRepo, milestone *github.Milestone) (*github.Milestone, error) { | ||
ctx := context.Background() | ||
client := github.NewClient(nil).WithAuthToken(token) | ||
|
||
milestones, _, err := client.Issues.CreateMilestone(ctx, repo.Owner, repo.Repository, milestone) | ||
return milestones, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var GithubCmd = &cobra.Command{ | ||
Use: "github", | ||
Short: "Github-related utilities", | ||
} | ||
|
||
func init() { | ||
GithubCmd.AddCommand(syncCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"propromo/cmdutils" | ||
) | ||
|
||
var syncCmd = &cobra.Command{ | ||
Use: "sync", | ||
Short: "Syncing utilities for GitHub", | ||
} | ||
|
||
var source string | ||
var targets []string | ||
|
||
func init() { | ||
syncCmd.AddCommand(milestonesCmd) | ||
} | ||
|
||
func getToken() string { | ||
token := viper.GetString("token") | ||
if token == "" { | ||
cmdutils.Logger.Fatal("No token provided.") | ||
} | ||
return token | ||
} | ||
|
||
func syncFlags(cmd *cobra.Command) { | ||
cmd.Flags().String("token", "", "github token") | ||
cobra.CheckErr(viper.BindPFlag("token", cmd.Flags().Lookup("token"))) | ||
|
||
cmd.Flags().StringVarP(&source, "source", "s", "", "Source repository") | ||
cobra.CheckErr(cmd.MarkFlagRequired("source")) | ||
cmd.Flags().StringSliceVarP(&targets, "target", "t", []string{}, "Comma-separated target repository(s)") | ||
cobra.CheckErr(cmd.MarkFlagRequired("target")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
githubmodel "github.com/google/go-github/v67/github" | ||
"github.com/spf13/cobra" | ||
"propromo/client/github" | ||
"propromo/cmdutils" | ||
"propromo/utils" | ||
) | ||
|
||
var milestonesCmd = &cobra.Command{ | ||
Use: "milestones", | ||
Short: "Sync milestones from one repo to others", | ||
Run: func(command *cobra.Command, args []string) { | ||
fmt.Println("Syncing from", source, "to", targets) | ||
|
||
sourceRepo, err := utils.ParseGitRepo(source) | ||
cobra.CheckErr(err) | ||
|
||
milestones, err := github.ListGitHubMilestones(sourceRepo) | ||
cobra.CheckErr(err) | ||
|
||
repos := make([]utils.GitRepo, len(targets)) | ||
for i, target := range targets { | ||
repo, err := utils.ParseGitRepo(target) | ||
cobra.CheckErr(err) | ||
repos[i] = repo | ||
} | ||
|
||
token := getToken() | ||
for _, repo := range repos { | ||
for _, milestone := range milestones { | ||
_, err := github.CreateGitHubMilestone(token, repo, &githubmodel.Milestone{ | ||
Title: milestone.Title, | ||
State: milestone.State, | ||
Description: milestone.Description, | ||
DueOn: milestone.DueOn, | ||
}) | ||
|
||
var ghErr *githubmodel.ErrorResponse | ||
if errors.As(err, &ghErr) && len(ghErr.Errors) == 1 && ghErr.Errors[0].Code == "already_exists" { | ||
cmdutils.Logger.Info("Milestone already exists!", "repo", repo.String(), "milestone", *milestone.Title) | ||
continue | ||
} | ||
cmdutils.Logger.Info("Milestone successfully created", "repo", repo.String(), "milestone", *milestone.Title) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
syncFlags(milestonesCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cmdutils | ||
|
||
import ( | ||
"github.com/charmbracelet/log" | ||
"os" | ||
) | ||
|
||
var Logger = log.NewWithOptions(os.Stdout, log.Options{ | ||
ReportCaller: true, | ||
ReportTimestamp: true, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package main | ||
|
||
import "propromo.cli/cmd" | ||
import "propromo/cmd" | ||
|
||
func main() { | ||
cmd.Execute() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type GitRepo struct { | ||
Owner string | ||
Repository string | ||
} | ||
|
||
func (repo *GitRepo) String() string { | ||
return fmt.Sprintf("%s/%s", repo.Owner, repo.Repository) | ||
} | ||
|
||
func ParseGitRepo(s string) (GitRepo, error) { | ||
// parse `org/repo` to `org` and `repo` | ||
result := strings.Split(s, "/") | ||
if len(strings.Split(s, "/")) != 2 { | ||
return GitRepo{}, fmt.Errorf("invalid git repo: %s", s) | ||
} | ||
|
||
return GitRepo{ | ||
Owner: result[0], | ||
Repository: result[1], | ||
}, nil | ||
} |