cmd/documentation: Add GitHub token support for contributor list retrieval (#1940)

* cmd/documentation: Add GitHub token support for contributor list retrieval

* docs: Clarify GITHUB_TOKEN usage

* cmd/documentation: Improve error logging and handling in GetContributorList function

* fix: Improve error message formatting in GetContributorList function

* docs: Clarify comment on GITHUB_TOKEN usage in documentation.go

* ci: Add GITHUB_TOKEN to CI environment variables
This commit is contained in:
Adrian Gallagher
2025-06-18 16:20:30 +10:00
committed by GitHub
parent c892f492a9
commit f21a18fa67
4 changed files with 24 additions and 5 deletions

View File

@@ -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 }})

View File

@@ -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

View File

@@ -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

View File

@@ -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