Created
February 2, 2020 07:05
-
-
Save wangkuiyi/bece632546c4f90f06318b2704127fbd to your computer and use it in GitHub Desktop.
This program crawls all code review comments of a GitHub repo
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 ( | |
"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) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feel free to change the URL to point to any other repository. To build and run this program, type the following command