-
-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests: add repository maturity test #5492
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,77 @@ | ||||||||
package main | ||||||||
|
||||||||
import ( | ||||||||
"encoding/json" | ||||||||
"fmt" | ||||||||
"net/http" | ||||||||
"os" | ||||||||
"strings" | ||||||||
"testing" | ||||||||
"time" | ||||||||
|
||||||||
"github.com/PuerkitoBio/goquery" | ||||||||
) | ||||||||
|
||||||||
var ( | ||||||||
githubApiAuthorizationToken = os.Getenv("GITHUB_API_TOKEN") | ||||||||
minimumMaturityDate = time.Now().AddDate(0, -5, 0) | ||||||||
) | ||||||||
|
||||||||
func TestMaturity(t *testing.T) { | ||||||||
doc := goqueryFromReadme(t) | ||||||||
doc.Find("body li > a:first-child").Each(func(_ int, s *goquery.Selection) { | ||||||||
t.Run(s.Text(), func(t *testing.T) { | ||||||||
href, ok := s.Attr("href") | ||||||||
if !ok { | ||||||||
t.Error("expected to have href") | ||||||||
} | ||||||||
|
||||||||
matches := reGithubRepo.FindStringSubmatch(href) | ||||||||
if matches == nil { | ||||||||
return | ||||||||
} | ||||||||
|
||||||||
if len(matches) != 3 { | ||||||||
t.Fatalf("failed to extract repo and user from: %s, got [%v]", href, strings.Join(matches, ", ")) | ||||||||
} | ||||||||
|
||||||||
if err := checkRepositoryMaturity(matches[1], matches[2]); err != nil { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ypu could use meaningful variable
Suggested change
|
||||||||
t.Fatal(err) | ||||||||
} | ||||||||
}) | ||||||||
}) | ||||||||
} | ||||||||
|
||||||||
func checkRepositoryMaturity(user, repo string) error { | ||||||||
until := minimumMaturityDate.Format(time.RFC3339) | ||||||||
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/commits?per_page=1&until=%s", user, repo, until) | ||||||||
request, err := http.NewRequest(http.MethodGet, url, nil) | ||||||||
if err != nil { | ||||||||
return fmt.Errorf("failed to create request for `%s`, %v", url, err) | ||||||||
} | ||||||||
|
||||||||
request.Header.Set("Accept", "application/vnd.github+json") | ||||||||
request.Header.Set("X-GitHub-Api-Version", "2022-11-28") | ||||||||
request.Header.Set("User-Agent", "avelino") | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The user agent could be a env variable too. And you should have a default one, one that respect the UserAgent format |
||||||||
request.Header.Set("Authorization", "Bearer "+githubApiAuthorizationToken) | ||||||||
|
||||||||
http.DefaultClient.Timeout = time.Minute | ||||||||
httpRes, err := http.DefaultClient.Do(request) | ||||||||
Comment on lines
+58
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would have created a dedicated client, and not playing with the default one |
||||||||
if err != nil { | ||||||||
return fmt.Errorf("failed to fetch commits for [%s/%s], %v", user, repo, err) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When something is repeated like %s/%s + user, repo in almost every error you return, plus the fact the user and repository are the argument of the function, I tend to do not add them in the error message. The caller should do it |
||||||||
} | ||||||||
defer httpRes.Body.Close() | ||||||||
|
||||||||
var commits []any | ||||||||
err = json.NewDecoder(httpRes.Body).Decode(&commits) | ||||||||
if err != nil { | ||||||||
return fmt.Errorf("failed to decode response for [%s/%s], %v", user, repo, err) | ||||||||
} | ||||||||
|
||||||||
if len(commits) == 0 { | ||||||||
minimumDate := minimumMaturityDate.Format(time.DateOnly) | ||||||||
return fmt.Errorf("the project [%s/%s] doesn't have any commits before %s, this is a maturity violation", user, repo, minimumDate) | ||||||||
} | ||||||||
|
||||||||
return nil | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or to respect the GitHub syntax