From d31865508a242f01f6d83a616b464ec075f3ab87 Mon Sep 17 00:00:00 2001 From: Reed Palmer Date: Fri, 20 Nov 2020 12:55:52 -0500 Subject: [PATCH] Construct pushURL from remote instead of hardcoding github.com --- pkg/git/git.go | 12 ++++++++++++ pkg/releaser/releaser.go | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/git/git.go b/pkg/git/git.go index d464e28c..8c876559 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -19,6 +19,7 @@ import ( "io/ioutil" "os" "os/exec" + "strings" ) type Git struct{} @@ -68,6 +69,17 @@ func (g *Git) Push(workingDir string, args ...string) error { return runCommand(workingDir, command) } +// GetPushURL returns the push url with a token inserted +func (g *Git) GetPushURL(remote string, token string) (string, error) { + pushURL, err := exec.Command("git", "remote", "get-url", "--push", remote).Output() + if err != nil { + return "", err + } + pushURLArray := strings.SplitAfter(string(pushURL), "https://") + pushURLWithToken := pushURLArray[0] + token + "@" + pushURLArray[1] + return pushURLWithToken, nil +} + func runCommand(workingDir string, command *exec.Cmd) error { command.Dir = workingDir command.Stdout = os.Stdout diff --git a/pkg/releaser/releaser.go b/pkg/releaser/releaser.go index f3cecf8a..2680d058 100644 --- a/pkg/releaser/releaser.go +++ b/pkg/releaser/releaser.go @@ -60,6 +60,7 @@ type Git interface { Add(workingDir string, args ...string) error Commit(workingDir string, message string) error Push(workingDir string, args ...string) error + GetPushURL(remote string, token string) (string, error) } type DefaultHttpClient struct{} @@ -207,7 +208,10 @@ func (r *Releaser) UpdateIndexFile() (bool, error) { return false, err } - pushURL := fmt.Sprintf("https://x-access-token:%s@github.com/%s/%s", r.config.Token, r.config.Owner, r.config.GitRepo) + pushURL, err := r.git.GetPushURL(r.config.Remote, r.config.Token) + if err != nil { + return false, err + } if r.config.Push { fmt.Printf("Pushing to branch %q\n", r.config.PagesBranch)