Skip to content

Commit

Permalink
Add sed-i-github-issues tool
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Sep 9, 2018
1 parent beeeaf7 commit f9ea734
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tools/sed-i-github-issues/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# set-i-github-issues

Replace multiple issue bodies at once, with a regexp.

## Example

GITHUB_TOKEN=xxx sed-i-github-issues -owner=moul -repo=roadmap -pattern "child of" -replace "blocks"
55 changes: 55 additions & 0 deletions tools/sed-i-github-issues/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"context"
"flag"
"log"
"os"
"regexp"

"github.com/google/go-github/github"
"golang.org/x/oauth2"
)

var (
owner = flag.String("owner", "", "github user")
repo = flag.String("repo", "", "github repo")
pattern = flag.String("pattern", "", "regex pattern")
replace = flag.String("replace", "", "regex replace")
)

func main() {
flag.Parse()

re := regexp.MustCompile(*pattern)

// github client
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
var allIssues []*github.Issue
opts := &github.IssueListByRepoOptions{State: "all"}
for {
issues, resp, err := client.Issues.ListByRepo(ctx, *owner, *repo, opts)
if err != nil {
log.Fatal(err)
}
allIssues = append(allIssues, issues...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
for _, issue := range allIssues {
after := re.ReplaceAllString(*issue.Body, *replace)
if *issue.Body != after {
if _, _, err := client.Issues.Edit(ctx, *owner, *repo, *issue.Number, &github.IssueRequest{Body: &after}); err != nil {
log.Fatal(err)
}
log.Println(*issue.Number)
}
}
}

0 comments on commit f9ea734

Please sign in to comment.