2020-09-24 21:21:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-06-10 12:28:34 +00:00
|
|
|
"context"
|
2020-09-24 21:21:00 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2022-06-10 12:28:34 +00:00
|
|
|
"testing"
|
2020-09-24 21:21:00 +00:00
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
2023-02-14 18:12:57 +00:00
|
|
|
const issueTemplateContent = `
|
2020-09-24 21:21:00 +00:00
|
|
|
{{range .}}
|
|
|
|
- [ ] {{.}}
|
|
|
|
{{end}}
|
|
|
|
`
|
|
|
|
|
2023-02-14 18:12:57 +00:00
|
|
|
var issueTemplate = template.Must(template.New("issue").Parse(issueTemplateContent))
|
|
|
|
|
2023-02-15 03:10:05 +00:00
|
|
|
// FIXME: use official github client
|
2020-09-24 21:21:00 +00:00
|
|
|
var reGithubRepo = regexp.MustCompile("https://github.com/[a-zA-Z0-9-._]+/[a-zA-Z0-9-._]+$")
|
|
|
|
var githubGETREPO = "https://api.github.com/repos%s"
|
|
|
|
var githubGETCOMMITS = "https://api.github.com/repos%s/commits"
|
|
|
|
var githubPOSTISSUES = "https://api.github.com/repos/avelino/awesome-go/issues"
|
2023-02-15 01:33:18 +00:00
|
|
|
|
|
|
|
// FIXME: use https
|
2020-09-24 21:21:00 +00:00
|
|
|
var awesomeGoGETISSUES = "http://api.github.com/repos/avelino/awesome-go/issues" //only returns open issues
|
2023-02-15 03:10:05 +00:00
|
|
|
// FIXME: variable has type Duration, but contains a number. we should use
|
|
|
|
//
|
|
|
|
// time.Hour * ... or change type of variable
|
2020-09-24 21:21:00 +00:00
|
|
|
var numberOfYears time.Duration = 1
|
2021-01-19 14:47:33 +00:00
|
|
|
var timeNow = time.Now()
|
2024-04-02 17:08:24 +00:00
|
|
|
var issueTitle = fmt.Sprintf("Investigate repositories with more than 1 year without update - %s", timeNow.Format(time.DateOnly))
|
2020-09-24 21:21:00 +00:00
|
|
|
|
|
|
|
const deadLinkMessage = " this repository might no longer exist! (status code >= 400 returned)"
|
|
|
|
const movedPermanently = " status code 301 received"
|
|
|
|
const status302 = " status code 302 received"
|
|
|
|
const archived = " repository has been archived"
|
|
|
|
|
2023-02-14 18:12:57 +00:00
|
|
|
// LIMIT specifies the max number of repositories that are added in a single run of the script
|
2020-09-24 21:21:00 +00:00
|
|
|
var LIMIT = 10
|
|
|
|
var ctr = 0
|
|
|
|
|
|
|
|
type tokenSource struct {
|
|
|
|
AccessToken string
|
|
|
|
}
|
2023-02-15 01:39:11 +00:00
|
|
|
|
2020-09-24 21:21:00 +00:00
|
|
|
type issue struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
}
|
2023-02-15 01:39:11 +00:00
|
|
|
|
2020-09-24 21:21:00 +00:00
|
|
|
func (t *tokenSource) Token() (*oauth2.Token, error) {
|
2023-02-15 02:55:29 +00:00
|
|
|
return &oauth2.Token{
|
2020-09-24 21:21:00 +00:00
|
|
|
AccessToken: t.AccessToken,
|
2023-02-15 02:55:29 +00:00
|
|
|
}, nil
|
2020-09-24 21:21:00 +00:00
|
|
|
}
|
2023-02-15 01:39:11 +00:00
|
|
|
|
2020-09-24 21:21:00 +00:00
|
|
|
func getRepositoriesFromBody(body string) []string {
|
|
|
|
links := strings.Split(body, "- ")
|
2023-02-15 02:55:29 +00:00
|
|
|
for i, link := range links {
|
|
|
|
link = strings.ReplaceAll(link, "\r", "")
|
|
|
|
link = strings.ReplaceAll(link, "[ ]", "")
|
|
|
|
link = strings.ReplaceAll(link, "[x]", "")
|
|
|
|
link = strings.ReplaceAll(link, " ", "")
|
|
|
|
link = strings.ReplaceAll(link, "\n", "")
|
|
|
|
link = strings.ReplaceAll(link, deadLinkMessage, "")
|
|
|
|
link = strings.ReplaceAll(link, movedPermanently, "")
|
|
|
|
link = strings.ReplaceAll(link, status302, "")
|
|
|
|
link = strings.ReplaceAll(link, archived, "")
|
|
|
|
links[i] = link
|
2020-09-24 21:21:00 +00:00
|
|
|
}
|
2023-02-15 02:55:29 +00:00
|
|
|
|
2020-09-24 21:21:00 +00:00
|
|
|
return links
|
|
|
|
}
|
2023-02-15 01:39:11 +00:00
|
|
|
|
2023-02-15 01:38:24 +00:00
|
|
|
func generateIssueBody(t *testing.T, repositories []string) (string, error) {
|
|
|
|
t.Helper()
|
2023-02-14 18:12:57 +00:00
|
|
|
|
2023-02-15 01:38:24 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
err := issueTemplate.Execute(buf, repositories)
|
|
|
|
requireNoErr(t, err, "Failed to generate template")
|
|
|
|
|
|
|
|
return buf.String(), nil
|
2020-09-24 21:21:00 +00:00
|
|
|
}
|
2023-02-15 01:38:24 +00:00
|
|
|
|
|
|
|
func createIssue(t *testing.T, staleRepos []string, client *http.Client) {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-09-24 21:21:00 +00:00
|
|
|
if len(staleRepos) == 0 {
|
|
|
|
log.Print("NO STALE REPOSITORIES")
|
|
|
|
return
|
|
|
|
}
|
2023-02-15 01:38:24 +00:00
|
|
|
|
|
|
|
body, err := generateIssueBody(t, staleRepos)
|
|
|
|
requireNoErr(t, err, "failed to generate issue body")
|
|
|
|
|
2020-09-24 21:21:00 +00:00
|
|
|
newIssue := &issue{
|
|
|
|
Title: issueTitle,
|
|
|
|
Body: body,
|
|
|
|
}
|
2023-02-15 02:55:29 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
2023-02-15 01:38:24 +00:00
|
|
|
requireNoErr(t, json.NewEncoder(buf).Encode(newIssue), "failed to encode json req")
|
|
|
|
|
2023-02-15 17:22:36 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, githubPOSTISSUES, buf)
|
2023-02-15 01:38:24 +00:00
|
|
|
requireNoErr(t, err, "failed to create request")
|
|
|
|
|
|
|
|
_, roundTripErr := client.Do(req)
|
|
|
|
requireNoErr(t, roundTripErr, "failed to send request")
|
2020-09-24 21:21:00 +00:00
|
|
|
}
|
2023-02-15 01:38:24 +00:00
|
|
|
|
2023-02-15 03:12:05 +00:00
|
|
|
func getAllFlaggedRepositories(t *testing.T, client *http.Client) map[string]bool {
|
2023-02-15 01:38:24 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2023-02-15 17:22:36 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, awesomeGoGETISSUES, nil)
|
2023-02-15 01:38:24 +00:00
|
|
|
requireNoErr(t, err, "failed to create request")
|
|
|
|
|
2020-09-24 21:21:00 +00:00
|
|
|
res, err := client.Do(req)
|
2023-02-15 01:38:24 +00:00
|
|
|
requireNoErr(t, err, "failed to send request")
|
|
|
|
|
2020-09-24 21:21:00 +00:00
|
|
|
defer res.Body.Close()
|
2023-02-15 01:38:24 +00:00
|
|
|
|
2023-02-15 02:55:29 +00:00
|
|
|
var issues []issue
|
|
|
|
requireNoErr(t, json.NewDecoder(res.Body).Decode(&issues), "failed to unmarshal response")
|
2023-02-15 01:38:24 +00:00
|
|
|
|
2023-02-15 03:12:05 +00:00
|
|
|
addressedRepositories := make(map[string]bool)
|
2023-02-15 02:55:29 +00:00
|
|
|
for _, issue := range issues {
|
|
|
|
if issue.Title != issueTitle {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
repos := getRepositoriesFromBody(issue.Body)
|
|
|
|
for _, repo := range repos {
|
2023-02-15 03:12:05 +00:00
|
|
|
addressedRepositories[repo] = true
|
2020-09-24 21:21:00 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-15 02:55:29 +00:00
|
|
|
|
2023-02-15 03:12:05 +00:00
|
|
|
return addressedRepositories
|
2020-09-24 21:21:00 +00:00
|
|
|
}
|
2023-02-15 01:39:11 +00:00
|
|
|
|
2023-02-15 17:17:45 +00:00
|
|
|
func checkRepoAvailability(toRun bool, href string, client *http.Client) ([]string, bool) {
|
2023-02-15 02:55:29 +00:00
|
|
|
if !toRun {
|
2023-02-15 17:16:00 +00:00
|
|
|
return nil, false
|
2023-02-15 02:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ownerRepo := strings.ReplaceAll(href, "https://github.com", "")
|
|
|
|
apiCall := fmt.Sprintf(githubGETREPO, ownerRepo)
|
2023-02-15 17:22:36 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, apiCall, nil)
|
2023-02-15 02:55:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed at repository %s\n", href)
|
2023-02-15 17:16:00 +00:00
|
|
|
return nil, false
|
2023-02-15 02:55:29 +00:00
|
|
|
}
|
2023-02-15 03:12:05 +00:00
|
|
|
|
2023-02-15 02:55:29 +00:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed at repository %s\n", href)
|
2023-02-15 17:16:00 +00:00
|
|
|
return nil, false
|
2020-09-24 21:21:00 +00:00
|
|
|
}
|
2023-02-15 03:12:05 +00:00
|
|
|
|
2023-02-15 02:55:29 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2023-02-15 17:08:13 +00:00
|
|
|
var repoResp struct {
|
|
|
|
Archived bool `json:"archived"`
|
|
|
|
}
|
|
|
|
|
2023-02-15 03:11:17 +00:00
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&repoResp); err != nil {
|
2023-02-15 17:16:00 +00:00
|
|
|
return nil, false
|
2023-02-15 03:11:17 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 17:16:00 +00:00
|
|
|
var isRepoAdded bool
|
2023-02-15 02:55:29 +00:00
|
|
|
|
2023-02-15 17:17:09 +00:00
|
|
|
var warnings []string
|
2023-02-15 02:55:29 +00:00
|
|
|
if resp.StatusCode == http.StatusMovedPermanently {
|
2023-02-15 17:17:09 +00:00
|
|
|
warnings = append(warnings, href+movedPermanently)
|
2023-02-15 02:55:29 +00:00
|
|
|
log.Printf("%s returned %d", href, resp.StatusCode)
|
|
|
|
isRepoAdded = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusFound && !isRepoAdded {
|
2023-02-15 17:17:09 +00:00
|
|
|
warnings = append(warnings, href+status302)
|
2023-02-15 02:55:29 +00:00
|
|
|
log.Printf("%s returned %d", href, resp.StatusCode)
|
|
|
|
isRepoAdded = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode >= http.StatusBadRequest && !isRepoAdded {
|
2023-02-15 17:17:09 +00:00
|
|
|
warnings = append(warnings, href+deadLinkMessage)
|
2023-02-15 02:55:29 +00:00
|
|
|
log.Printf("%s might not exist!", href)
|
|
|
|
isRepoAdded = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if repoResp.Archived && !isRepoAdded {
|
2023-02-15 17:17:09 +00:00
|
|
|
warnings = append(warnings, href+archived)
|
2023-02-15 02:55:29 +00:00
|
|
|
log.Printf("%s is archived!", href)
|
|
|
|
isRepoAdded = true
|
|
|
|
}
|
|
|
|
|
2023-02-15 17:17:09 +00:00
|
|
|
// FIXME: expression `(len(warnings) > 0) == isRepoAdded` is always true.
|
|
|
|
return warnings, isRepoAdded
|
2020-09-24 21:21:00 +00:00
|
|
|
}
|
2023-02-15 01:39:11 +00:00
|
|
|
|
2023-02-15 17:16:18 +00:00
|
|
|
func checkRepoCommitActivity(toRun bool, href string, client *http.Client) ([]string, bool) {
|
2023-02-15 02:55:29 +00:00
|
|
|
if !toRun {
|
2023-02-15 17:16:18 +00:00
|
|
|
return nil, false
|
2023-02-15 02:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ownerRepo := strings.ReplaceAll(href, "https://github.com", "")
|
|
|
|
apiCall := fmt.Sprintf(githubGETCOMMITS, ownerRepo)
|
2023-02-15 17:22:36 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, apiCall, nil)
|
2023-02-15 02:55:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed at repository %s\n", href)
|
2023-02-15 17:16:18 +00:00
|
|
|
return nil, false
|
2020-09-24 21:21:00 +00:00
|
|
|
}
|
2023-02-15 02:55:29 +00:00
|
|
|
|
|
|
|
since := timeNow.Add(-1 * 365 * 24 * numberOfYears * time.Hour)
|
|
|
|
sinceQuery := since.Format(time.RFC3339)
|
|
|
|
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Add("since", sinceQuery)
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed at repository %s\n", href)
|
2023-02-15 17:16:18 +00:00
|
|
|
return nil, false
|
2023-02-15 02:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var respObj []map[string]interface{}
|
2023-02-15 17:16:18 +00:00
|
|
|
// FIXME: handle error in all that cases
|
2023-02-26 00:14:29 +00:00
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&respObj); err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2023-02-15 02:55:29 +00:00
|
|
|
|
2023-02-15 17:16:18 +00:00
|
|
|
var warnings []string
|
|
|
|
var isRepoAdded bool
|
2023-02-15 02:55:29 +00:00
|
|
|
isAged := len(respObj) == 0
|
|
|
|
if isAged {
|
|
|
|
log.Printf("%s has not had a commit in a while", href)
|
2023-02-15 17:16:18 +00:00
|
|
|
warnings = append(warnings, href)
|
2023-02-15 02:55:29 +00:00
|
|
|
isRepoAdded = true
|
|
|
|
}
|
|
|
|
|
2023-02-15 17:16:18 +00:00
|
|
|
// FIXME: expression `(len(warnings) > 0) == isRepoAdded` is always true.
|
|
|
|
return warnings, isRepoAdded
|
2020-09-24 21:21:00 +00:00
|
|
|
}
|
2023-02-15 00:14:43 +00:00
|
|
|
|
|
|
|
func TestStaleRepository(t *testing.T) {
|
|
|
|
doc := goqueryFromReadme(t)
|
2023-02-15 02:55:29 +00:00
|
|
|
|
2020-10-11 19:03:33 +00:00
|
|
|
oauth := os.Getenv("OAUTH_TOKEN")
|
2023-02-15 02:55:29 +00:00
|
|
|
client := &http.Client{
|
|
|
|
Transport: &http.Transport{},
|
|
|
|
}
|
|
|
|
|
2020-09-24 21:21:00 +00:00
|
|
|
if oauth == "" {
|
|
|
|
log.Print("No oauth token found. Using unauthenticated client ...")
|
|
|
|
} else {
|
|
|
|
tokenSource := &tokenSource{
|
|
|
|
AccessToken: oauth,
|
|
|
|
}
|
2022-06-10 12:28:34 +00:00
|
|
|
client = oauth2.NewClient(context.Background(), tokenSource)
|
2020-09-24 21:21:00 +00:00
|
|
|
}
|
2023-02-15 02:55:29 +00:00
|
|
|
|
|
|
|
// FIXME: return addressedRepositories, no need to pass
|
2023-02-15 03:12:05 +00:00
|
|
|
addressedRepositories := getAllFlaggedRepositories(t, client)
|
2020-09-24 21:21:00 +00:00
|
|
|
|
2023-02-15 02:55:29 +00:00
|
|
|
var staleRepos []string
|
|
|
|
doc.
|
|
|
|
Find("body li > a:first-child").
|
|
|
|
EachWithBreak(func(_ int, s *goquery.Selection) bool {
|
|
|
|
href, ok := s.Attr("href")
|
|
|
|
if !ok {
|
|
|
|
log.Println("expected to have href")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctr >= LIMIT && LIMIT != -1 {
|
|
|
|
log.Print("Max number of issues created")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-02-15 17:19:11 +00:00
|
|
|
if _, issueExists := addressedRepositories[href]; issueExists {
|
2023-02-15 02:55:29 +00:00
|
|
|
log.Printf("issue already exists for %s\n", href)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-02-15 17:19:11 +00:00
|
|
|
if !reGithubRepo.MatchString(href) {
|
2020-09-24 21:21:00 +00:00
|
|
|
log.Printf("%s non-github repo not currently handled", href)
|
|
|
|
}
|
2023-02-15 02:55:29 +00:00
|
|
|
|
2023-02-26 00:14:29 +00:00
|
|
|
// FIXME: this is `or` expres24sion. Probably we need `and`?
|
2023-02-15 17:17:45 +00:00
|
|
|
warnings, isRepoAdded := checkRepoAvailability(true, href, client)
|
2023-02-15 17:17:09 +00:00
|
|
|
staleRepos = append(staleRepos, warnings...)
|
2023-02-15 17:16:00 +00:00
|
|
|
|
2023-02-15 17:17:09 +00:00
|
|
|
warnings, isRepoAdded = checkRepoCommitActivity(!isRepoAdded, href, client)
|
|
|
|
staleRepos = append(staleRepos, warnings...)
|
2023-02-15 17:16:18 +00:00
|
|
|
|
2023-02-15 02:55:29 +00:00
|
|
|
if isRepoAdded {
|
|
|
|
ctr++
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2023-02-15 01:38:24 +00:00
|
|
|
createIssue(t, staleRepos, client)
|
2020-09-24 21:21:00 +00:00
|
|
|
}
|