Skip to content

Commit

Permalink
Adding support for https cloning using github username and token prov… (
Browse files Browse the repository at this point in the history
runatlantis#117)

* Adding support for https cloning using github username and token provided instead of using ssh

* Update event_parser.go

* now using new name for CloneURL that is sanitized and has no username and password
  • Loading branch information
anubhavmishra authored Aug 13, 2017
1 parent c663be4 commit edf8258
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
9 changes: 6 additions & 3 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ type Repo struct {
Owner string
// Name is just the repo name, ex. "atlantis".
Name string
// SSHURL is the full url for cloning.
// ex. "git@github.com:hootsuite/atlantis.git".
SSHURL string
// CloneURL is the full HTTPS url for cloning with username and token string
// ex. "https://username:token@github.com/atlantis/atlantis.git".
CloneURL string
// SanitizedCloneURL is the full HTTPS url for cloning without the username and password.
// ex. "https://github.com/atlantis/atlantis.git".
SanitizedCloneURL string
}

// PullRequest is a GitHub pull request.
Expand Down
23 changes: 15 additions & 8 deletions server/event_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"errors"
"fmt"
"regexp"
"strings"

"github.com/google/go-github/github"
"github.com/hootsuite/atlantis/models"
)

type EventParser struct {
GithubUser string
GithubUser string
GithubToken string
}

func (e *EventParser) DetermineCommand(comment *github.IssueCommentEvent) (*Command, error) {
Expand Down Expand Up @@ -171,14 +173,19 @@ func (e *EventParser) ExtractRepoData(ghRepo *github.Repository) (models.Repo, e
if repoName == "" {
return repo, errors.New("repository.name is null")
}
repoSSHURL := ghRepo.GetSSHURL()
if repoSSHURL == "" {
return repo, errors.New("repository.ssh_url is null")
repoSanitizedCloneURL := ghRepo.GetCloneURL()
if repoSanitizedCloneURL == "" {
return repo, errors.New("repository.clone_url is null")
}

// construct HTTPS repo clone url string with username and password
repoCloneURL := strings.Replace(repoSanitizedCloneURL, "https://", fmt.Sprintf("https://%s:%s@", e.GithubUser, e.GithubToken), -1)

return models.Repo{
Owner: repoOwner,
FullName: repoFullName,
SSHURL: repoSSHURL,
Name: repoName,
Owner: repoOwner,
FullName: repoFullName,
CloneURL: repoCloneURL,
SanitizedCloneURL: repoSanitizedCloneURL,
Name: repoName,
}, nil
}
6 changes: 3 additions & 3 deletions server/event_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestDetermineCommandInvalid(t *testing.T) {
t.Log("given a comment that does not match the regex should return an error")
e := server.EventParser{"user"}
e := server.EventParser{"user", "token"}
comments := []string{
// just the executable, no command
"run",
Expand All @@ -35,7 +35,7 @@ func TestDetermineCommandInvalid(t *testing.T) {

func TestDetermineCommandHelp(t *testing.T) {
t.Log("given a help comment, should match")
e := server.EventParser{"user"}
e := server.EventParser{"user", "token"}
comments := []string{
"run help",
"atlantis help",
Expand All @@ -50,7 +50,7 @@ func TestDetermineCommandHelp(t *testing.T) {
}

func TestDetermineCommandPermutations(t *testing.T) {
e := server.EventParser{"user"}
e := server.EventParser{"user", "token"}

execNames := []string{"run", "atlantis", "@user"}
commandNames := []server.CommandName{server.Plan, server.Apply}
Expand Down
3 changes: 2 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ func NewServer(config ServerConfig) (*Server, error) {
}
logger := logging.NewSimpleLogger("server", log.New(os.Stderr, "", log.LstdFlags), false, logging.ToLogLevel(config.LogLevel))
eventParser := &EventParser{
GithubUser: config.GithubUser,
GithubUser: config.GithubUser,
GithubToken: config.GithubToken,
}
commandHandler := &CommandHandler{
applyExecutor: applyExecutor,
Expand Down
6 changes: 3 additions & 3 deletions server/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func (w *Workspace) Clone(ctx *CommandContext) (string, error) {
return "", errors.Wrap(err, "creating new workspace")
}

ctx.Log.Info("git cloning %q into %q", ctx.HeadRepo.SSHURL, cloneDir)
cloneCmd := exec.Command("git", "clone", ctx.HeadRepo.SSHURL, cloneDir)
ctx.Log.Info("git cloning %q into %q", ctx.HeadRepo.SanitizedCloneURL, cloneDir)
cloneCmd := exec.Command("git", "clone", ctx.HeadRepo.CloneURL, cloneDir)
if output, err := cloneCmd.CombinedOutput(); err != nil {
return "", errors.Wrapf(err, "cloning %s: %s", ctx.HeadRepo.SSHURL, string(output))
return "", errors.Wrapf(err, "cloning %s: %s", ctx.HeadRepo.SanitizedCloneURL, string(output))
}

// check out the branch for this PR
Expand Down

0 comments on commit edf8258

Please sign in to comment.