Skip to content
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

Fix #9189 - API Allow only specific Colums to be updated on Issue #9539

Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e5c26f3
dont insert "-1" in any case to issue.poster_id
6543 Dec 29, 2019
4c8c595
Make sure API cant override importand fields
6543 Dec 29, 2019
27ceb65
code format
6543 Dec 29, 2019
c6b0666
fix lint
6543 Dec 29, 2019
06c0072
Merge branch 'master' into fix-9189_API_dont-update-posterID-if-ghost…
6543 Dec 29, 2019
65c3f67
WIP test
6543 Dec 29, 2019
651a357
Merge branch 'master' into fix-9189_API_dont-update-posterID-if-ghost…
lafriks Dec 29, 2019
6d84cb1
add missing poster_id
6543 Dec 29, 2019
e7ef8d6
fix test
6543 Dec 29, 2019
695ca42
user.IsGhost handle nil
6543 Dec 29, 2019
aa0d09d
CI.restart()
6543 Dec 29, 2019
cb94942
make sure no -1 is realy added
6543 Dec 29, 2019
2244b58
CI.restart()
6543 Dec 29, 2019
1cc1831
@lunny suggestion remove some not allowed fields
6543 Dec 30, 2019
6442205
seperate issue.LoadMilestone
6543 Dec 30, 2019
6642286
load milestone and return it on IssueEdit via API
6543 Dec 30, 2019
45d8921
extend Test for TestAPIEditIssue
6543 Dec 30, 2019
245dd3c
fix fixtures
6543 Dec 30, 2019
8894b3a
Merge branch 'master' into fix-9189_API_dont-update-posterID-if-ghost…
6543 Dec 30, 2019
6cbeb66
declare allowedColumnsUpdateIssueByAPI only once
6543 Dec 30, 2019
d65920d
Merge branch 'master' into fix-9189_API_dont-update-posterID-if-ghost…
6543 Dec 31, 2019
ee1d278
Merge branch 'master' into fix-9189_API_dont-update-posterID-if-ghost…
6543 Jan 1, 2020
58a56d0
Update Year
6543 Jan 1, 2020
342f542
no var just write id drecty into func cal
6543 Jan 1, 2020
6416e81
Merge branch 'master' into fix-9189_API_dont-update-posterID-if-ghost…
6543 Jan 1, 2020
File filter

Filter by extension

Filter by extension

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

func TestAPIEditIssue(t *testing.T) {
defer prepareTestEnv(t)()

issueBefore := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 10}).(*models.Issue)
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issueBefore.RepoID}).(*models.Repository)
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
assert.NoError(t, issueBefore.LoadAttributes())

session := loginUser(t, owner.Name)
token := getTokenForLoggedInUser(t, session)

issueState := "closed"

urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d?token=%s", owner.Name, repo.Name, issueBefore.Index, token)
req := NewRequestWithJSON(t, "PATCH", urlStr, api.EditIssueOption{
State: &issueState,
// ToDo change more
})
resp := session.MakeRequest(t, req, http.StatusCreated)
var apiIssue api.Issue
DecodeJSON(t, resp, &apiIssue)

issueAfter := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 10}).(*models.Issue)

assert.Equal(t, api.StateOpen, issueBefore.State())
assert.Equal(t, api.StateClosed, issueAfter.State())
// check deleted user
assert.Equal(t, int64(500), issueAfter.PosterID)
assert.NoError(t, issueAfter.LoadAttributes())
assert.Equal(t, int64(-1), issueAfter.PosterID)
assert.Equal(t, int64(-1), issueBefore.PosterID)
assert.Equal(t, int64(-1), apiIssue.Poster.ID)

}
14 changes: 13 additions & 1 deletion models/fixtures/issue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,16 @@
is_closed: false
is_pull: true
created_unix: 946684820
updated_unix: 978307180
updated_unix: 978307180

-
id: 10
repo_id: 42
index: 1
poster_id: 500
name: issue from deleted account
content: content from deleted account
is_closed: false
is_pull: false
created_unix: 946684830
updated_unix: 999307200
8 changes: 4 additions & 4 deletions models/fixtures/repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@
is_private: false
num_stars: 0
num_forks: 0
num_issues: 0
num_issues: 1
is_mirror: false

-
Expand Down Expand Up @@ -588,7 +588,7 @@
is_mirror: false
status: 0

-
-
id: 46
owner_id: 26
lower_name: repo_external_tracker
Expand All @@ -600,7 +600,7 @@
is_mirror: false
status: 0

-
-
id: 47
owner_id: 26
lower_name: repo_external_tracker_numeric
Expand All @@ -612,7 +612,7 @@
is_mirror: false
status: 0

-
-
id: 48
owner_id: 26
lower_name: repo_external_tracker_alpha
Expand Down
30 changes: 20 additions & 10 deletions models/issue.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
6543 marked this conversation as resolved.
Show resolved Hide resolved
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -1568,25 +1569,34 @@ func SearchIssueIDsByKeyword(kw string, repoIDs []int64, limit, start int) (int6
return total, ids, nil
}

func updateIssue(e Engine, issue *Issue) error {
_, err := e.ID(issue.ID).AllCols().Update(issue)
if err != nil {
return err
}
return nil
func updateIssueByCols(e Engine, issue *Issue, columns ...string) (err error) {
_, err = e.ID(issue.ID).Cols(columns...).Update(issue)
return
}

// UpdateIssue updates all fields of given issue.
func UpdateIssue(issue *Issue) error {
// UpdateIssueByAPI updates all allowed fields of given issue.
func UpdateIssueByAPI(issue *Issue) error {
// allowed fields to update
columns := []string{"name", "is_closed", "content", "milestone_id",
"priority", "num_comments", "ref", "deadline_unix", "created_unix",
6543 marked this conversation as resolved.
Show resolved Hide resolved
"updated_unix", "closed_unix", "is_locked"}

sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}
if err := updateIssue(sess, issue); err != nil {
if err := issue.loadPoster(sess); err != nil {
return err
}
if err := issue.loadPoster(sess); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said on backport, loadPoster should not be removed.

if !issue.Poster.IsGhost() {
6543 marked this conversation as resolved.
Show resolved Hide resolved
if issue.PosterID <= 0 {
return fmt.Errorf("Issue %d can't be updated with PosterID %d", issue.Index, issue.PosterID)
}
columns = append(columns, "poster_id")
}

if err := updateIssueByCols(sess, issue, columns...); err != nil {
return err
}
if err := issue.addCrossReferences(sess, issue.Poster, true); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,14 @@ func NewGhostUser() *User {
}
}

// IsGhost check if user is fake user for a deleted account
func (u *User) IsGhost() bool {
if u == nil {
return false
}
return u.ID == -1 && u.Name == "Ghost"
6543 marked this conversation as resolved.
Show resolved Hide resolved
}

var (
reservedUsernames = []string{
"attachments",
Expand Down
4 changes: 2 additions & 2 deletions routers/api/v1/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,8 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
}
}

if err = models.UpdateIssue(issue); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateIssue", err)
if err = models.UpdateIssueByAPI(issue); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateIssueByAPI", err)
return
}
if form.State != nil {
Expand Down
4 changes: 2 additions & 2 deletions routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,8 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
}
}

if err = models.UpdateIssue(issue); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateIssue", err)
if err = models.UpdateIssueByAPI(issue); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateIssueByAPI", err)
return
}
if form.State != nil {
Expand Down