diff --git a/tools/sed-i-github-issues/README.md b/tools/sed-i-github-issues/README.md new file mode 100644 index 000000000..c250c2073 --- /dev/null +++ b/tools/sed-i-github-issues/README.md @@ -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" diff --git a/tools/sed-i-github-issues/main.go b/tools/sed-i-github-issues/main.go new file mode 100644 index 000000000..9ffbc2745 --- /dev/null +++ b/tools/sed-i-github-issues/main.go @@ -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) + } + } +}