Skip to content
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

Adding support for https cloning using github username and token prov… #117

Merged
merged 4 commits into from
Aug 13, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
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. "[email protected]:hootsuite/atlantis.git".
SSHURL string
// CloneURL is the full HTTPS url for cloning.
// ex. "https://github.com/atlantis/atlantis.git".
CloneURL string
Copy link
Collaborator Author

@anubhavmishra anubhavmishra Aug 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CloneURL used for logging instead of using the URL with username and password in it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about keeping it clone url but naming the other one sanitizedcloneurl? No opinion, I like both.

// CloneURLUsernameToken is the full HTTPS url for cloning with username and token string
// ex. "https://username:[email protected]/atlantis/atlantis.git".
CloneURLUsernameToken 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")
repoCloneURL := ghRepo.GetCloneURL()
if repoCloneURL == "" {
return repo, errors.New("repository.clone_url is null")
}

// construct HTTPS repo clone url string with username and token
repoCloneURLUsernameToken := strings.Replace(repoCloneURL, "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,
CloneURLUsernameToken: repoCloneURLUsernameToken,
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.CloneURL, cloneDir)
cloneCmd := exec.Command("git", "clone", ctx.HeadRepo.CloneURLUsernameToken, 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.CloneURL, string(output))
}

// check out the branch for this PR
Expand Down