order contributors by number of contributions

This commit is contained in:
rushtehrani
2020-06-11 12:08:30 -07:00
parent 73e7cda6f1
commit 5ecd8ca185

View File

@@ -11,9 +11,10 @@ import (
)
type user struct {
Login string `json:"login"`
URL string `json:"html_url"`
AvatarURL string `json:"avatar_url"`
Login string `json:"login"`
URL string `json:"html_url"`
AvatarURL string `json:"avatar_url"`
ContributionsCount int
}
type label struct {
@@ -110,7 +111,7 @@ func getPrefixSection(prefix string) (section string) {
// Parse issues, pulling only PRs and categorize them based on labels
// Print everything as MD that can be copied into release notes
func printMarkDown(issues []*issue, version *string) {
contributors := make(map[string]user, 0)
contributorsMap := make(map[string]user, 0)
sections := make(map[string]string, 0)
for _, iss := range issues {
@@ -120,7 +121,13 @@ func printMarkDown(issues []*issue, version *string) {
parts := strings.Split(iss.Title, ":")
if len(parts) > 0 {
contributors[iss.User.Login] = iss.User
if user, ok := contributorsMap[iss.User.Login]; ok {
user.ContributionsCount += 1
contributorsMap[iss.User.Login] = user
} else {
iss.User.ContributionsCount = 1
contributorsMap[iss.User.Login] = iss.User
}
sections[getPrefixSection(parts[0])] += fmt.Sprintf("- %s ([#%d](%s))\n", iss.Title, iss.Number, iss.URL)
}
}
@@ -146,22 +153,21 @@ func printMarkDown(issues []*issue, version *string) {
}
fmt.Println("# Contributors")
usernames := make([]string, 0)
for _, contributor := range contributors {
usernames = append(usernames, contributor.Login)
contributors := make([]user, 0)
for _, contributor := range contributorsMap {
contributors = append(contributors, contributor)
}
sort.Slice(usernames, func(i, j int) bool { return strings.ToLower(usernames[i]) < strings.ToLower(usernames[j]) })
for _, username := range usernames {
user := contributors[username]
sort.Slice(contributors, func(i, j int) bool { return contributors[i].ContributionsCount > contributors[j].ContributionsCount })
for _, user := range contributors {
fmt.Println(fmt.Sprintf("- <a href=\"%s\"><img src=\"%s\" width=\"12\"/> <strong>%s</strong></a> %s", user.URL, user.AvatarURL, user.Login, user.Login))
}
}
func httpGet(url string, username *string) (*http.Response, error) {
func httpGet(url string, username, token *string) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet, url, nil)
if username != nil {
req.SetBasicAuth(*username, "")
req.SetBasicAuth(*username, *token)
}
res, err := client.Do(req)
if err != nil {
@@ -172,9 +178,9 @@ func httpGet(url string, username *string) (*http.Response, error) {
}
// Get milestone by title
func getMilestone(repository string, version, username *string) (*milestone, error) {
func getMilestone(repository string, version, username, token *string) (*milestone, error) {
url := fmt.Sprintf("%s%s/milestones", apiPrefix, repository)
res, err := httpGet(url, username)
res, err := httpGet(url, username, token)
if err != nil {
return nil, err
}
@@ -199,9 +205,9 @@ func getMilestone(repository string, version, username *string) (*milestone, err
}
// Get issues from repository
func getIssues(repository string, milestone *milestone, username *string) ([]*issue, error) {
func getIssues(repository string, milestone *milestone, username, token *string) ([]*issue, error) {
url := fmt.Sprintf("%s%s/issues?state=closed&direction=asc&milestone=%d", apiPrefix, repository, milestone.Number)
res, err := httpGet(url, username)
res, err := httpGet(url, username, token)
if err != nil {
return nil, err
}
@@ -222,18 +228,19 @@ func getIssues(repository string, milestone *milestone, username *string) ([]*is
func main() {
version := flag.String("v", "1.0.0", "Version of release, example: -v=1.0.0")
username := flag.String("u", "", "GitHub username for request, example: -u=octocat")
token := flag.String("t", "", "GitHub token for request, example: -t=<token>")
flag.Parse()
issues := make([]*issue, 0)
for _, repository := range repositories {
mil, err := getMilestone(repository, version, username)
mil, err := getMilestone(repository, version, username, token)
if err != nil {
fmt.Printf(err.Error())
return
}
iss, err := getIssues(repository, mil, username)
iss, err := getIssues(repository, mil, username, token)
if err != nil {
return
}