Skip to content

Implement index parameter on api/create issue #7876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions integrations/api_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,86 @@ func TestAPICreateIssue(t *testing.T) {
Title: title,
})
}

func TestAPICreateIssueID(t *testing.T) {
prepareTestEnv(t)
const body, firstTitle, dupTitle, freeTitle, notAllowedTitle = "apiTestBody", "apiTestTitle-first", "apiTestTitle-dup", "apiTestTitle-free", "apiTestTitle-notAllowed"
const firstIndex = int64(3000)

repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
admin := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)

assert.True(t, admin.IsAdmin)
assert.False(t, owner.IsAdmin)

session := loginUser(t, admin.Name)
token := getTokenForLoggedInUser(t, session)
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues?state=all&token=%s", owner.Name, repo.Name, token)

// Must create with index 3000
req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueOption{
Index: firstIndex,
Body: body,
Title: firstTitle,
Assignee: owner.Name,
})
resp := session.MakeRequest(t, req, http.StatusCreated)
var apiIssue api.Issue
DecodeJSON(t, resp, &apiIssue)
assert.Equal(t, apiIssue.Index, firstIndex)
assert.Equal(t, apiIssue.Body, body)
assert.Equal(t, apiIssue.Title, firstTitle)

// Must fail
req = NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueOption{
Index: firstIndex,
Body: body,
Title: dupTitle,
Assignee: owner.Name,
})
resp = session.MakeRequest(t, req, http.StatusInternalServerError)

// Must be the first one created
models.AssertExistsAndLoadBean(t, &models.Issue{
Index: firstIndex,
RepoID: repo.ID,
AssigneeID: owner.ID,
Content: body,
Title: firstTitle,
})

// Must create with index firstIndex + 1
req = NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueOption{
Body: body,
Title: freeTitle,
Assignee: owner.Name,
})
resp = session.MakeRequest(t, req, http.StatusCreated)
DecodeJSON(t, resp, &apiIssue)
assert.Equal(t, apiIssue.Index, firstIndex+1)
assert.Equal(t, apiIssue.Body, body)
assert.Equal(t, apiIssue.Title, freeTitle)

// Must be the last one created
models.AssertExistsAndLoadBean(t, &models.Issue{
Index: firstIndex + 1,
RepoID: repo.ID,
AssigneeID: owner.ID,
Content: body,
Title: freeTitle,
})

session = loginUser(t, owner.Name)
token = getTokenForLoggedInUser(t, session)
urlStr = fmt.Sprintf("/api/v1/repos/%s/%s/issues?state=all&token=%s", owner.Name, repo.Name, token)

// Must fail create with index
req = NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueOption{
Index: firstIndex + 2,
Body: body,
Title: notAllowedTitle,
Assignee: owner.Name,
})
resp = session.MakeRequest(t, req, http.StatusBadRequest)
}
17 changes: 13 additions & 4 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1055,11 +1055,20 @@ func getMaxIndexOfIssue(e Engine, repoID int64) (int64, error) {
func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
opts.Issue.Title = strings.TrimSpace(opts.Issue.Title)

maxIndex, err := getMaxIndexOfIssue(e, opts.Issue.RepoID)
if err != nil {
return err
if opts.Issue.Index == 0 {
maxIndex, err := getMaxIndexOfIssue(e, opts.Issue.RepoID)
if err != nil {
return err
}
opts.Issue.Index = maxIndex + 1
} else if !doer.IsAdmin {
// Require admin to specify Issue.Index
return ErrUserDoesNotHaveAccessToRepo{UserID: doer.ID, RepoName: opts.Repo.Name}
}

if opts.Issue.Index < 1 {
return fmt.Errorf("Issue number out of range or max issue number reached")
}
opts.Issue.Index = maxIndex + 1

if opts.Issue.MilestoneID > 0 {
milestone, err := getMilestoneByRepoID(e, opts.Issue.RepoID, opts.Issue.MilestoneID)
Expand Down
106 changes: 106 additions & 0 deletions models/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,109 @@ func TestIssue_SearchIssueIDsByKeyword(t *testing.T) {
assert.EqualValues(t, 1, total)
assert.EqualValues(t, []int64{1}, ids)
}

func TestIssueCreateWithID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

repo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
admin := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
notadmin := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
index := int64(3000)
assert.True(t, admin.IsAdmin)
assert.False(t, notadmin.IsAdmin)

issue := &Issue{
Index: index,
RepoID: repo.ID,
Repo: repo,
Title: "TestIssueCreateWithID",
PosterID: admin.ID,
Poster: admin,
Content: "Issue body",
}

err := NewIssue(repo, issue, nil, nil, nil)
assert.NoError(t, err)

issue = &Issue{
Index: index,
RepoID: repo.ID,
Repo: repo,
Title: "duplicate TestIssueCreateWithID",
PosterID: admin.ID,
Poster: admin,
Content: "Issue body",
}

err = NewIssue(repo, issue, nil, nil, nil)
assert.Error(t, err)

issue = AssertExistsAndLoadBean(t, &Issue{RepoID: repo.ID, Index: index}).(*Issue)

assert.Equal(t, "TestIssueCreateWithID", issue.Title)

issue = &Issue{
Index: -1,
RepoID: repo.ID,
Repo: repo,
Title: "neg index TestIssueCreateWithID",
PosterID: admin.ID,
Poster: admin,
Content: "Issue body",
}

err = NewIssue(repo, issue, nil, nil, nil)
assert.Error(t, err)

issue = &Issue{
RepoID: repo.ID,
Repo: repo,
Title: "sequential TestIssueCreateWithID",
PosterID: notadmin.ID,
Poster: notadmin,
Content: "Issue body",
}

err = NewIssue(repo, issue, nil, nil, nil)
assert.NoError(t, err)
assert.Equal(t, index+1, issue.Index)

issue = &Issue{
Index: index + 2,
RepoID: repo.ID,
Repo: repo,
Title: "not admin TestIssueCreateWithID",
PosterID: notadmin.ID,
Poster: notadmin,
Content: "Issue body",
}

expectedError := ErrUserDoesNotHaveAccessToRepo{UserID: notadmin.ID, RepoName: repo.Name}.Error()
err = NewIssue(repo, issue, nil, nil, nil)
assert.EqualError(t, err, expectedError)

issue = &Issue{
Index: 0x7fffffffffffffff,
RepoID: repo.ID,
Repo: repo,
Title: "index barely in range TestIssueCreateWithID",
PosterID: admin.ID,
Poster: admin,
Content: "Issue body",
}

err = NewIssue(repo, issue, nil, nil, nil)
assert.NoError(t, err)

issue = &Issue{
RepoID: repo.ID,
Repo: repo,
Title: "out of index space TestIssueCreateWithID",
PosterID: notadmin.ID,
Poster: notadmin,
Content: "Issue body",
}

err = NewIssue(repo, issue, nil, nil, nil)
assert.Error(t, err)
}
1 change: 1 addition & 0 deletions modules/structs/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type ListIssueOption struct {

// CreateIssueOption options to create one issue
type CreateIssueOption struct {
Index int64 `json:"index"`
// required:true
Title string `json:"title" binding:"Required"`
Body string `json:"body"`
Expand Down
1 change: 1 addition & 0 deletions routers/api/v1/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) {
}

issue := &models.Issue{
Index: form.Index,
RepoID: ctx.Repo.Repository.ID,
Repo: ctx.Repo.Repository,
Title: form.Title,
Expand Down
7 changes: 0 additions & 7 deletions routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,8 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
deadlineUnix = timeutil.TimeStamp(form.Deadline.Unix())
}

maxIndex, err := models.GetMaxIndexOfIssue(repo.ID)
if err != nil {
ctx.ServerError("GetPatch", err)
return
}

prIssue := &models.Issue{
RepoID: repo.ID,
Index: maxIndex + 1,
Title: form.Title,
PosterID: ctx.User.ID,
Poster: ctx.User,
Expand Down
5 changes: 5 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7350,6 +7350,11 @@
"format": "date-time",
"x-go-name": "Deadline"
},
"index": {
"type": "integer",
"format": "int64",
"x-go-name": "Index"
},
"labels": {
"description": "list of label ids",
"type": "array",
Expand Down