Skip to content

Commit

Permalink
Merge pull request #140 from runatlantis/mohit
Browse files Browse the repository at this point in the history
Comment back on pull request when lock is discarded from Atlantis UI
  • Loading branch information
lkysow authored May 30, 2018
2 parents 822ac11 + a57d4c6 commit ba1d3fd
Show file tree
Hide file tree
Showing 10 changed files with 438 additions and 272 deletions.
8 changes: 4 additions & 4 deletions server/events/command_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *CommandHandler) ExecuteCommand(baseRepo models.Repo, headRepo models.Re
case models.Github:
pull, headRepo, err = c.getGithubData(baseRepo, pullNum)
case models.Gitlab:
pull, err = c.getGitlabData(baseRepo.FullName, pullNum)
pull, err = c.getGitlabData(baseRepo, pullNum)
default:
err = errors.New("Unknown VCS type, this is a bug!")
}
Expand Down Expand Up @@ -120,15 +120,15 @@ func (c *CommandHandler) getGithubData(baseRepo models.Repo, pullNum int) (model
return pull, repo, nil
}

func (c *CommandHandler) getGitlabData(repoFullName string, pullNum int) (models.PullRequest, error) {
func (c *CommandHandler) getGitlabData(baseRepo models.Repo, pullNum int) (models.PullRequest, error) {
if c.GitlabMergeRequestGetter == nil {
return models.PullRequest{}, errors.New("Atlantis not configured to support GitLab")
}
mr, err := c.GitlabMergeRequestGetter.GetMergeRequest(repoFullName, pullNum)
mr, err := c.GitlabMergeRequestGetter.GetMergeRequest(baseRepo.FullName, pullNum)
if err != nil {
return models.PullRequest{}, errors.Wrap(err, "making merge request API call to GitLab")
}
pull := c.EventParser.ParseGitlabMergeRequest(mr)
pull := c.EventParser.ParseGitlabMergeRequest(mr, baseRepo)
return pull, nil
}

Expand Down
19 changes: 15 additions & 4 deletions server/events/event_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type EventParsing interface {
ParseGithubRepo(ghRepo *github.Repository) (models.Repo, error)
ParseGitlabMergeEvent(event gitlab.MergeEvent) (models.PullRequest, models.Repo, error)
ParseGitlabMergeCommentEvent(event gitlab.MergeCommentEvent) (baseRepo models.Repo, headRepo models.Repo, user models.User, err error)
ParseGitlabMergeRequest(mr *gitlab.MergeRequest) models.PullRequest
ParseGitlabMergeRequest(mr *gitlab.MergeRequest, baseRepo models.Repo) models.PullRequest
}

type EventParser struct {
Expand Down Expand Up @@ -104,7 +104,11 @@ func (e *EventParser) ParseGithubPull(pull *github.PullRequest) (models.PullRequ
return pullModel, headRepoModel, errors.New("number is null")
}

headRepoModel, err := e.ParseGithubRepo(pull.Head.Repo)
baseRepoModel, err := e.ParseGithubRepo(pull.Base.Repo)
if err != nil {
return pullModel, headRepoModel, err
}
headRepoModel, err = e.ParseGithubRepo(pull.Head.Repo)
if err != nil {
return pullModel, headRepoModel, err
}
Expand All @@ -121,6 +125,7 @@ func (e *EventParser) ParseGithubPull(pull *github.PullRequest) (models.PullRequ
URL: url,
Num: num,
State: pullState,
BaseRepo: baseRepoModel,
}, headRepoModel, nil
}

Expand All @@ -136,16 +141,17 @@ func (e *EventParser) ParseGitlabMergeEvent(event gitlab.MergeEvent) (models.Pul
// GitLab also has a "merged" state, but we map that to Closed so we don't
// need to check for it.

repo, err := models.NewRepo(models.Gitlab, event.Project.PathWithNamespace, event.Project.GitHTTPURL, e.GitlabUser, e.GitlabToken)
pull := models.PullRequest{
URL: event.ObjectAttributes.URL,
Author: event.User.Username,
Num: event.ObjectAttributes.IID,
HeadCommit: event.ObjectAttributes.LastCommit.ID,
Branch: event.ObjectAttributes.SourceBranch,
State: modelState,
BaseRepo: repo,
}

repo, err := models.NewRepo(models.Gitlab, event.Project.PathWithNamespace, event.Project.GitHTTPURL, e.GitlabUser, e.GitlabToken)
return pull, repo, err
}

Expand All @@ -169,7 +175,11 @@ func (e *EventParser) ParseGitlabMergeCommentEvent(event gitlab.MergeCommentEven
return
}

func (e *EventParser) ParseGitlabMergeRequest(mr *gitlab.MergeRequest) models.PullRequest {
// ParseGitlabMergeRequest parses the merge requests and returns a pull request
// model. We require passing in baseRepo because although can't get this information
// from the merge request, the only caller of this function already has that
// data. This means we can construct the pull request object correctly.
func (e *EventParser) ParseGitlabMergeRequest(mr *gitlab.MergeRequest, baseRepo models.Repo) models.PullRequest {
pullState := models.Closed
if mr.State == gitlabPullOpened {
pullState = models.Open
Expand All @@ -184,5 +194,6 @@ func (e *EventParser) ParseGitlabMergeRequest(mr *gitlab.MergeRequest) models.Pu
HeadCommit: mr.SHA,
Branch: mr.SourceBranch,
State: pullState,
BaseRepo: baseRepo,
}
}
65 changes: 40 additions & 25 deletions server/events/event_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestParseGithubPull(t *testing.T) {
_, _, err = parser.ParseGithubPull(&testPull)
ErrEquals(t, "number is null", err)

pullRes, repoRes, err := parser.ParseGithubPull(&Pull)
pullRes, _, err := parser.ParseGithubPull(&Pull)
Ok(t, err)
Equals(t, models.PullRequest{
URL: Pull.GetHTMLURL(),
Expand All @@ -137,19 +137,18 @@ func TestParseGithubPull(t *testing.T) {
HeadCommit: Pull.Head.GetSHA(),
Num: Pull.GetNumber(),
State: models.Open,
}, pullRes)

Equals(t, models.Repo{
Owner: "owner",
FullName: "owner/repo",
CloneURL: "https://github-user:github-token@github.com/owner/repo.git",
SanitizedCloneURL: Repo.GetCloneURL(),
Name: "repo",
VCSHost: models.VCSHost{
Hostname: "github.com",
Type: models.Github,
BaseRepo: models.Repo{
Owner: "owner",
FullName: "owner/repo",
CloneURL: "https://github-user:github-token@github.com/owner/repo.git",
SanitizedCloneURL: Repo.GetCloneURL(),
Name: "repo",
VCSHost: models.VCSHost{
Hostname: "github.com",
Type: models.Github,
},
},
}, repoRes)
}, pullRes)
}

func TestParseGitlabMergeEvent(t *testing.T) {
Expand All @@ -159,16 +158,8 @@ func TestParseGitlabMergeEvent(t *testing.T) {
Ok(t, err)
pull, repo, err := parser.ParseGitlabMergeEvent(*event)
Ok(t, err)
Equals(t, models.PullRequest{
URL: "http://example.com/diaspora/merge_requests/1",
Author: "root",
Num: 1,
HeadCommit: "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
Branch: "ms-viewport",
State: models.Open,
}, pull)

Equals(t, models.Repo{
expRepo := models.Repo{
FullName: "gitlabhq/gitlab-test",
Name: "gitlab-test",
SanitizedCloneURL: "https://example.com/gitlabhq/gitlab-test.git",
Expand All @@ -178,7 +169,19 @@ func TestParseGitlabMergeEvent(t *testing.T) {
Hostname: "example.com",
Type: models.Gitlab,
},
}, repo)
}

Equals(t, models.PullRequest{
URL: "http://example.com/diaspora/merge_requests/1",
Author: "root",
Num: 1,
HeadCommit: "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
Branch: "ms-viewport",
State: models.Open,
BaseRepo: expRepo,
}, pull)

Equals(t, expRepo, repo)

t.Log("If the state is closed, should set field correctly.")
event.ObjectAttributes.State = "closed"
Expand All @@ -192,19 +195,31 @@ func TestParseGitlabMergeRequest(t *testing.T) {
var event *gitlab.MergeRequest
err := json.Unmarshal([]byte(mergeRequestJSON), &event)
Ok(t, err)
pull := parser.ParseGitlabMergeRequest(event)
repo := models.Repo{
FullName: "gitlabhq/gitlab-test",
Name: "gitlab-test",
SanitizedCloneURL: "https://example.com/gitlabhq/gitlab-test.git",
Owner: "gitlabhq",
CloneURL: "https://gitlab-user:gitlab-token@example.com/gitlabhq/gitlab-test.git",
VCSHost: models.VCSHost{
Hostname: "example.com",
Type: models.Gitlab,
},
}
pull := parser.ParseGitlabMergeRequest(event, repo)
Equals(t, models.PullRequest{
URL: "https://gitlab.com/lkysow/atlantis-example/merge_requests/8",
Author: "lkysow",
Num: 8,
HeadCommit: "0b4ac85ea3063ad5f2974d10cd68dd1f937aaac2",
Branch: "abc",
State: models.Open,
BaseRepo: repo,
}, pull)

t.Log("If the state is closed, should set field correctly.")
event.State = "closed"
pull = parser.ParseGitlabMergeRequest(event)
pull = parser.ParseGitlabMergeRequest(event, repo)
Equals(t, models.Closed, pull.State)
}

Expand Down
20 changes: 12 additions & 8 deletions server/events/mocks/mock_event_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ func (mock *MockEventParsing) ParseGitlabMergeCommentEvent(event go_gitlab.Merge
return ret0, ret1, ret2, ret3
}

func (mock *MockEventParsing) ParseGitlabMergeRequest(mr *go_gitlab.MergeRequest) models.PullRequest {
params := []pegomock.Param{mr}
func (mock *MockEventParsing) ParseGitlabMergeRequest(mr *go_gitlab.MergeRequest, baseRepo models.Repo) models.PullRequest {
params := []pegomock.Param{mr, baseRepo}
result := pegomock.GetGenericMockFrom(mock).Invoke("ParseGitlabMergeRequest", params, []reflect.Type{reflect.TypeOf((*models.PullRequest)(nil)).Elem()})
var ret0 models.PullRequest
if len(result) != 0 {
Expand Down Expand Up @@ -289,8 +289,8 @@ func (c *EventParsing_ParseGitlabMergeCommentEvent_OngoingVerification) GetAllCa
return
}

func (verifier *VerifierEventParsing) ParseGitlabMergeRequest(mr *go_gitlab.MergeRequest) *EventParsing_ParseGitlabMergeRequest_OngoingVerification {
params := []pegomock.Param{mr}
func (verifier *VerifierEventParsing) ParseGitlabMergeRequest(mr *go_gitlab.MergeRequest, baseRepo models.Repo) *EventParsing_ParseGitlabMergeRequest_OngoingVerification {
params := []pegomock.Param{mr, baseRepo}
methodInvocations := pegomock.GetGenericMockFrom(verifier.mock).Verify(verifier.inOrderContext, verifier.invocationCountMatcher, "ParseGitlabMergeRequest", params)
return &EventParsing_ParseGitlabMergeRequest_OngoingVerification{mock: verifier.mock, methodInvocations: methodInvocations}
}
Expand All @@ -300,18 +300,22 @@ type EventParsing_ParseGitlabMergeRequest_OngoingVerification struct {
methodInvocations []pegomock.MethodInvocation
}

func (c *EventParsing_ParseGitlabMergeRequest_OngoingVerification) GetCapturedArguments() *go_gitlab.MergeRequest {
mr := c.GetAllCapturedArguments()
return mr[len(mr)-1]
func (c *EventParsing_ParseGitlabMergeRequest_OngoingVerification) GetCapturedArguments() (*go_gitlab.MergeRequest, models.Repo) {
mr, baseRepo := c.GetAllCapturedArguments()
return mr[len(mr)-1], baseRepo[len(baseRepo)-1]
}

func (c *EventParsing_ParseGitlabMergeRequest_OngoingVerification) GetAllCapturedArguments() (_param0 []*go_gitlab.MergeRequest) {
func (c *EventParsing_ParseGitlabMergeRequest_OngoingVerification) GetAllCapturedArguments() (_param0 []*go_gitlab.MergeRequest, _param1 []models.Repo) {
params := pegomock.GetGenericMockFrom(c.mock).GetInvocationParams(c.methodInvocations)
if len(params) > 0 {
_param0 = make([]*go_gitlab.MergeRequest, len(params[0]))
for u, param := range params[0] {
_param0[u] = param.(*go_gitlab.MergeRequest)
}
_param1 = make([]models.Repo, len(params[1]))
for u, param := range params[1] {
_param1[u] = param.(models.Repo)
}
}
return
}
2 changes: 2 additions & 0 deletions server/events/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ type PullRequest struct {
// Gitlab supports an additional "merged" state but Github doesn't so we map
// merged to Closed.
State PullRequestState
// BaseRepo is the repository that the pull request will be merged into.
BaseRepo Repo
}

type PullRequestState int
Expand Down
3 changes: 2 additions & 1 deletion server/events/vcs/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var Pull = github.PullRequest{
Repo: &Repo,
},
Base: &github.PullRequestBranch{
SHA: github.String("sha256"),
SHA: github.String("sha256"),
Repo: &Repo,
},
HTMLURL: github.String("html-url"),
User: &github.User{
Expand Down
Loading

0 comments on commit ba1d3fd

Please sign in to comment.