Skip to content

Instantly share code, notes, and snippets.

@wangkuiyi
Created February 2, 2020 07:05
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 wangkuiyi/bece632546c4f90f06318b2704127fbd to your computer and use it in GitHub Desktop.
Save wangkuiyi/bece632546c4f90f06318b2704127fbd to your computer and use it in GitHub Desktop.
This program crawls all code review comments of a GitHub repo
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"regexp"
"strings"
)
var (
betweenAngles = regexp.MustCompile(`<(.*)>`)
betweenQuotes = regexp.MustCompile(`\"(.*)\"`)
)
type Comment struct {
Body string `json: body`
}
type CommentList []Comment
func main() {
url := "https://api.github.com/repos/sql-machine-learning/sqlflow/pulls/comments"
for {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
req.SetBasicAuth(os.Getenv("GITHUB_USER"), os.Getenv("GITHUB_PASSWD"))
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
headerLink := resp.Header["Link"][0]
links := map[string]string{}
for _, link := range strings.Split(headerLink, ",") {
ss := strings.Split(link, ";")
l := betweenAngles.FindStringSubmatch(ss[0])[1]
t := betweenQuotes.FindStringSubmatch(ss[1])[1]
links[t] = l
}
var cl CommentList
err = json.NewDecoder(resp.Body).Decode(&cl)
if err != nil {
log.Fatal(err)
}
for _, c := range cl {
fmt.Println(c.Body)
}
if _, ok := links["next"]; !ok {
break
}
url = links["next"]
fmt.Fprintf(os.Stderr, url)
}
}
@wangkuiyi
Copy link
Author

Feel free to change the URL to point to any other repository. To build and run this program, type the following command

go build crawl.go && GITHUB_USER=<your_github_account> GITHUB_PASSWD=<your_passwd> ./crawl > /tmp/crawl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment