Created
July 17, 2019 21:01
-
-
Save kpettijohn/3bbd708768475acd292ab0b8b2b4742f to your computer and use it in GitHub Desktop.
Add a Github label to all issues in a repository based on an existing label
This file contains hidden or 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
package main | |
import ( | |
"context" | |
"fmt" | |
"github.com/google/go-github/v27/github" | |
flag "github.com/spf13/pflag" | |
"golang.org/x/oauth2" | |
"log" | |
"net/url" | |
"os" | |
) | |
func main() { | |
var owner string | |
var repository string | |
var newLabel string | |
var existingLabel string | |
flag.StringVarP(&owner, "owner", "o", "kpettijohn", "Git repo owner") | |
flag.StringVarP(&repository, "repo", "r", "issue-tests", "Git repo name") | |
flag.StringVarP(&newLabel, "new-label", "n", "proj/prometheus", "Label to add to all issues on a given repo") | |
flag.StringVarP(&existingLabel, "existing-label", "e", "Prometheus", "Existing label") | |
flag.Parse() | |
baseURL, err := url.Parse("https://enterprise.org.com/api/v3/") | |
if err != nil { | |
log.Fatalf("Error: %s", err) | |
} | |
uploadURL, err := url.Parse("https://enterprise.org.com/api/v3/") | |
if err != nil { | |
log.Fatalf("Error: %s", err) | |
} | |
ctx := context.Background() | |
ts := oauth2.StaticTokenSource( | |
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")}, | |
) | |
tc := oauth2.NewClient(ctx, ts) | |
client := github.NewClient(tc) | |
client.BaseURL = baseURL | |
client.UploadURL = uploadURL | |
repos, _, err := client.Repositories.Get(ctx, owner, repository) | |
if err != nil { | |
log.Fatalf("Error: %s", err) | |
} | |
issueListopetions := &github.IssueListByRepoOptions{ | |
State: "all", | |
Labels: []string{existingLabel}, | |
} | |
issues, _, err := client.Issues.ListByRepo(ctx, owner, repository, issueListopetions) | |
if err != nil { | |
log.Fatalf("Error: %s", err) | |
} | |
for _, issue := range issues { | |
log.Printf("Adding label (%s) to issue #%d on %s/%s...", newLabel, *issue.Number, owner, repository) | |
_, resp, err := client.Issues.AddLabelsToIssue(ctx, owner, repository, *issue.Number, []string{newLabel}) | |
if err != nil { | |
log.Fatalf("Error: %s", err) | |
} | |
log.Println(resp.Response.Status) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment