Skip to content

Implement push subcommand. #18

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

Merged
merged 1 commit into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
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
32 changes: 32 additions & 0 deletions cmd/push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"github.com/github/codeql-action-sync/internal/cachedirectory"
"github.com/github/codeql-action-sync/internal/push"
"github.com/spf13/cobra"
)

var pushCmd = &cobra.Command{
Use: "push",
Short: "Push the CodeQL Action from the local cache to a GitHub Enterprise Server installation.",
RunE: func(cmd *cobra.Command, args []string) error {
cacheDirectory := cachedirectory.NewCacheDirectory(rootFlags.cacheDir)
return push.Push(cmd.Context(), cacheDirectory, pushFlags.destinationURL, pushFlags.destinationToken, pushFlags.destinationRepository)
},
}

type pushFlagFields struct {
destinationURL string
destinationToken string
destinationRepository string
}

var pushFlags = pushFlagFields{}

func (f *pushFlagFields) Init(cmd *cobra.Command) {
cmd.Flags().StringVar(&f.destinationURL, "destination-url", "", "The URL of the GitHub Enterprise instance to push to.")
cmd.MarkFlagRequired("destination-url")
cmd.Flags().StringVar(&f.destinationToken, "destination-token", "", "A token to access the API on the GitHub Enterprise instance.")
cmd.MarkFlagRequired("destination-token")
cmd.Flags().StringVar(&f.destinationRepository, "destination-repository", "github/codeql-action", "The name of the repository to create on GitHub Enterprise.")
}
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@ func Execute(ctx context.Context) error {
rootCmd.AddCommand(pullCmd)
pullFlags.Init(pullCmd)

rootCmd.AddCommand(pushCmd)
pushFlags.Init(pushCmd)

return rootCmd.ExecuteContext(ctx)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ require (
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.6.1
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
)
4 changes: 4 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/cachedirectory/cachedirectory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path"
"path/filepath"

"github.com/go-git/go-git/v5"
"github.com/pkg/errors"
)

Expand All @@ -16,6 +17,8 @@ const errorNotACacheOrEmpty = "The cache directory you have selected is not empt
const errorCacheParentDoesNotExist = "Cannot create cache directory because its parent, does not exist."
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."

const CacheReferencePrefix = "refs/remotes/" + git.DefaultRemoteName + "/"

type CacheDirectory struct {
path string
}
Expand Down
4 changes: 2 additions & 2 deletions internal/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ func (pullService *pullService) pullGit(fresh bool) error {
err = localRepository.FetchContext(pullService.ctx, &git.FetchOptions{
RemoteName: git.DefaultRemoteName,
RefSpecs: []config.RefSpec{
config.RefSpec("+refs/heads/*:refs/remotes/" + git.DefaultRemoteName + "/heads/*"),
config.RefSpec("+refs/tags/*:refs/remotes/" + git.DefaultRemoteName + "/tags/*"),
config.RefSpec("+refs/heads/*:" + cachedirectory.CacheReferencePrefix + "heads/*"),
config.RefSpec("+refs/tags/*:" + cachedirectory.CacheReferencePrefix + "tags/*"),
},
Progress: os.Stderr,
Tags: git.NoTags,
Expand Down
4 changes: 2 additions & 2 deletions internal/pull/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestPullGitFresh(t *testing.T) {
pullService := getTestPullService(t, temporaryDirectory, initialActionRepository, "")
err := pullService.pullGit(true)
require.NoError(t, err)
checkExpectedReferencesInCache(t, pullService.cacheDirectory, []string{
test.CheckExpectedReferencesInRepository(t, pullService.cacheDirectory.GitPath(), []string{
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/remotes/origin/heads/main",
"26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/heads/v1",
"e529a54fad10a936308b2220e05f7f00757f8e7c refs/remotes/origin/heads/v3",
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestPullGitNotFreshWithChanges(t *testing.T) {
pullService = getTestPullService(t, temporaryDirectory, modifiedActionRepository, "")
err = pullService.pullGit(false)
require.NoError(t, err)
checkExpectedReferencesInCache(t, pullService.cacheDirectory, []string{
test.CheckExpectedReferencesInRepository(t, pullService.cacheDirectory.GitPath(), []string{
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/remotes/origin/heads/main",
"26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/heads/v1",
"33d42021633d74bcd0bf9c95e3d3159131a5faa7 refs/remotes/origin/heads/v3", // v3 was force-pushed, and should have been force-pulled too.
Expand Down
2 changes: 0 additions & 2 deletions internal/pull/pull_test/codeql-action-initial.git/config
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
repositoryformatversion = 0
filemode = true
bare = true
[remote "origin"]
url = /tmp/codeql-sim
2 changes: 0 additions & 2 deletions internal/pull/pull_test/codeql-action-modified.git/config
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
repositoryformatversion = 0
filemode = true
bare = true
[remote "origin"]
url = /tmp/codeql-sim
Loading