Skip to content

Commit

Permalink
Construct pushURL from remote instead of hardcoding github.com
Browse files Browse the repository at this point in the history
  • Loading branch information
rdpa committed Nov 20, 2020
1 parent c7da1f3 commit d318655
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 12 additions & 0 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"strings"
)

type Git struct{}
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion pkg/releaser/releaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d318655

Please sign in to comment.