1
0
Fork 0
mirror of https://github.com/avelino/awesome-go.git synced 2024-11-13 11:14:37 -05:00

refactoring 2

This commit is contained in:
Kirill Zhuravlev 2023-02-15 03:55:29 +01:00 committed by Avelino
parent 4f9028fc96
commit 47f88e024c
No known key found for this signature in database
GPG key ID: B345B4D52E98180A

View file

@ -62,26 +62,26 @@ type repo struct {
} }
func (t *tokenSource) Token() (*oauth2.Token, error) { func (t *tokenSource) Token() (*oauth2.Token, error) {
token := &oauth2.Token{ return &oauth2.Token{
AccessToken: t.AccessToken, AccessToken: t.AccessToken,
} }, nil
return token, nil
} }
func getRepositoriesFromBody(body string) []string { func getRepositoriesFromBody(body string) []string {
links := strings.Split(body, "- ") links := strings.Split(body, "- ")
for idx, link := range links { for i, link := range links {
str := strings.ReplaceAll(link, "\r", "") link = strings.ReplaceAll(link, "\r", "")
str = strings.ReplaceAll(str, "[ ]", "") link = strings.ReplaceAll(link, "[ ]", "")
str = strings.ReplaceAll(str, "[x]", "") link = strings.ReplaceAll(link, "[x]", "")
str = strings.ReplaceAll(str, " ", "") link = strings.ReplaceAll(link, " ", "")
str = strings.ReplaceAll(str, "\n", "") link = strings.ReplaceAll(link, "\n", "")
str = strings.ReplaceAll(str, deadLinkMessage, "") link = strings.ReplaceAll(link, deadLinkMessage, "")
str = strings.ReplaceAll(str, movedPermanently, "") link = strings.ReplaceAll(link, movedPermanently, "")
str = strings.ReplaceAll(str, status302, "") link = strings.ReplaceAll(link, status302, "")
str = strings.ReplaceAll(str, archived, "") link = strings.ReplaceAll(link, archived, "")
links[idx] = str links[i] = link
} }
return links return links
} }
@ -110,7 +110,7 @@ func createIssue(t *testing.T, staleRepos []string, client *http.Client) {
Title: issueTitle, Title: issueTitle,
Body: body, Body: body,
} }
buf := new(bytes.Buffer) buf := bytes.NewBuffer(nil)
requireNoErr(t, json.NewEncoder(buf).Encode(newIssue), "failed to encode json req") requireNoErr(t, json.NewEncoder(buf).Encode(newIssue), "failed to encode json req")
req, err := http.NewRequest("POST", githubPOSTISSUES, buf) req, err := http.NewRequest("POST", githubPOSTISSUES, buf)
@ -131,19 +131,22 @@ func getAllFlaggedRepositories(t *testing.T, client *http.Client, flaggedReposit
res, err := client.Do(req) res, err := client.Do(req)
requireNoErr(t, err, "failed to send request") requireNoErr(t, err, "failed to send request")
var target []issue
defer res.Body.Close() defer res.Body.Close()
requireNoErr(t, json.NewDecoder(res.Body).Decode(&target), "failed to unmarshal response") var issues []issue
requireNoErr(t, json.NewDecoder(res.Body).Decode(&issues), "failed to unmarshal response")
for _, i := range target { for _, issue := range issues {
if i.Title == issueTitle { if issue.Title != issueTitle {
repos := getRepositoriesFromBody(i.Body) continue
}
repos := getRepositoriesFromBody(issue.Body)
for _, repo := range repos { for _, repo := range repos {
(*flaggedRepositories)[repo] = true (*flaggedRepositories)[repo] = true
} }
} }
}
return nil return nil
} }
@ -153,7 +156,10 @@ func containsOpenIssue(link string, openIssues map[string]bool) bool {
} }
func testRepoState(toRun bool, href string, client *http.Client, staleRepos *[]string) bool { func testRepoState(toRun bool, href string, client *http.Client, staleRepos *[]string) bool {
if toRun { if !toRun {
return false
}
ownerRepo := strings.ReplaceAll(href, "https://github.com", "") ownerRepo := strings.ReplaceAll(href, "https://github.com", "")
apiCall := fmt.Sprintf(githubGETREPO, ownerRepo) apiCall := fmt.Sprintf(githubGETREPO, ownerRepo)
req, err := http.NewRequest("GET", apiCall, nil) req, err := http.NewRequest("GET", apiCall, nil)
@ -171,70 +177,84 @@ func testRepoState(toRun bool, href string, client *http.Client, staleRepos *[]s
var repoResp repo var repoResp repo
json.NewDecoder(resp.Body).Decode(&repoResp) json.NewDecoder(resp.Body).Decode(&repoResp)
isRepoAdded := false isRepoAdded := false
if resp.StatusCode == http.StatusMovedPermanently { if resp.StatusCode == http.StatusMovedPermanently {
*staleRepos = append(*staleRepos, href+movedPermanently) *staleRepos = append(*staleRepos, href+movedPermanently)
log.Printf("%s returned %d", href, resp.StatusCode) log.Printf("%s returned %d", href, resp.StatusCode)
isRepoAdded = true isRepoAdded = true
} }
if resp.StatusCode == http.StatusFound && !isRepoAdded { if resp.StatusCode == http.StatusFound && !isRepoAdded {
*staleRepos = append(*staleRepos, href+status302) *staleRepos = append(*staleRepos, href+status302)
log.Printf("%s returned %d", href, resp.StatusCode) log.Printf("%s returned %d", href, resp.StatusCode)
isRepoAdded = true isRepoAdded = true
} }
if resp.StatusCode >= http.StatusBadRequest && !isRepoAdded { if resp.StatusCode >= http.StatusBadRequest && !isRepoAdded {
*staleRepos = append(*staleRepos, href+deadLinkMessage) *staleRepos = append(*staleRepos, href+deadLinkMessage)
log.Printf("%s might not exist!", href) log.Printf("%s might not exist!", href)
isRepoAdded = true isRepoAdded = true
} }
if repoResp.Archived && !isRepoAdded { if repoResp.Archived && !isRepoAdded {
*staleRepos = append(*staleRepos, href+archived) *staleRepos = append(*staleRepos, href+archived)
log.Printf("%s is archived!", href) log.Printf("%s is archived!", href)
isRepoAdded = true isRepoAdded = true
} }
return isRepoAdded return isRepoAdded
} }
return false
}
func testCommitAge(toRun bool, href string, client *http.Client, staleRepos *[]string) bool { func testCommitAge(toRun bool, href string, client *http.Client, staleRepos *[]string) bool {
if toRun { if !toRun {
var respObj []map[string]interface{} return false
since := timeNow.Add(-1 * 365 * 24 * numberOfYears * time.Hour) }
sinceQuery := since.Format(time.RFC3339)
ownerRepo := strings.ReplaceAll(href, "https://github.com", "") ownerRepo := strings.ReplaceAll(href, "https://github.com", "")
apiCall := fmt.Sprintf(githubGETCOMMITS, ownerRepo) apiCall := fmt.Sprintf(githubGETCOMMITS, ownerRepo)
req, err := http.NewRequest("GET", apiCall, nil) req, err := http.NewRequest("GET", apiCall, nil)
isRepoAdded := false
if err != nil { if err != nil {
log.Printf("Failed at repository %s\n", href) log.Printf("Failed at repository %s\n", href)
return false return false
} }
since := timeNow.Add(-1 * 365 * 24 * numberOfYears * time.Hour)
sinceQuery := since.Format(time.RFC3339)
q := req.URL.Query() q := req.URL.Query()
q.Add("since", sinceQuery) q.Add("since", sinceQuery)
req.URL.RawQuery = q.Encode() req.URL.RawQuery = q.Encode()
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
log.Printf("Failed at repository %s\n", href) log.Printf("Failed at repository %s\n", href)
return false return false
} }
defer resp.Body.Close() defer resp.Body.Close()
var respObj []map[string]interface{}
json.NewDecoder(resp.Body).Decode(&respObj) json.NewDecoder(resp.Body).Decode(&respObj)
isRepoAdded := false
isAged := len(respObj) == 0 isAged := len(respObj) == 0
if isAged { if isAged {
log.Printf("%s has not had a commit in a while", href) log.Printf("%s has not had a commit in a while", href)
*staleRepos = append(*staleRepos, href) *staleRepos = append(*staleRepos, href)
isRepoAdded = true isRepoAdded = true
} }
return isRepoAdded return isRepoAdded
} }
return false
}
func TestStaleRepository(t *testing.T) { func TestStaleRepository(t *testing.T) {
doc := goqueryFromReadme(t) doc := goqueryFromReadme(t)
var staleRepos []string
oauth := os.Getenv("OAUTH_TOKEN") oauth := os.Getenv("OAUTH_TOKEN")
client := &http.Client{} client := &http.Client{
Transport: &http.Transport{},
}
if oauth == "" { if oauth == "" {
log.Print("No oauth token found. Using unauthenticated client ...") log.Print("No oauth token found. Using unauthenticated client ...")
} else { } else {
@ -243,37 +263,47 @@ func TestStaleRepository(t *testing.T) {
} }
client = oauth2.NewClient(context.Background(), tokenSource) client = oauth2.NewClient(context.Background(), tokenSource)
} }
addressedRepositories := make(map[string]bool) addressedRepositories := make(map[string]bool)
// FIXME: return addressedRepositories, no need to pass
err := getAllFlaggedRepositories(t, client, &addressedRepositories) err := getAllFlaggedRepositories(t, client, &addressedRepositories)
requireNoErr(t, err, "failed to get existing issues") requireNoErr(t, err, "failed to get existing issues")
doc.Find("body li > a:first-child").EachWithBreak(func(_ int, s *goquery.Selection) bool { var staleRepos []string
doc.
Find("body li > a:first-child").
EachWithBreak(func(_ int, s *goquery.Selection) bool {
href, ok := s.Attr("href") href, ok := s.Attr("href")
if !ok { if !ok {
log.Println("expected to have href") log.Println("expected to have href")
return true return true
} }
if ctr >= LIMIT && LIMIT != -1 { if ctr >= LIMIT && LIMIT != -1 {
log.Print("Max number of issues created") log.Print("Max number of issues created")
return false return false
} }
issueExists := containsOpenIssue(href, addressedRepositories) issueExists := containsOpenIssue(href, addressedRepositories)
if issueExists { if issueExists {
log.Printf("issue already exists for %s\n", href) log.Printf("issue already exists for %s\n", href)
} else { return true
}
isGithubRepo := reGithubRepo.MatchString(href) isGithubRepo := reGithubRepo.MatchString(href)
if isGithubRepo { if !isGithubRepo {
log.Printf("%s non-github repo not currently handled", href)
}
// FIXME: this is `or` expression. Probably we need `and` // FIXME: this is `or` expression. Probably we need `and`
isRepoAdded := testRepoState(true, href, client, &staleRepos) isRepoAdded := testRepoState(true, href, client, &staleRepos)
isRepoAdded = testCommitAge(!isRepoAdded, href, client, &staleRepos) isRepoAdded = testCommitAge(!isRepoAdded, href, client, &staleRepos)
if isRepoAdded { if isRepoAdded {
ctr++ ctr++
} }
} else {
log.Printf("%s non-github repo not currently handled", href)
}
}
return true return true
}) })
createIssue(t, staleRepos, client) createIssue(t, staleRepos, client)
} }