Skip to content

Commit b55b519

Browse files
committed
Add a flag to allow for pushing Git contents to GitHub Enterprise over SSH.
1 parent 9ab49c4 commit b55b519

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ From a machine with access to both GitHub.com and GitHub Enterprise Server use t
2727
* `--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. The token does not need to have any scopes.
2828
* `--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.
2929
* `--force` - By default the tool will not overwrite existing repositories. Providing this flag will allow it to.
30+
* `--push-ssh` - Push Git contents over SSH rather than HTTPS. To use this option you must have SSH access to your GitHub Enterprise instance configured.
3031

3132
### I don't have a machine that can access both GitHub.com and GitHub Enterprise Server.
3233
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.
@@ -47,6 +48,7 @@ Now use the `./codeql-action-sync push` command to upload the CodeQL Action and
4748
* `--cache-dir` - The directory to which the Action was previously downloaded.
4849
* `--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.
4950
* `--force` - By default the tool will not overwrite existing repositories. Providing this flag will allow it to.
51+
* `--push-ssh` - Push Git contents over SSH rather than HTTPS. To use this option you must have SSH access to your GitHub Enterprise instance configured.
5052

5153
## Contributing
5254
For more details on contributing improvements to this tool, see our [contributor guide](CONTRIBUTING.md).

cmd/push.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var pushCmd = &cobra.Command{
1313
RunE: func(cmd *cobra.Command, args []string) error {
1414
version.LogVersion()
1515
cacheDirectory := cachedirectory.NewCacheDirectory(rootFlags.cacheDir)
16-
return push.Push(cmd.Context(), cacheDirectory, pushFlags.destinationURL, pushFlags.destinationToken, pushFlags.destinationRepository, pushFlags.force)
16+
return push.Push(cmd.Context(), cacheDirectory, pushFlags.destinationURL, pushFlags.destinationToken, pushFlags.destinationRepository, pushFlags.force, pushFlags.pushSSH)
1717
},
1818
}
1919

@@ -22,6 +22,7 @@ type pushFlagFields struct {
2222
destinationToken string
2323
destinationRepository string
2424
force bool
25+
pushSSH bool
2526
}
2627

2728
var pushFlags = pushFlagFields{}
@@ -33,4 +34,5 @@ func (f *pushFlagFields) Init(cmd *cobra.Command) {
3334
cmd.MarkFlagRequired("destination-token")
3435
cmd.Flags().StringVar(&f.destinationRepository, "destination-repository", "github/codeql-action", "The name of the repository to create on GitHub Enterprise.")
3536
cmd.Flags().BoolVar(&f.force, "force", false, "Replace the existing repository even if it was not created by the sync tool.")
37+
cmd.Flags().BoolVar(&f.pushSSH, "push-ssh", false, "Push Git contents over SSH rather than HTTPS. To use this option you must have SSH access to your GitHub Enterprise instance configured.")
3638
}

cmd/sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var syncCmd = &cobra.Command{
1818
if err != nil {
1919
return err
2020
}
21-
err = push.Push(cmd.Context(), cacheDirectory, pushFlags.destinationURL, pushFlags.destinationToken, pushFlags.destinationRepository, pushFlags.force)
21+
err = push.Push(cmd.Context(), cacheDirectory, pushFlags.destinationURL, pushFlags.destinationToken, pushFlags.destinationRepository, pushFlags.force, pushFlags.pushSSH)
2222
if err != nil {
2323
return err
2424
}

internal/push/push.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type pushService struct {
4545
destinationRepositoryOwner string
4646
destinationToken string
4747
force bool
48+
pushSSH bool
4849
}
4950

5051
func (pushService *pushService) createRepository() (*github.Repository, error) {
@@ -124,6 +125,9 @@ func (pushService *pushService) createRepository() (*github.Repository, error) {
124125

125126
func (pushService *pushService) pushGit(repository *github.Repository, initialPush bool) error {
126127
remoteURL := repository.GetCloneURL()
128+
if pushService.pushSSH {
129+
remoteURL = repository.GetSSHURL()
130+
}
127131
if initialPush {
128132
log.Debugf("Pushing Git releases to %s...", remoteURL)
129133
} else {
@@ -143,6 +147,10 @@ func (pushService *pushService) pushGit(repository *github.Repository, initialPu
143147
Username: "x-access-token",
144148
Password: pushService.destinationToken,
145149
}
150+
if pushService.pushSSH {
151+
// Use the SSH key from the environment.
152+
credentials = nil
153+
}
146154

147155
refSpecBatches := [][]config.RefSpec{}
148156
remoteReferences, err := remote.List(&git.ListOptions{Auth: credentials})
@@ -319,7 +327,7 @@ func (pushService *pushService) pushReleases() error {
319327
return nil
320328
}
321329

322-
func Push(ctx context.Context, cacheDirectory cachedirectory.CacheDirectory, destinationURL string, destinationToken string, destinationRepository string, force bool) error {
330+
func Push(ctx context.Context, cacheDirectory cachedirectory.CacheDirectory, destinationURL string, destinationToken string, destinationRepository string, force bool, pushSSH bool) error {
323331
err := cacheDirectory.CheckOrCreateVersionFile(false, version.Version())
324332
if err != nil {
325333
return err
@@ -351,6 +359,7 @@ func Push(ctx context.Context, cacheDirectory cachedirectory.CacheDirectory, des
351359
destinationRepositoryName: destinationRepositoryName,
352360
destinationToken: destinationToken,
353361
force: force,
362+
pushSSH: pushSSH,
354363
}
355364

356365
repository, err := pushService.createRepository()

0 commit comments

Comments
 (0)