Skip to content

Instantly share code, notes, and snippets.

@PurpleBooth
Last active August 24, 2018 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PurpleBooth/845b03126f93d3c20da8dfb4877ebbe0 to your computer and use it in GitHub Desktop.
Save PurpleBooth/845b03126f93d3c20da8dfb4877ebbe0 to your computer and use it in GitHub Desktop.
hacktastic time it took a pr to get merged
package main
import (
"golang.org/x/oauth2"
"github.com/google/go-github/github"
"context"
"fmt"
"time"
"os"
"log"
"encoding/csv"
)
func main() {
if len(os.Args) != 4 {
fmt.Printf("Usage: %s token owner repository\n", os.Args[0])
os.Exit(1)
}
token := os.Args[1]
owner := os.Args[2]
repository := os.Args[3]
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
w := csv.NewWriter(os.Stdout)
w.Write([]string{"url", "created_at", "merged_at", "duration"})
// get all pages of results
opt := github.PullRequestListOptions{State: "closed"}
for {
pulls, resp, err := client.PullRequests.List(ctx, owner, repository, &opt)
if err != nil {
panic(err.Error())
}
for _, pullRequest := range pulls {
if pullRequest.GetMergedAt() == (time.Time{}) {
continue
}
if err := w.Write([]string{pullRequest.GetURL(), pullRequest.GetCreatedAt().Format(time.RFC3339), pullRequest.GetMergedAt().Format(time.RFC3339), pullRequest.GetMergedAt().Sub(pullRequest.GetCreatedAt()).String()}); err != nil {
log.Fatalln("error writing record to csv:", err)
}
w.Flush()
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment