diff --git a/cmd/issuelock/issuelock.go b/cmd/issuelock/issuelock.go new file mode 100644 index 00000000..28a8c8cb --- /dev/null +++ b/cmd/issuelock/issuelock.go @@ -0,0 +1,70 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Command issuelock locks Github issues. +package main + +import ( + "io/ioutil" + "log" + "os" + "path/filepath" + "strings" + "time" + + "github.com/google/go-github/github" + "golang.org/x/oauth2" +) + +func main() { + tokenFile := filepath.Join(os.Getenv("HOME"), "keys", "github-gobot") + slurp, err := ioutil.ReadFile(tokenFile) + if err != nil { + log.Fatal(err) + } + f := strings.SplitN(strings.TrimSpace(string(slurp)), ":", 2) + if len(f) != 2 || f[0] == "" || f[1] == "" { + log.Fatalf("Expected token file %s to be of form :", tokenFile) + } + ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: f[1]}) + tc := oauth2.NewClient(oauth2.NoContext, ts) + client := github.NewClient(tc) + + tooOld := time.Now().Add(-365 * 24 * time.Hour).Format("2006-01-02") + log.Printf("Freezing closed issues before %v", tooOld) + for { + result, response, err := client.Search.Issues("repo:golang/go is:closed -label:FrozenDueToAge updated:<="+tooOld, &github.SearchOptions{ + Sort: "created", + Order: "asc", + ListOptions: github.ListOptions{ + PerPage: 500, + }, + }) + if err != nil { + log.Fatal(err) + } + + if *result.Total == 0 { + return + } + log.Printf("Matches: %d, Res: %#v", *result.Total, response) + for _, is := range result.Issues { + num := *is.Number + log.Printf("Freezing issue: %d", *is.Number) + if err := freeze(client, num); err != nil { + log.Fatal(err) + } + time.Sleep(500 * time.Millisecond) // be nice to github + } + } +} + +func freeze(client *github.Client, issueNum int) error { + _, err := client.Issues.Lock("golang", "go", issueNum) + if err != nil { + return err + } + _, _, err = client.Issues.AddLabelsToIssue("golang", "go", issueNum, []string{"FrozenDueToAge"}) + return err +}