diff --git a/scripts.go b/scripts.go index 68c2a43acb6..1d0a260cb71 100644 --- a/scripts.go +++ b/scripts.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "fmt" "io/ioutil" "github.com/PuerkitoBio/goquery" @@ -13,9 +14,9 @@ func readme() []byte { if err != nil { panic(err) } - html := append([]byte(""), blackfriday.MarkdownCommon(input)...) - html = append(html, []byte("")...) - return html + html := fmt.Sprintf("%s", blackfriday.MarkdownCommon(input)) + htmlByteArray := []byte(html) + return htmlByteArray } func startQuery() *goquery.Document { @@ -24,6 +25,5 @@ func startQuery() *goquery.Document { if err != nil { panic(err) } - return query } diff --git a/test_stale_repositories.go b/test_stale_repositories.go index 787b617ab87..d88983beb72 100644 --- a/test_stale_repositories.go +++ b/test_stale_repositories.go @@ -18,8 +18,9 @@ import ( var reGithubRepo = regexp.MustCompile("https://github.com/[a-zA-Z0-9-.]+/[a-zA-Z0-9-.]+$") var githubGETCOMMITS = "https://api.github.com/repos%s/commits" var githubPOSTISSUES = "https://api.github.com/repos/avelino/awesome-go/issues" +var awesomeGoGETISSUES = "http://api.github.com/repos/avelino/awesome-go/issues" //only returns open issues var numberOfYears time.Duration = 1 -var issueFmt = "Investigate %s. Has not had any new commits in a while" +var issueFmt = "Investigate %s. This repository has not had any new commits in a while." var delay time.Duration = 1 type tokenSource struct { @@ -41,7 +42,35 @@ func createIssues(issueRequests []*http.Request, oauthClient *http.Client) { time.Sleep(delay * time.Second) } } -func testCommitAge(href string, oauthClient *http.Client, issueRequests *[]*http.Request) { +func getAllOpenIssues(oauthClient *http.Client) (map[string]bool, error) { + openIssues := make(map[string]bool) + req, err := http.NewRequest("GET", awesomeGoGETISSUES, nil) + if err != nil { + log.Print("Failed to get all issues") + return nil, err + } + res, err := oauthClient.Do(req) + if err != nil { + log.Print("Failed to get all issues") + return nil, err + } + target := []issue{} + defer res.Body.Close() + json.NewDecoder(res.Body).Decode(&target) + for _, i := range target { + openIssues[i.Title] = true + } + return openIssues, nil +} +func containsOpenIssue(ownerRepo string, openIssues map[string]bool) bool { + issueTitle := fmt.Sprintf(issueFmt, ownerRepo) + _, ok := openIssues[issueTitle] + if ok { + return true + } + return false +} +func testCommitAge(href string, oauthClient *http.Client, issueRequests *[]*http.Request, openIssues map[string]bool) { var isValidRepo bool var isAged bool var ownerRepo string @@ -53,6 +82,11 @@ func testCommitAge(href string, oauthClient *http.Client, issueRequests *[]*http sinceQuery := since.Format(time.RFC3339) if isValidRepo { ownerRepo = strings.ReplaceAll(href, "https://github.com", "") + issueExists := containsOpenIssue(ownerRepo, openIssues) + if issueExists { + log.Printf("issue already exists for %s\n", ownerRepo) + return + } apiCall = fmt.Sprintf(githubGETCOMMITS, ownerRepo) req, err := http.NewRequest("GET", apiCall, nil) if err != nil { @@ -93,12 +127,18 @@ func testStaleRepository() { AccessToken: oauth, } oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource) + openIssues, err := getAllOpenIssues(oauthClient) + if err != nil { + log.Println("Failed to get existing issues. Exiting...") + return + } query.Find("body li > a:first-child").Each(func(_ int, s *goquery.Selection) { href, ok := s.Attr("href") if !ok { log.Println("expected to have href") + } else { + testCommitAge(href, oauthClient, &issueRequests, openIssues) } - testCommitAge(href, oauthClient, &issueRequests) }) createIssues(issueRequests, oauthClient) }