Skip to content

Commit

Permalink
Test event parser
Browse files Browse the repository at this point in the history
  • Loading branch information
lkysow committed Nov 16, 2017
1 parent 6258957 commit 3e1ab00
Show file tree
Hide file tree
Showing 12 changed files with 446 additions and 38 deletions.
2 changes: 1 addition & 1 deletion server/events/command_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/hootsuite/atlantis/server/events/vcs"
"github.com/hootsuite/atlantis/server/logging"
"github.com/hootsuite/atlantis/server/recovery"
"github.com/pkg/errors"
"github.com/lkysow/go-gitlab"
"github.com/pkg/errors"
)

//go:generate pegomock generate --use-experimental-model-gen --package mocks -o mocks/mock_command_runner.go CommandRunner
Expand Down
8 changes: 4 additions & 4 deletions server/events/command_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package events_test

import (
"testing"
"reflect"
"strings"
"bytes"
"log"
"errors"
"log"
"reflect"
"strings"
"testing"

"github.com/google/go-github/github"
"github.com/hootsuite/atlantis/server/events"
Expand Down
35 changes: 27 additions & 8 deletions server/events/event_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,40 +221,59 @@ func (e *EventParser) ParseGitlabMergeEvent(event gitlab.MergeEvent) (models.Pul
}

cloneURL := e.addGitlabAuth(event.Project.GitHTTPURL)
// Get owner and name from PathWithNamespace because the fields
// event.Project.Name and event.Project.Owner can have capitals.
owner, name := e.getOwnerAndName(event.Project.PathWithNamespace)
repo := models.Repo{
FullName: event.Project.PathWithNamespace,
Name: event.Project.Name,
Name: name,
SanitizedCloneURL: event.Project.GitHTTPURL,
Owner: event.Project.Namespace,
Owner: owner,
CloneURL: cloneURL,
}
return pull, repo
}

// addGitlabAuth adds gitlab username/password to the cloneURL.
// Expects cloneURL to be an https URL.
// We support http and https URLs because GitLab's docs have http:// URLs whereas
// their API responses have https://.
// Ex. https://gitlab.com/owner/repo.git => https://uname:[email protected]/owner/repo.git
func (e *EventParser) addGitlabAuth(cloneURL string) string {
return strings.Replace(cloneURL, "https://", fmt.Sprintf("https://%s:%s@", e.GitlabUser, e.GitlabToken), -1)
httpsReplaced := strings.Replace(cloneURL, "https://", fmt.Sprintf("https://%s:%s@", e.GitlabUser, e.GitlabToken), -1)
return strings.Replace(httpsReplaced, "http://", fmt.Sprintf("http://%s:%s@", e.GitlabUser, e.GitlabToken), -1)
}

// getOwnerAndName takes pathWithNamespace that should look like "owner/repo"
// and returns "owner", "repo"
func (e *EventParser) getOwnerAndName(pathWithNamespace string) (string, string) {
pathSplit := strings.Split(pathWithNamespace, "/")
if len(pathSplit) > 1 {
return pathSplit[0], pathSplit[1]
}
return "", ""
}

// ParseGitlabMergeCommentEvent creates Atlantis models out of a GitLab event.
func (e *EventParser) ParseGitlabMergeCommentEvent(event gitlab.MergeCommentEvent) (baseRepo models.Repo, headRepo models.Repo, user models.User) {
// Get owner and name from PathWithNamespace because the fields
// event.Project.Name and event.Project.Owner can have capitals.
owner, name := e.getOwnerAndName(event.Project.PathWithNamespace)
baseRepo = models.Repo{
FullName: event.Project.PathWithNamespace,
Name: event.Project.Name,
Name: name,
SanitizedCloneURL: event.Project.GitHTTPURL,
Owner: event.Project.Namespace,
Owner: owner,
CloneURL: e.addGitlabAuth(event.Project.GitHTTPURL),
}
user = models.User{
Username: event.User.Username,
}
owner, name = e.getOwnerAndName(event.MergeRequest.Source.PathWithNamespace)
headRepo = models.Repo{
FullName: event.MergeRequest.Source.PathWithNamespace,
Name: event.MergeRequest.Source.Name,
Name: name,
SanitizedCloneURL: event.MergeRequest.Source.GitHTTPURL,
Owner: event.MergeRequest.Source.Namespace,
Owner: owner,
CloneURL: e.addGitlabAuth(event.MergeRequest.Source.GitHTTPURL),
}
return
Expand Down
Loading

0 comments on commit 3e1ab00

Please sign in to comment.