Skip to content

Commit

Permalink
fixes avelino#3211 added check if issue has not been previously opened
Browse files Browse the repository at this point in the history
  • Loading branch information
mrKappen committed Aug 25, 2020
1 parent dd8234b commit 101506c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
8 changes: 4 additions & 4 deletions scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"fmt"
"io/ioutil"

"github.com/PuerkitoBio/goquery"
Expand All @@ -13,9 +14,9 @@ func readme() []byte {
if err != nil {
panic(err)
}
html := append([]byte("<body>"), blackfriday.MarkdownCommon(input)...)
html = append(html, []byte("</body>")...)
return html
html := fmt.Sprintf("<body>%s</body>", blackfriday.MarkdownCommon(input))
htmlByteArray := []byte(html)
return htmlByteArray
}

func startQuery() *goquery.Document {
Expand All @@ -24,6 +25,5 @@ func startQuery() *goquery.Document {
if err != nil {
panic(err)
}

return query
}
46 changes: 43 additions & 3 deletions test_stale_repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 101506c

Please sign in to comment.