Skip to content

Commit 556ea9e

Browse files
committed
Implement push subcommand.
1 parent 20fe60d commit 556ea9e

40 files changed

+647
-8
lines changed

cmd/push.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cmd
2+
3+
import (
4+
"github.com/github/codeql-action-sync/internal/cachedirectory"
5+
"github.com/github/codeql-action-sync/internal/push"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var pushCmd = &cobra.Command{
10+
Use: "push",
11+
Short: "Push the CodeQL Action from the local cache to a GitHub Enterprise Server installation.",
12+
RunE: func(cmd *cobra.Command, args []string) error {
13+
cacheDirectory := cachedirectory.NewCacheDirectory(rootFlags.cacheDir)
14+
return push.Push(cmd.Context(), cacheDirectory, pushFlags.destinationURL, pushFlags.destinationToken, pushFlags.destinationRepository)
15+
},
16+
}
17+
18+
type pushFlagFields struct {
19+
destinationURL string
20+
destinationToken string
21+
destinationRepository string
22+
}
23+
24+
var pushFlags = pushFlagFields{}
25+
26+
func (f *pushFlagFields) Init(cmd *cobra.Command) {
27+
cmd.Flags().StringVar(&f.destinationURL, "destination-url", "", "The URL of the GitHub Enterprise instance to push to.")
28+
cmd.MarkFlagRequired("destination-url")
29+
cmd.Flags().StringVar(&f.destinationToken, "destination-token", "", "A token to access the API on the GitHub Enterprise instance.")
30+
cmd.MarkFlagRequired("destination-token")
31+
cmd.Flags().StringVar(&f.destinationRepository, "destination-repository", "github/codeql-action", "The name of the repository to create on GitHub Enterprise.")
32+
}

cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,8 @@ func Execute(ctx context.Context) error {
5555
rootCmd.AddCommand(pullCmd)
5656
pullFlags.Init(pullCmd)
5757

58+
rootCmd.AddCommand(pushCmd)
59+
pushFlags.Init(pushCmd)
60+
5861
return rootCmd.ExecuteContext(ctx)
5962
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ require (
1010
github.com/pkg/errors v0.8.1
1111
github.com/spf13/cobra v1.0.0
1212
github.com/stretchr/testify v1.6.1
13+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
1314
)

go.sum

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/cachedirectory/cachedirectory.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path"
99
"path/filepath"
1010

11+
"github.com/go-git/go-git/v5"
1112
"github.com/pkg/errors"
1213
)
1314

@@ -16,6 +17,8 @@ const errorNotACacheOrEmpty = "The cache directory you have selected is not empt
1617
const errorCacheParentDoesNotExist = "Cannot create cache directory because its parent, does not exist."
1718
const errorPushNonCache = "The directory you have provided does not appear to be valid. Please check it exists and that you have run the `pull` command to populate it."
1819

20+
const CacheReferencePrefix = "refs/remotes/" + git.DefaultRemoteName + "/"
21+
1922
type CacheDirectory struct {
2023
path string
2124
}

internal/pull/pull.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func (pullService *pullService) pullGit(fresh bool) error {
8181
err = localRepository.FetchContext(pullService.ctx, &git.FetchOptions{
8282
RemoteName: git.DefaultRemoteName,
8383
RefSpecs: []config.RefSpec{
84-
config.RefSpec("+refs/heads/*:refs/remotes/" + git.DefaultRemoteName + "/heads/*"),
85-
config.RefSpec("+refs/tags/*:refs/remotes/" + git.DefaultRemoteName + "/tags/*"),
84+
config.RefSpec("+refs/heads/*:" + cachedirectory.CacheReferencePrefix + "heads/*"),
85+
config.RefSpec("+refs/tags/*:" + cachedirectory.CacheReferencePrefix + "tags/*"),
8686
},
8787
Progress: os.Stderr,
8888
Tags: git.NoTags,

internal/pull/pull_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestPullGitFresh(t *testing.T) {
5858
pullService := getTestPullService(t, temporaryDirectory, initialActionRepository, "")
5959
err := pullService.pullGit(true)
6060
require.NoError(t, err)
61-
checkExpectedReferencesInCache(t, pullService.cacheDirectory, []string{
61+
test.CheckExpectedReferencesInRepository(t, pullService.cacheDirectory.GitPath(), []string{
6262
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/remotes/origin/heads/main",
6363
"26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/heads/v1",
6464
"e529a54fad10a936308b2220e05f7f00757f8e7c refs/remotes/origin/heads/v3",
@@ -93,7 +93,7 @@ func TestPullGitNotFreshWithChanges(t *testing.T) {
9393
pullService = getTestPullService(t, temporaryDirectory, modifiedActionRepository, "")
9494
err = pullService.pullGit(false)
9595
require.NoError(t, err)
96-
checkExpectedReferencesInCache(t, pullService.cacheDirectory, []string{
96+
test.CheckExpectedReferencesInRepository(t, pullService.cacheDirectory.GitPath(), []string{
9797
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/remotes/origin/heads/main",
9898
"26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/heads/v1",
9999
"33d42021633d74bcd0bf9c95e3d3159131a5faa7 refs/remotes/origin/heads/v3", // v3 was force-pushed, and should have been force-pulled too.

internal/pull/pull_test/codeql-action-initial.git/config

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
repositoryformatversion = 0
33
filemode = true
44
bare = true
5-
[remote "origin"]
6-
url = /tmp/codeql-sim

internal/pull/pull_test/codeql-action-modified.git/config

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
repositoryformatversion = 0
33
filemode = true
44
bare = true
5-
[remote "origin"]
6-
url = /tmp/codeql-sim

0 commit comments

Comments
 (0)