Skip to content
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

Implements github request with token fallback #1725

Merged
merged 1 commit into from
Mar 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions v2/internal/runner/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,21 +224,38 @@ func (r *Runner) checkNucleiIgnoreFileUpdates(configDir string) bool {
return true
}

// getLatestReleaseFromGithub returns the latest release from GitHub
func (r *Runner) getLatestReleaseFromGithub(latestTag string) (*github.RepositoryRelease, error) {
func getGHClientIncognito() *github.Client {
var tc *http.Client
return github.NewClient(tc)
}

func getGHClientWithToken() *github.Client {
if token, ok := os.LookupEnv("GITHUB_TOKEN"); ok {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc = oauth2.NewClient(ctx, ts)
oauthClient := oauth2.NewClient(ctx, ts)
return github.NewClient(oauthClient)
}
return nil
}

gitHubClient := github.NewClient(tc)

// getLatestReleaseFromGithub returns the latest release from GitHub
func (r *Runner) getLatestReleaseFromGithub(latestTag string) (*github.RepositoryRelease, error) {
var (
gitHubClient *github.Client
retried bool
)
gitHubClient = getGHClientIncognito()
getRelease:
release, _, err := gitHubClient.Repositories.GetReleaseByTag(context.Background(), userName, repoName, "v"+latestTag)
if err != nil {
// retry with authentication
if gitHubClient = getGHClientWithToken(); gitHubClient != nil && !retried {
retried = true
goto getRelease
}
return nil, err
}
if release == nil {
Expand Down