Skip to content

Commit

Permalink
Fix new release creation API to allow empty target (#5870)
Browse files Browse the repository at this point in the history
* Fix new release creation API to allow empty target

* Add more test cases

* Update swagger
  • Loading branch information
lafriks authored and techknowlogick committed Jan 30, 2019
1 parent 8b5f6ce commit fada696
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 348 deletions.
13 changes: 2 additions & 11 deletions Gopkg.lock

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

81 changes: 58 additions & 23 deletions integrations/api_releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,16 @@ import (
"github.com/stretchr/testify/assert"
)

func TestAPICreateRelease(t *testing.T) {
prepareTestEnv(t)

repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
session := loginUser(t, owner.LowerName)
token := getTokenForLoggedInUser(t, session)
gitRepo, err := git.OpenRepository(repo.RepoPath())
assert.NoError(t, err)

err = gitRepo.CreateTag("v0.0.1", "master")
assert.NoError(t, err)

commitID, err := gitRepo.GetTagCommitID("v0.0.1")
assert.NoError(t, err)

func createNewReleaseUsingAPI(t *testing.T, session *TestSession, token string, owner *models.User, repo *models.Repository, name, target, title, desc string) *api.Release {
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/releases?token=%s",
owner.Name, repo.Name, token)
req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateReleaseOption{
TagName: "v0.0.1",
Title: "v0.0.1",
Note: "test",
TagName: name,
Title: title,
Note: desc,
IsDraft: false,
IsPrerelease: false,
Target: commitID,
Target: target,
})
resp := session.MakeRequest(t, req, http.StatusCreated)

Expand All @@ -53,10 +38,32 @@ func TestAPICreateRelease(t *testing.T) {
Note: newRelease.Note,
})

urlStr = fmt.Sprintf("/api/v1/repos/%s/%s/releases/%d?token=%s",
return &newRelease
}

func TestAPICreateAndUpdateRelease(t *testing.T) {
prepareTestEnv(t)

repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
session := loginUser(t, owner.LowerName)
token := getTokenForLoggedInUser(t, session)

gitRepo, err := git.OpenRepository(repo.RepoPath())
assert.NoError(t, err)

err = gitRepo.CreateTag("v0.0.1", "master")
assert.NoError(t, err)

target, err := gitRepo.GetTagCommitID("v0.0.1")
assert.NoError(t, err)

newRelease := createNewReleaseUsingAPI(t, session, token, owner, repo, "v0.0.1", target, "v0.0.1", "test")

urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/releases/%d?token=%s",
owner.Name, repo.Name, newRelease.ID, token)
req = NewRequest(t, "GET", urlStr)
resp = session.MakeRequest(t, req, http.StatusOK)
req := NewRequest(t, "GET", urlStr)
resp := session.MakeRequest(t, req, http.StatusOK)

var release api.Release
DecodeJSON(t, resp, &release)
Expand All @@ -83,3 +90,31 @@ func TestAPICreateRelease(t *testing.T) {
Note: newRelease.Note,
})
}

func TestAPICreateReleaseToDefaultBranch(t *testing.T) {
prepareTestEnv(t)

repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
session := loginUser(t, owner.LowerName)
token := getTokenForLoggedInUser(t, session)

createNewReleaseUsingAPI(t, session, token, owner, repo, "v0.0.1", "", "v0.0.1", "test")
}

func TestAPICreateReleaseToDefaultBranchOnExistingTag(t *testing.T) {
prepareTestEnv(t)

repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
session := loginUser(t, owner.LowerName)
token := getTokenForLoggedInUser(t, session)

gitRepo, err := git.OpenRepository(repo.RepoPath())
assert.NoError(t, err)

err = gitRepo.CreateTag("v0.0.1", "master")
assert.NoError(t, err)

createNewReleaseUsingAPI(t, session, token, owner, repo, "v0.0.1", "", "v0.0.1", "test")
}
4 changes: 4 additions & 0 deletions routers/api/v1/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) {
ctx.ServerError("GetRelease", err)
return
}
// If target is not provided use default branch
if len(form.Target) == 0 {
form.Target = ctx.Repo.Repository.DefaultBranch
}
rel = &models.Release{
RepoID: ctx.Repo.Repository.ID,
PublisherID: ctx.User.ID,
Expand Down
21 changes: 21 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7570,6 +7570,13 @@
"description": "PayloadCommit represents a commit",
"type": "object",
"properties": {
"added": {
"type": "array",
"items": {
"type": "string"
},
"x-go-name": "Added"
},
"author": {
"$ref": "#/definitions/PayloadUser"
},
Expand All @@ -7585,6 +7592,20 @@
"type": "string",
"x-go-name": "Message"
},
"modified": {
"type": "array",
"items": {
"type": "string"
},
"x-go-name": "Modified"
},
"removed": {
"type": "array",
"items": {
"type": "string"
},
"x-go-name": "Removed"
},
"timestamp": {
"type": "string",
"format": "date-time",
Expand Down
6 changes: 5 additions & 1 deletion vendor/code.gitea.io/sdk/gitea/gitea.go

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

3 changes: 3 additions & 0 deletions vendor/code.gitea.io/sdk/gitea/hook.go

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

2 changes: 1 addition & 1 deletion vendor/code.gitea.io/sdk/gitea/release.go

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

48 changes: 48 additions & 0 deletions vendor/code.gitea.io/sdk/gitea/repo_commit.go

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

26 changes: 26 additions & 0 deletions vendor/code.gitea.io/sdk/gitea/repo_tag.go

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

Loading

0 comments on commit fada696

Please sign in to comment.