Created
July 20, 2024 09:43
-
-
Save PurpleBooth/4e6ca7ebcdf73c9895246a904af62a82 to your computer and use it in GitHub Desktop.
Scrape github for merge durations
This file contains 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 ( | |
"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