Skip to content

Commit 41ceecb

Browse files
authored
Merge pull request #28 from github/source-token
Allow providing a token for accessing GitHub.com.
2 parents 7d3f464 + 3706500 commit 41ceecb

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ From a machine with access to both GitHub.com and GitHub Enterprise Server use t
2424

2525
**Optional Arguments:**
2626
* `--cache-dir` - A temporary directory in which to store data downloaded from GitHub.com before it is uploaded to GitHub Enterprise Server. If not specified a directory next to the sync tool will be used.
27+
* `--source-token` - A token to access the API of GitHub.com. This is normally not required, but can be provided if you have issues with API rate limiting. If provided, it should have the `public_repo` scope.
2728
* `--destination-repository` - The name of the repository in which to create or update the CodeQL Action. If not specified `github/codeql-action` will be used.
2829

2930
### I don't have a machine that can access both GitHub.com and GitHub Enterprise Server.
3031
From a machine with access to GitHub.com use the `./codeql-action-sync pull` command to download a copy of the CodeQL Action and bundles to a local folder.
3132

3233
**Optional Arguments:**
3334
* `--cache-dir` - The directory in which to store data downloaded from GitHub.com. If not specified a directory next to the sync tool will be used.
35+
* `--source-token` - A token to access the API of GitHub.com. This is normally not required, but can be provided if you have issues with API rate limiting. If provided, it should have the `public_repo` scope.
3436

3537
Next copy the sync tool and cache directory to another machine which has access to GitHub Enterprise Server.
3638

cmd/pull.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ var pullCmd = &cobra.Command{
1313
RunE: func(cmd *cobra.Command, args []string) error {
1414
version.LogVersion()
1515
cacheDirectory := cachedirectory.NewCacheDirectory(rootFlags.cacheDir)
16-
return pull.Pull(cmd.Context(), cacheDirectory)
16+
return pull.Pull(cmd.Context(), cacheDirectory, pullFlags.sourceToken)
1717
},
1818
}
1919

20-
type pullFlagFields struct{}
20+
type pullFlagFields struct {
21+
sourceToken string
22+
}
2123

2224
var pullFlags = pullFlagFields{}
2325

24-
func (f *pullFlagFields) Init(cmd *cobra.Command) {}
26+
func (f *pullFlagFields) Init(cmd *cobra.Command) {
27+
cmd.Flags().StringVar(&f.sourceToken, "source-token", "", "A token to access the API of GitHub.com. This is normally not required, but can be provided if you have issues with API rate limiting.")
28+
}

cmd/sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var syncCmd = &cobra.Command{
1414
RunE: func(cmd *cobra.Command, args []string) error {
1515
version.LogVersion()
1616
cacheDirectory := cachedirectory.NewCacheDirectory(rootFlags.cacheDir)
17-
err := pull.Pull(cmd.Context(), cacheDirectory)
17+
err := pull.Pull(cmd.Context(), cacheDirectory, pullFlags.sourceToken)
1818
if err != nil {
1919
return err
2020
}

internal/pull/pull.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import (
1313

1414
"github.com/github/codeql-action-sync/internal/actionconfiguration"
1515
"github.com/mitchellh/ioprogress"
16+
"golang.org/x/oauth2"
1617

1718
"github.com/github/codeql-action-sync/internal/cachedirectory"
1819
"github.com/github/codeql-action-sync/internal/version"
1920
"github.com/go-git/go-git/v5"
2021
"github.com/go-git/go-git/v5/config"
2122
"github.com/go-git/go-git/v5/plumbing"
2223
"github.com/go-git/go-git/v5/plumbing/object"
24+
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
2325
"github.com/google/go-github/v32/github"
2426
"github.com/pkg/errors"
2527
)
@@ -37,6 +39,7 @@ type pullService struct {
3739
cacheDirectory cachedirectory.CacheDirectory
3840
gitCloneURL string
3941
githubDotComClient *github.Client
42+
sourceToken string
4043
}
4144

4245
func (pullService *pullService) pullGit(fresh bool) error {
@@ -78,6 +81,14 @@ func (pullService *pullService) pullGit(fresh bool) error {
7881
return errors.Wrap(err, "Error setting Git remote.")
7982
}
8083

84+
var credentials *githttp.BasicAuth
85+
if pullService.sourceToken != "" {
86+
credentials = &githttp.BasicAuth{
87+
Username: "x-access-token",
88+
Password: pullService.sourceToken,
89+
}
90+
}
91+
8192
err = localRepository.FetchContext(pullService.ctx, &git.FetchOptions{
8293
RemoteName: git.DefaultRemoteName,
8394
RefSpecs: []config.RefSpec{
@@ -87,6 +98,7 @@ func (pullService *pullService) pullGit(fresh bool) error {
8798
Progress: os.Stderr,
8899
Tags: git.NoTags,
89100
Force: true,
101+
Auth: credentials,
90102
})
91103
if err != nil && err != git.NoErrAlreadyUpToDate {
92104
return errors.Wrap(err, "Error doing Git fetch.")
@@ -220,17 +232,26 @@ func (pullService *pullService) pullReleases() error {
220232
return nil
221233
}
222234

223-
func Pull(ctx context.Context, cacheDirectory cachedirectory.CacheDirectory) error {
235+
func Pull(ctx context.Context, cacheDirectory cachedirectory.CacheDirectory, sourceToken string) error {
224236
err := cacheDirectory.CheckOrCreateVersionFile(true, version.Version())
225237
if err != nil {
226238
return err
227239
}
228240

241+
var tokenClient *http.Client
242+
if sourceToken != "" {
243+
tokenSource := oauth2.StaticTokenSource(
244+
&oauth2.Token{AccessToken: sourceToken},
245+
)
246+
tokenClient = oauth2.NewClient(ctx, tokenSource)
247+
}
248+
229249
pullService := pullService{
230250
ctx: ctx,
231251
cacheDirectory: cacheDirectory,
232252
gitCloneURL: sourceURL,
233-
githubDotComClient: github.NewClient(nil),
253+
githubDotComClient: github.NewClient(tokenClient),
254+
sourceToken: sourceToken,
234255
}
235256

236257
err = pullService.pullGit(false)

0 commit comments

Comments
 (0)