diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ab354c2e..efb24403 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -2,6 +2,7 @@ on: [push, pull_request] name: CI env: GO_VERSION: 1.24.x + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} jobs: backend: name: GoCryptoTrader backend (${{ matrix.os }}, ${{ matrix.goarch }}, psql=${{ matrix.psql }}, skip_wrapper_tests=${{ matrix.skip_wrapper_tests}}, sonic=${{ matrix.sonic }}) diff --git a/cmd/documentation/README.md b/cmd/documentation/README.md index 1bb06b63..a8198ed2 100644 --- a/cmd/documentation/README.md +++ b/cmd/documentation/README.md @@ -29,7 +29,7 @@ Be aware, this tool will: - Works off a configuration JSON file located at ``gocryptotrader/cmd/documentation/`` for future use with multiple repositories. - Automatically find the directory and file tree for the GoCryptoTrader source code and alert you of undocumented file systems which **need** to be updated. - Automatically find the template folder tree. -- Fetch an updated contributor list and rank on pull request commit amount +- Fetch an updated contributor list and rank on pull request commit amount. Set the `GITHUB_TOKEN` environment variable or use the `ghtoken` command-line flag for optional authentication. - Sets up core folder docs for the root directory tree including **LICENSE** and **CONTRIBUTORS** ### config.json example diff --git a/cmd/documentation/cmd_templates/documentation.tmpl b/cmd/documentation/cmd_templates/documentation.tmpl index 06233731..9c604cd0 100644 --- a/cmd/documentation/cmd_templates/documentation.tmpl +++ b/cmd/documentation/cmd_templates/documentation.tmpl @@ -11,7 +11,7 @@ Be aware, this tool will: - Works off a configuration JSON file located at ``gocryptotrader/cmd/documentation/`` for future use with multiple repositories. - Automatically find the directory and file tree for the GoCryptoTrader source code and alert you of undocumented file systems which **need** to be updated. - Automatically find the template folder tree. -- Fetch an updated contributor list and rank on pull request commit amount +- Fetch an updated contributor list and rank on pull request commit amount. Set the `GITHUB_TOKEN` environment variable or use the `ghtoken` command-line flag for optional authentication. - Sets up core folder docs for the root directory tree including **LICENSE** and **CONTRIBUTORS** ### config.json example diff --git a/cmd/documentation/documentation.go b/cmd/documentation/documentation.go index a40c10b4..e109f7d9 100644 --- a/cmd/documentation/documentation.go +++ b/cmd/documentation/documentation.go @@ -75,6 +75,7 @@ var ( // checking ref = []string{"gocryptotrader", "cmd", "documentation"} engineFolder = "engine" + githubToken = os.Getenv("GITHUB_TOKEN") // Overridden by the ghtoken flag when set ) // Contributor defines an account associated with this code base by doing @@ -85,6 +86,12 @@ type Contributor struct { Contributions int `json:"contributions"` } +// ghError defines a GitHub error response +type ghError struct { + Message string `json:"message"` + Status string `json:"status"` +} + // Config defines the running config to deploy documentation across a github // repository including exclusion lists for files and directories type Config struct { @@ -123,6 +130,7 @@ type Attributes struct { func main() { flag.BoolVar(&verbose, "v", false, "Verbose output") flag.StringVar(&toolDir, "tooldir", "", "Pass in the documentation tool directory if outside tool folder") + flag.StringVar(&githubToken, "ghtoken", githubToken, "Github authentication token to use when fetching the contributors list") flag.Parse() wd, err := os.Getwd() @@ -174,8 +182,7 @@ func main() { } contributors, err = GetContributorList(context.TODO(), config.GithubRepo, verbose) if err != nil { - log.Fatalf("Documentation Generation Tool - GetContributorList error %s", - err) + log.Fatalf("Documentation Generation Tool - GetContributorList error: %s", err) } // Github API missing/deleted user contributors @@ -354,14 +361,25 @@ func GetContributorList(ctx context.Context, repo string, verbose bool) ([]Contr vals := url.Values{} vals.Set("per_page", strconv.Itoa(defaultGithubAPIPerPageLimit)) + headers := make(map[string]string) + if githubToken != "" { + headers["Authorization"] = "Bearer " + githubToken + fmt.Println("Using GitHub token for authentication") + } + for page := 1; ; page++ { vals.Set("page", strconv.Itoa(page)) - contents, err := common.SendHTTPRequest(ctx, http.MethodGet, common.EncodeURLValues(repo+GithubAPIEndpoint, vals), nil, nil, verbose) + contents, err := common.SendHTTPRequest(ctx, http.MethodGet, common.EncodeURLValues(repo+GithubAPIEndpoint, vals), headers, nil, verbose) if err != nil { return nil, err } + var g ghError + if err := json.Unmarshal(contents, &g); err == nil && g.Message != "" { + return nil, fmt.Errorf("GitHub error message: %q Status: %s", g.Message, g.Status) + } + var resp []Contributor if err := json.Unmarshal(contents, &resp); err != nil { return nil, err