diff --git a/models/models.go b/models/models.go index 06d66290..c58c8a4f 100644 --- a/models/models.go +++ b/models/models.go @@ -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. diff --git a/server/event_parser.go b/server/event_parser.go index 40662d51..e3e99f73 100644 --- a/server/event_parser.go +++ b/server/event_parser.go @@ -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) { @@ -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 } diff --git a/server/event_parser_test.go b/server/event_parser_test.go index 1e4da6e3..dde417df 100644 --- a/server/event_parser_test.go +++ b/server/event_parser_test.go @@ -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", @@ -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", @@ -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} diff --git a/server/server.go b/server/server.go index b9c0dd56..cb7dcc0e 100644 --- a/server/server.go +++ b/server/server.go @@ -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, diff --git a/server/workspace.go b/server/workspace.go index 5023a444..3db01f17 100644 --- a/server/workspace.go +++ b/server/workspace.go @@ -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