diff --git a/integrations/repo_commits_test.go b/integrations/repo_commits_test.go index 1f70d692519b..a83710b95255 100644 --- a/integrations/repo_commits_test.go +++ b/integrations/repo_commits_test.go @@ -5,7 +5,9 @@ package integrations import ( + "encoding/json" "net/http" + "net/http/httptest" "path" "testing" @@ -67,6 +69,27 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) { for _, class := range classes { assert.True(t, sel.HasClass(class)) } + + //By SHA + req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/"+path.Base(commitURL)+"/statuses") + testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), state) + //By Ref + req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/heads%2Fmaster/statuses") + testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), state) +} + +func testRepoCommitsWithStatus(t *testing.T, resp *httptest.ResponseRecorder, state string) { + decoder := json.NewDecoder(resp.Body) + statuses := []*api.Status{} + assert.NoError(t, decoder.Decode(&statuses)) + assert.Len(t, statuses, 1) + for _, s := range statuses { + assert.Equal(t, api.StatusState(state), s.State) + assert.Equal(t, "http://localhost:3003/api/v1/repos/user2/repo1/statuses/65f1bf27bc3bf70f64657658635e66094edbcb4d", s.URL) + assert.Equal(t, "http://test.ci/", s.TargetURL) + assert.Equal(t, "", s.Description) + assert.Equal(t, "testci", s.Context) + } } func TestRepoCommitsWithStatusPending(t *testing.T) { diff --git a/models/commit_status_test.go b/models/commit_status_test.go index 012d4cef4acf..97783ae6f1a1 100644 --- a/models/commit_status_test.go +++ b/models/commit_status_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestGetCommitStatusesViaSHA(t *testing.T) { +func TestGetCommitStatuses(t *testing.T) { assert.NoError(t, PrepareTestDatabase()) repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) @@ -22,57 +22,23 @@ func TestGetCommitStatusesViaSHA(t *testing.T) { assert.Equal(t, int(maxResults), 5) assert.Len(t, statuses, 5) - assert.Equal(t, statuses[0].Context, "ci/awesomeness") - assert.Equal(t, statuses[0].State, CommitStatusPending) - assert.Equal(t, statuses[0].APIURL(), "TODO") + assert.Equal(t, "ci/awesomeness", statuses[0].Context) + assert.Equal(t, CommitStatusPending, statuses[0].State) + assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[0].APIURL()) - assert.Equal(t, statuses[1].Context, "cov/awesomeness") - assert.Equal(t, statuses[1].State, CommitStatusWarning) - assert.Equal(t, statuses[1].APIURL(), "TODO") + assert.Equal(t, "cov/awesomeness", statuses[1].Context) + assert.Equal(t, CommitStatusWarning, statuses[1].State) + assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[1].APIURL()) - assert.Equal(t, statuses[2].Context, "cov/awesomeness") - assert.Equal(t, statuses[2].State, CommitStatusSuccess) - assert.Equal(t, statuses[2].APIURL(), "TODO") + assert.Equal(t, "cov/awesomeness", statuses[2].Context) + assert.Equal(t, CommitStatusSuccess, statuses[2].State) + assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[2].APIURL()) - assert.Equal(t, statuses[3].Context, "ci/awesomeness") - assert.Equal(t, statuses[3].State, CommitStatusFailure) - assert.Equal(t, statuses[3].APIURL(), "TODO") + assert.Equal(t, "ci/awesomeness", statuses[3].Context) + assert.Equal(t, CommitStatusFailure, statuses[3].State) + assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[3].APIURL()) - assert.Equal(t, statuses[4].Context, "deploy/awesomeness") - assert.Equal(t, statuses[4].State, CommitStatusError) - assert.Equal(t, statuses[4].APIURL(), "TODO") + assert.Equal(t, "deploy/awesomeness", statuses[4].Context) + assert.Equal(t, CommitStatusError, statuses[4].State) + assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[4].APIURL()) } - -/* TODO -func TestGetCommitStatusesViaBranch(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) - - repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) - - sha1 := "master" - statuses, maxResults, err := GetCommitStatuses(repo1, sha1, &CommitStatusOptions{}) - assert.NoError(t, err) - assert.Equal(t, int(maxResults), 5) - assert.Len(t, statuses, 5) - - assert.Equal(t, statuses[0].Context, "ci/awesomeness") - assert.Equal(t, statuses[0].State, CommitStatusPending) - assert.Equal(t, statuses[0].APIURL(), "TODO") - - assert.Equal(t, statuses[1].Context, "cov/awesomeness") - assert.Equal(t, statuses[1].State, CommitStatusWarning) - assert.Equal(t, statuses[1].APIURL(), "TODO") - - assert.Equal(t, statuses[2].Context, "cov/awesomeness") - assert.Equal(t, statuses[2].State, CommitStatusSuccess) - assert.Equal(t, statuses[2].APIURL(), "TODO") - - assert.Equal(t, statuses[3].Context, "ci/awesomeness") - assert.Equal(t, statuses[3].State, CommitStatusFailure) - assert.Equal(t, statuses[3].APIURL(), "TODO") - - assert.Equal(t, statuses[4].Context, "deploy/awesomeness") - assert.Equal(t, statuses[4].State, CommitStatusError) - assert.Equal(t, statuses[4].APIURL(), "TODO") -} -*/ diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index 3594f7e00ef0..9927ca0bf53e 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -166,10 +166,7 @@ func GetCommitStatusesByRef(ctx *context.APIContext) { } if len(refs) > 0 { - c, err := refs[0].Commit() - if err == nil { - sha = c.ID.String() //Replace SHA with ref tag - } + sha = refs[0].Object.String() //Replace SHA with ref id } getCommitStatuses(ctx, sha)