BugFix: updateLabel can updated not allowed fields #11242 (#11242)

This commit is contained in:
6543 2020-04-29 01:28:56 +02:00 committed by GitHub
parent 4563eb873d
commit 6b6f20b6d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 9 deletions

View file

@ -609,7 +609,7 @@ func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed, isMergeP
return nil, err return nil, err
} }
for idx := range issue.Labels { for idx := range issue.Labels {
if err = updateLabel(e, issue.Labels[idx]); err != nil { if err = updateLabelCols(e, issue.Labels[idx], "num_issues", "num_closed_issue"); err != nil {
return nil, err return nil, err
} }
} }

View file

@ -246,7 +246,7 @@ func UpdateLabel(l *Label) error {
if !LabelColorPattern.MatchString(l.Color) { if !LabelColorPattern.MatchString(l.Color) {
return fmt.Errorf("bad color code: %s", l.Color) return fmt.Errorf("bad color code: %s", l.Color)
} }
return updateLabel(x, l) return updateLabelCols(x, l, "name", "description", "color")
} }
// DeleteLabel delete a label // DeleteLabel delete a label
@ -587,7 +587,7 @@ func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
return getLabelsByIssueID(x, issueID) return getLabelsByIssueID(x, issueID)
} }
func updateLabel(e Engine, l *Label) error { func updateLabelCols(e Engine, l *Label, cols ...string) error {
_, err := e.ID(l.ID). _, err := e.ID(l.ID).
SetExpr("num_issues", SetExpr("num_issues",
builder.Select("count(*)").From("issue_label"). builder.Select("count(*)").From("issue_label").
@ -601,7 +601,7 @@ func updateLabel(e Engine, l *Label) error {
"issue.is_closed": true, "issue.is_closed": true,
}), }),
). ).
AllCols().Update(l) Cols(cols...).Update(l)
return err return err
} }
@ -653,7 +653,7 @@ func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err
return err return err
} }
return updateLabel(e, label) return updateLabelCols(e, label, "num_issues", "num_closed_issue")
} }
// NewIssueLabel creates a new issue-label relation. // NewIssueLabel creates a new issue-label relation.
@ -729,7 +729,7 @@ func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (
return err return err
} }
return updateLabel(e, label) return updateLabelCols(e, label, "num_issues", "num_closed_issue")
} }
// DeleteIssueLabel deletes issue-label relation. // DeleteIssueLabel deletes issue-label relation.

View file

@ -252,9 +252,16 @@ func TestGetLabelsByIssueID(t *testing.T) {
func TestUpdateLabel(t *testing.T) { func TestUpdateLabel(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
label := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) label := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
label.Color = "#ffff00" // make sure update wont overwrite it
label.Name = "newLabelName" update := &Label{
assert.NoError(t, UpdateLabel(label)) ID: label.ID,
Color: "#ffff00",
Name: "newLabelName",
Description: label.Description,
}
label.Color = update.Color
label.Name = update.Name
assert.NoError(t, UpdateLabel(update))
newLabel := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) newLabel := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
assert.Equal(t, *label, *newLabel) assert.Equal(t, *label, *newLabel)
CheckConsistencyFor(t, &Label{}, &Repository{}) CheckConsistencyFor(t, &Label{}, &Repository{})