Skip to content

Commit

Permalink
Merge pull request #886 from mcdafydd/fixes-880
Browse files Browse the repository at this point in the history
Fixes #880
  • Loading branch information
lkysow authored Dec 20, 2019
2 parents b066ff1 + 022dc8a commit c7046fa
Show file tree
Hide file tree
Showing 14 changed files with 392 additions and 35 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/imdario/mergo v0.3.5 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.7.3 // indirect
github.com/mcdafydd/go-azuredevops v0.9.0
github.com/mcdafydd/go-azuredevops v0.10.2
github.com/microcosm-cc/bluemonday v1.0.1
github.com/mitchellh/colorstring v0.0.0-20150917214807-8631ce90f286
github.com/mitchellh/go-homedir v1.0.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-github/v28 v28.0.0 h1:+UjHI4+1W/vsXR4jJBWt0ZA74XHbvt5yBAvsf1M3bgM=
Expand Down Expand Up @@ -194,6 +196,10 @@ github.com/mcdafydd/go-azuredevops v0.8.4 h1:MVQgoF2xep/7mMPRrv0u6+nCbFSrMTImnwv
github.com/mcdafydd/go-azuredevops v0.8.4/go.mod h1:1RwqgbF/Afc2o/BLKsRy3dD/nNFQF0YqroNp0CkP4i0=
github.com/mcdafydd/go-azuredevops v0.9.0 h1:WX53H3Z4Mgz9QqRutZFs8+MR8sGJch2Jd6IvPp0GNfo=
github.com/mcdafydd/go-azuredevops v0.9.0/go.mod h1:1RwqgbF/Afc2o/BLKsRy3dD/nNFQF0YqroNp0CkP4i0=
github.com/mcdafydd/go-azuredevops v0.10.0 h1:gfwMWZ3Gn14qTDbx1U2Ut2YNzdu64DqPQ5/twqLl8H4=
github.com/mcdafydd/go-azuredevops v0.10.0/go.mod h1:/NYbgJ/1+9+SmG5CjETCoWm+FlLNcRwdiw1/AGW9zm0=
github.com/mcdafydd/go-azuredevops v0.10.2 h1:cVAxfGqSUK7i4ZRc7s+EpeWSOrDgkBM4SzTRI/IUfoE=
github.com/mcdafydd/go-azuredevops v0.10.2/go.mod h1:/NYbgJ/1+9+SmG5CjETCoWm+FlLNcRwdiw1/AGW9zm0=
github.com/microcosm-cc/bluemonday v1.0.1 h1:SIYunPjnlXcW+gVfvm0IlSeR5U3WZUOLfVmqg85Go44=
github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4=
github.com/mitchellh/colorstring v0.0.0-20150917214807-8631ce90f286 h1:KHyL+3mQOF9sPfs26lsefckcFNDcIZtiACQiECzIUkw=
Expand Down
55 changes: 47 additions & 8 deletions server/events/vcs/azuredevops_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vcs
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -180,19 +181,57 @@ func (g *AzureDevopsClient) UpdateStatus(repo models.Repo, pull models.PullReque
}

genreStr := "Atlantis Bot"
status := azuredevops.GitStatus{
State: &adState,
Description: &description,
Context: &azuredevops.GitStatusContext{
Name: &src,
Genre: &genreStr,
},
status := azuredevops.GitPullRequestStatus{}
status.Context = &azuredevops.GitStatusContext{
Name: &src,
Genre: &genreStr,
}
status.Description = &description
status.State = &adState
if url != "" {
status.TargetURL = &url
}

owner, project, repoName := SplitAzureDevopsRepoFullName(repo.FullName)
_, _, err := g.Client.Git.CreateStatus(g.ctx, owner, project, repoName, pull.HeadCommit, status)

opts := azuredevops.PullRequestListOptions{}
source, resp, err := g.Client.PullRequests.Get(g.ctx, owner, project, pull.Num, &opts)
if err != nil {
return errors.Wrap(err, "getting pull request")
}
if resp.StatusCode != http.StatusOK {
return errors.Wrapf(err, "http response code %d getting pull request", resp.StatusCode)
}
if source.GetSupportsIterations() {
opts := azuredevops.PullRequestIterationsListOptions{}
iterations, resp, err := g.Client.PullRequests.ListIterations(g.ctx, owner, project, repoName, pull.Num, &opts)
if err != nil {
return errors.Wrap(err, "listing pull request iterations")
}
if resp.StatusCode != http.StatusOK {
return errors.Wrapf(err, "http response code %d listing pull request iterations", resp.StatusCode)
}
for _, iteration := range iterations {
if sourceRef := iteration.GetSourceRefCommit(); sourceRef != nil {
if *sourceRef.CommitID == pull.HeadCommit {
status.IterationID = iteration.ID
break
}
}
}
if iterationID := status.IterationID; iterationID != nil {
if !(*iterationID >= 1) {
return errors.New("supportsIterations was true but got invalid iteration ID or no matching iteration commit SHA was found")
}
}
}
_, resp, err = g.Client.PullRequests.CreateStatus(g.ctx, owner, project, repoName, pull.Num, &status)
if err != nil {
return errors.Wrap(err, "creating pull request status")
}
if resp.StatusCode != http.StatusOK {
return errors.Wrapf(err, "http response code %d creating pull request status", resp.StatusCode)
}
return err
}

Expand Down
47 changes: 39 additions & 8 deletions server/events/vcs/azuredevops_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,38 +121,69 @@ func TestAzureDevopsClient_MergePull(t *testing.T) {

func TestAzureDevopsClient_UpdateStatus(t *testing.T) {
cases := []struct {
status models.CommitStatus
expState string
status models.CommitStatus
expState string
supportsIterations bool
}{
{
models.PendingCommitStatus,
"pending",
true,
},
{
models.SuccessCommitStatus,
"succeeded",
true,
},
{
models.FailedCommitStatus,
"failed",
true,
},
{
models.PendingCommitStatus,
"pending",
false,
},
{
models.SuccessCommitStatus,
"succeeded",
false,
},
{
models.FailedCommitStatus,
"failed",
false,
},
}
response := `{"context":{"genre":"Atlantis Bot","name":"src"},"description":"description","state":"%s","targetUrl":"https://google.com"}
`
iterResponse := `{"count": 2, "value": [{"id": 1, "sourceRefCommit": { "commitId": "oldsha"}}, {"id": 2, "sourceRefCommit": { "commitId": "sha"}}]}`
prResponse := `{"supportsIterations": %t}`
partResponse := `{"context":{"genre":"Atlantis Bot","name":"src"},"description":"description","state":"%s","targetUrl":"https://google.com"`

for _, c := range cases {
prResponse := fmt.Sprintf(prResponse, c.supportsIterations)
t.Run(c.expState, func(t *testing.T) {
gotRequest := false
testServer := httptest.NewTLSServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
case "/owner/project/_apis/git/repositories/repo/commits/sha/statuses?api-version=5.1-preview.1":
case "/owner/project/_apis/git/repositories/repo/pullrequests/22/statuses?api-version=5.1-preview.1":
gotRequest = true
defer r.Body.Close() // nolint: errcheck
body, err := ioutil.ReadAll(r.Body)
Ok(t, err)
exp := fmt.Sprintf(response, c.expState)
exp := fmt.Sprintf(partResponse, c.expState)
if c.supportsIterations == true {
exp = fmt.Sprintf("%s%s}\n", exp, `,"iterationId":2`)
} else {
exp = fmt.Sprintf("%s}\n", exp)
}
Equals(t, exp, string(body))
defer r.Body.Close() // nolint: errcheck
w.Write([]byte(response)) // nolint: errcheck
w.Write([]byte(exp)) // nolint: errcheck
case "/owner/project/_apis/git/repositories/repo/pullrequests/22/iterations?api-version=5.1":
w.Write([]byte(iterResponse)) // nolint: errcheck
case "/owner/project/_apis/git/pullrequests/22?api-version=5.1-preview.1":
w.Write([]byte(prResponse)) // nolint: errcheck
default:
t.Errorf("got unexpected request at %q", r.RequestURI)
http.Error(w, "not found", http.StatusNotFound)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion vendor/github.com/google/go-cmp/cmp/report_text.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c7046fa

Please sign in to comment.