Allow providing a token for accessing GitHub.com.

This commit is contained in:
Chris Gavin 2020-08-25 10:27:59 +01:00
Родитель ab43c930ba
Коммит 370650097d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 07F950B80C27E4DA
4 изменённых файлов: 33 добавлений и 6 удалений

Просмотреть файл

@ -22,6 +22,7 @@ From a machine with access to both GitHub.com and GitHub Enterprise Server use t
**Optional Arguments:**
* `--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.
* `--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.
* `--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.
### I don't have a machine that can access both GitHub.com and GitHub Enterprise Server.
@ -29,6 +30,7 @@ From a machine with access to GitHub.com use the `./codeql-action-sync pull` com
**Optional Arguments:**
* `--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.
* `--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.
Next copy the sync tool and cache directory to another machine which has access to GitHub Enterprise Server.

Просмотреть файл

@ -13,12 +13,16 @@ var pullCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
version.LogVersion()
cacheDirectory := cachedirectory.NewCacheDirectory(rootFlags.cacheDir)
return pull.Pull(cmd.Context(), cacheDirectory)
return pull.Pull(cmd.Context(), cacheDirectory, pullFlags.sourceToken)
},
}
type pullFlagFields struct{}
type pullFlagFields struct {
sourceToken string
}
var pullFlags = pullFlagFields{}
func (f *pullFlagFields) Init(cmd *cobra.Command) {}
func (f *pullFlagFields) Init(cmd *cobra.Command) {
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.")
}

Просмотреть файл

@ -14,7 +14,7 @@ var syncCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
version.LogVersion()
cacheDirectory := cachedirectory.NewCacheDirectory(rootFlags.cacheDir)
err := pull.Pull(cmd.Context(), cacheDirectory)
err := pull.Pull(cmd.Context(), cacheDirectory, pullFlags.sourceToken)
if err != nil {
return err
}

Просмотреть файл

@ -13,6 +13,7 @@ import (
"github.com/github/codeql-action-sync/internal/actionconfiguration"
"github.com/mitchellh/ioprogress"
"golang.org/x/oauth2"
"github.com/github/codeql-action-sync/internal/cachedirectory"
"github.com/github/codeql-action-sync/internal/version"
@ -20,6 +21,7 @@ import (
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/google/go-github/v32/github"
"github.com/pkg/errors"
)
@ -37,6 +39,7 @@ type pullService struct {
cacheDirectory cachedirectory.CacheDirectory
gitCloneURL string
githubDotComClient *github.Client
sourceToken string
}
func (pullService *pullService) pullGit(fresh bool) error {
@ -78,6 +81,14 @@ func (pullService *pullService) pullGit(fresh bool) error {
return errors.Wrap(err, "Error setting Git remote.")
}
var credentials *githttp.BasicAuth
if pullService.sourceToken != "" {
credentials = &githttp.BasicAuth{
Username: "x-access-token",
Password: pullService.sourceToken,
}
}
err = localRepository.FetchContext(pullService.ctx, &git.FetchOptions{
RemoteName: git.DefaultRemoteName,
RefSpecs: []config.RefSpec{
@ -87,6 +98,7 @@ func (pullService *pullService) pullGit(fresh bool) error {
Progress: os.Stderr,
Tags: git.NoTags,
Force: true,
Auth: credentials,
})
if err != nil && err != git.NoErrAlreadyUpToDate {
return errors.Wrap(err, "Error doing Git fetch.")
@ -220,17 +232,26 @@ func (pullService *pullService) pullReleases() error {
return nil
}
func Pull(ctx context.Context, cacheDirectory cachedirectory.CacheDirectory) error {
func Pull(ctx context.Context, cacheDirectory cachedirectory.CacheDirectory, sourceToken string) error {
err := cacheDirectory.CheckOrCreateVersionFile(true, version.Version())
if err != nil {
return err
}
var tokenClient *http.Client
if sourceToken != "" {
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: sourceToken},
)
tokenClient = oauth2.NewClient(ctx, tokenSource)
}
pullService := pullService{
ctx: ctx,
cacheDirectory: cacheDirectory,
gitCloneURL: sourceURL,
githubDotComClient: github.NewClient(nil),
githubDotComClient: github.NewClient(tokenClient),
sourceToken: sourceToken,
}
err = pullService.pullGit(false)