Add Organization Wide Labels (#10814)
* Add organization wide labels Implement organization wide labels similar to organization wide webhooks. This lets you create individual labels for organizations that can be used for all repos under that organization (so being able to reuse the same label across multiple repos). This makes it possible for small organizations with many repos to use labels effectively. Fixes #7406 * Add migration * remove comments * fix tests * Update options/locale/locale_en-US.ini Removed unused translation string * show org labels in issue search label filter * Use more clear var name * rename migration after merge from master * comment typo * update migration again after rebase with master * check for orgID <=0 per guillep2k review * fmt * Apply suggestions from code review Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * remove unused code * Make sure RepoID is 0 when searching orgID per code review * more changes/code review requests * More descriptive translation var per code review * func description/delete comment when issue label deleted instead of hiding it * remove comment * only use issues in that repo when calculating number of open issues for org label on repo label page * Add integration test for IssuesSearch API with labels * remove unused function * Update models/issue_label.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Use subquery in GetLabelIDsInReposByNames * Fix tests to use correct orgID * fix more tests * IssuesSearch api now uses new BuildLabelNamesIssueIDsCondition. Add a few more tests as well * update comment for clarity * Revert previous code change now that we can use the new BuildLabelNamesIssueIDsCondition * Don't sort repos by date in IssuesSearch API After much debugging I've found a strange issue where in some cases MySQL will return a different result than other enigines if a query is sorted by a null collumn. For example with our integration test data where we don't set updated_unix in repository fixtures: SELECT `id`, `owner_id`, `owner_name`, `lower_name`, `name`, `description`, `website`, `original_service_type`, `original_url`, `default_branch`, `num_watches`, `num_stars`, `num_forks`, `num_issues`, `num_closed_issues`, `num_pulls`, `num_closed_pulls`, `num_milestones`, `num_closed_milestones`, `is_private`, `is_empty`, `is_archived`, `is_mirror`, `status`, `is_fork`, `fork_id`, `is_template`, `template_id`, `size`, `is_fsck_enabled`, `close_issues_via_commit_in_any_branch`, `topics`, `avatar`, `created_unix`, `updated_unix` FROM `repository` ORDER BY updated_unix DESC LIMIT 15 OFFSET 45 Returns different results for MySQL than other engines. However, the similar query: SELECT `id`, `owner_id`, `owner_name`, `lower_name`, `name`, `description`, `website`, `original_service_type`, `original_url`, `default_branch`, `num_watches`, `num_stars`, `num_forks`, `num_issues`, `num_closed_issues`, `num_pulls`, `num_closed_pulls`, `num_milestones`, `num_closed_milestones`, `is_private`, `is_empty`, `is_archived`, `is_mirror`, `status`, `is_fork`, `fork_id`, `is_template`, `template_id`, `size`, `is_fsck_enabled`, `close_issues_via_commit_in_any_branch`, `topics`, `avatar`, `created_unix`, `updated_unix` FROM `repository` ORDER BY updated_unix DESC LIMIT 15 OFFSET 30 Returns the same results. This causes integration tests to fail on MySQL in certain cases but would never show up in a real installation. Since this API call always returns issues based on the optionally provided repo_priority_id or the issueID itself, there is no change to results by changing the repo sorting method used to get ids earlier in the function. * linter is back! * code review * remove now unused option * Fix newline at end of files * more unused code * update to master * check for matching ids before query * Update models/issue_label.go Co-Authored-By: 6543 <6543@obermui.de> * Update models/issue_label.go * update comments * Update routers/org/setting.go Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
@ -134,3 +134,74 @@ func TestAPIReplaceIssueLabels(t *testing.T) {
|
||||
models.AssertCount(t, &models.IssueLabel{IssueID: issue.ID}, 1)
|
||||
models.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: issue.ID, LabelID: label.ID})
|
||||
}
|
||||
|
||||
func TestAPIModifyOrgLabels(t *testing.T) {
|
||||
assert.NoError(t, models.LoadFixtures())
|
||||
|
||||
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
|
||||
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
|
||||
user := "user1"
|
||||
session := loginUser(t, user)
|
||||
token := getTokenForLoggedInUser(t, session)
|
||||
urlStr := fmt.Sprintf("/api/v1/orgs/%s/labels?token=%s", owner.Name, token)
|
||||
|
||||
// CreateLabel
|
||||
req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
|
||||
Name: "TestL 1",
|
||||
Color: "abcdef",
|
||||
Description: "test label",
|
||||
})
|
||||
resp := session.MakeRequest(t, req, http.StatusCreated)
|
||||
apiLabel := new(api.Label)
|
||||
DecodeJSON(t, resp, &apiLabel)
|
||||
dbLabel := models.AssertExistsAndLoadBean(t, &models.Label{ID: apiLabel.ID, OrgID: owner.ID}).(*models.Label)
|
||||
assert.EqualValues(t, dbLabel.Name, apiLabel.Name)
|
||||
assert.EqualValues(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color)
|
||||
|
||||
req = NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
|
||||
Name: "TestL 2",
|
||||
Color: "#123456",
|
||||
Description: "jet another test label",
|
||||
})
|
||||
session.MakeRequest(t, req, http.StatusCreated)
|
||||
req = NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
|
||||
Name: "WrongTestL",
|
||||
Color: "#12345g",
|
||||
})
|
||||
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
|
||||
|
||||
//ListLabels
|
||||
req = NewRequest(t, "GET", urlStr)
|
||||
resp = session.MakeRequest(t, req, http.StatusOK)
|
||||
var apiLabels []*api.Label
|
||||
DecodeJSON(t, resp, &apiLabels)
|
||||
assert.Len(t, apiLabels, 4)
|
||||
|
||||
//GetLabel
|
||||
singleURLStr := fmt.Sprintf("/api/v1/orgs/%s/labels/%d?token=%s", owner.Name, dbLabel.ID, token)
|
||||
req = NewRequest(t, "GET", singleURLStr)
|
||||
resp = session.MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, resp, &apiLabel)
|
||||
assert.EqualValues(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color)
|
||||
|
||||
//EditLabel
|
||||
newName := "LabelNewName"
|
||||
newColor := "09876a"
|
||||
newColorWrong := "09g76a"
|
||||
req = NewRequestWithJSON(t, "PATCH", singleURLStr, &api.EditLabelOption{
|
||||
Name: &newName,
|
||||
Color: &newColor,
|
||||
})
|
||||
resp = session.MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, resp, &apiLabel)
|
||||
assert.EqualValues(t, newColor, apiLabel.Color)
|
||||
req = NewRequestWithJSON(t, "PATCH", singleURLStr, &api.EditLabelOption{
|
||||
Color: &newColorWrong,
|
||||
})
|
||||
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
|
||||
|
||||
//DeleteLabel
|
||||
req = NewRequest(t, "DELETE", singleURLStr)
|
||||
resp = session.MakeRequest(t, req, http.StatusNoContent)
|
||||
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ func TestAPIEditIssue(t *testing.T) {
|
||||
assert.Equal(t, title, issueAfter.Title)
|
||||
}
|
||||
|
||||
func TestAPISearchIssue(t *testing.T) {
|
||||
func TestAPISearchIssues(t *testing.T) {
|
||||
defer prepareTestEnv(t)()
|
||||
|
||||
session := loginUser(t, "user2")
|
||||
@ -173,3 +173,66 @@ func TestAPISearchIssue(t *testing.T) {
|
||||
DecodeJSON(t, resp, &apiIssues)
|
||||
assert.Len(t, apiIssues, 1)
|
||||
}
|
||||
|
||||
func TestAPISearchIssuesWithLabels(t *testing.T) {
|
||||
defer prepareTestEnv(t)()
|
||||
|
||||
session := loginUser(t, "user1")
|
||||
token := getTokenForLoggedInUser(t, session)
|
||||
|
||||
link, _ := url.Parse("/api/v1/repos/issues/search")
|
||||
req := NewRequest(t, "GET", link.String())
|
||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||
var apiIssues []*api.Issue
|
||||
DecodeJSON(t, resp, &apiIssues)
|
||||
|
||||
assert.Len(t, apiIssues, 9)
|
||||
|
||||
query := url.Values{}
|
||||
query.Add("token", token)
|
||||
link.RawQuery = query.Encode()
|
||||
req = NewRequest(t, "GET", link.String())
|
||||
resp = session.MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, resp, &apiIssues)
|
||||
assert.Len(t, apiIssues, 9)
|
||||
|
||||
query.Add("labels", "label1")
|
||||
link.RawQuery = query.Encode()
|
||||
req = NewRequest(t, "GET", link.String())
|
||||
resp = session.MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, resp, &apiIssues)
|
||||
assert.Len(t, apiIssues, 2)
|
||||
|
||||
// multiple labels
|
||||
query.Set("labels", "label1,label2")
|
||||
link.RawQuery = query.Encode()
|
||||
req = NewRequest(t, "GET", link.String())
|
||||
resp = session.MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, resp, &apiIssues)
|
||||
assert.Len(t, apiIssues, 2)
|
||||
|
||||
// an org label
|
||||
query.Set("labels", "orglabel4")
|
||||
link.RawQuery = query.Encode()
|
||||
req = NewRequest(t, "GET", link.String())
|
||||
resp = session.MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, resp, &apiIssues)
|
||||
assert.Len(t, apiIssues, 1)
|
||||
|
||||
// org and repo label
|
||||
query.Set("labels", "label2,orglabel4")
|
||||
query.Add("state", "all")
|
||||
link.RawQuery = query.Encode()
|
||||
req = NewRequest(t, "GET", link.String())
|
||||
resp = session.MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, resp, &apiIssues)
|
||||
assert.Len(t, apiIssues, 2)
|
||||
|
||||
// org and repo label which share the same issue
|
||||
query.Set("labels", "label1,orglabel4")
|
||||
link.RawQuery = query.Encode()
|
||||
req = NewRequest(t, "GET", link.String())
|
||||
resp = session.MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, resp, &apiIssues)
|
||||
assert.Len(t, apiIssues, 2)
|
||||
}
|
||||
|
@ -1502,10 +1502,41 @@ func (err ErrTrackedTimeNotExist) Error() string {
|
||||
// |_______ (____ /___ /\___ >____/
|
||||
// \/ \/ \/ \/
|
||||
|
||||
// ErrRepoLabelNotExist represents a "RepoLabelNotExist" kind of error.
|
||||
type ErrRepoLabelNotExist struct {
|
||||
LabelID int64
|
||||
RepoID int64
|
||||
}
|
||||
|
||||
// IsErrRepoLabelNotExist checks if an error is a RepoErrLabelNotExist.
|
||||
func IsErrRepoLabelNotExist(err error) bool {
|
||||
_, ok := err.(ErrRepoLabelNotExist)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrRepoLabelNotExist) Error() string {
|
||||
return fmt.Sprintf("label does not exist [label_id: %d, repo_id: %d]", err.LabelID, err.RepoID)
|
||||
}
|
||||
|
||||
// ErrOrgLabelNotExist represents a "OrgLabelNotExist" kind of error.
|
||||
type ErrOrgLabelNotExist struct {
|
||||
LabelID int64
|
||||
OrgID int64
|
||||
}
|
||||
|
||||
// IsErrOrgLabelNotExist checks if an error is a OrgErrLabelNotExist.
|
||||
func IsErrOrgLabelNotExist(err error) bool {
|
||||
_, ok := err.(ErrOrgLabelNotExist)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrOrgLabelNotExist) Error() string {
|
||||
return fmt.Sprintf("label does not exist [label_id: %d, org_id: %d]", err.LabelID, err.OrgID)
|
||||
}
|
||||
|
||||
// ErrLabelNotExist represents a "LabelNotExist" kind of error.
|
||||
type ErrLabelNotExist struct {
|
||||
LabelID int64
|
||||
RepoID int64
|
||||
}
|
||||
|
||||
// IsErrLabelNotExist checks if an error is a ErrLabelNotExist.
|
||||
@ -1515,7 +1546,7 @@ func IsErrLabelNotExist(err error) bool {
|
||||
}
|
||||
|
||||
func (err ErrLabelNotExist) Error() string {
|
||||
return fmt.Sprintf("label does not exist [label_id: %d, repo_id: %d]", err.LabelID, err.RepoID)
|
||||
return fmt.Sprintf("label does not exist [label_id: %d]", err.LabelID)
|
||||
}
|
||||
|
||||
// _____ .__.__ __
|
||||
|
@ -12,3 +12,8 @@
|
||||
id: 3
|
||||
issue_id: 2
|
||||
label_id: 1
|
||||
|
||||
-
|
||||
id: 4
|
||||
issue_id: 2
|
||||
label_id: 4
|
||||
|
@ -1,6 +1,7 @@
|
||||
-
|
||||
id: 1
|
||||
repo_id: 1
|
||||
org_id: 0
|
||||
name: label1
|
||||
color: '#abcdef'
|
||||
num_issues: 2
|
||||
@ -9,7 +10,26 @@
|
||||
-
|
||||
id: 2
|
||||
repo_id: 1
|
||||
org_id: 0
|
||||
name: label2
|
||||
color: '#000000'
|
||||
num_issues: 1
|
||||
num_closed_issues: 1
|
||||
-
|
||||
id: 3
|
||||
repo_id: 0
|
||||
org_id: 3
|
||||
name: orglabel3
|
||||
color: '#abcdef'
|
||||
num_issues: 0
|
||||
num_closed_issues: 0
|
||||
|
||||
-
|
||||
id: 4
|
||||
repo_id: 0
|
||||
org_id: 3
|
||||
name: orglabel4
|
||||
color: '#000000'
|
||||
num_issues: 1
|
||||
num_closed_issues: 0
|
||||
|
||||
|
@ -459,7 +459,7 @@ func (issue *Issue) ClearLabels(doer *User) (err error) {
|
||||
return err
|
||||
}
|
||||
if !perm.CanWriteIssuesOrPulls(issue.IsPull) {
|
||||
return ErrLabelNotExist{}
|
||||
return ErrRepoLabelNotExist{}
|
||||
}
|
||||
|
||||
if err = issue.clearLabels(sess, doer); err != nil {
|
||||
@ -894,7 +894,7 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
|
||||
|
||||
for _, label := range labels {
|
||||
// Silently drop invalid labels.
|
||||
if label.RepoID != opts.Repo.ID {
|
||||
if label.RepoID != opts.Repo.ID && label.OrgID != opts.Repo.OwnerID {
|
||||
continue
|
||||
}
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -66,10 +66,10 @@ func TestGetLabelInRepoByName(t *testing.T) {
|
||||
assert.Equal(t, "label1", label.Name)
|
||||
|
||||
_, err = GetLabelInRepoByName(1, "")
|
||||
assert.True(t, IsErrLabelNotExist(err))
|
||||
assert.True(t, IsErrRepoLabelNotExist(err))
|
||||
|
||||
_, err = GetLabelInRepoByName(NonexistentID, "nonexistent")
|
||||
assert.True(t, IsErrLabelNotExist(err))
|
||||
assert.True(t, IsErrRepoLabelNotExist(err))
|
||||
}
|
||||
|
||||
func TestGetLabelInRepoByNames(t *testing.T) {
|
||||
@ -103,10 +103,10 @@ func TestGetLabelInRepoByID(t *testing.T) {
|
||||
assert.EqualValues(t, 1, label.ID)
|
||||
|
||||
_, err = GetLabelInRepoByID(1, -1)
|
||||
assert.True(t, IsErrLabelNotExist(err))
|
||||
assert.True(t, IsErrRepoLabelNotExist(err))
|
||||
|
||||
_, err = GetLabelInRepoByID(NonexistentID, NonexistentID)
|
||||
assert.True(t, IsErrLabelNotExist(err))
|
||||
assert.True(t, IsErrRepoLabelNotExist(err))
|
||||
}
|
||||
|
||||
func TestGetLabelsInRepoByIDs(t *testing.T) {
|
||||
@ -135,6 +135,107 @@ func TestGetLabelsByRepoID(t *testing.T) {
|
||||
testSuccess(1, "default", []int64{1, 2})
|
||||
}
|
||||
|
||||
// Org vrsions
|
||||
|
||||
func TestGetLabelInOrgByName(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
label, err := GetLabelInOrgByName(3, "orglabel3")
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 3, label.ID)
|
||||
assert.Equal(t, "orglabel3", label.Name)
|
||||
|
||||
_, err = GetLabelInOrgByName(3, "")
|
||||
assert.True(t, IsErrOrgLabelNotExist(err))
|
||||
|
||||
_, err = GetLabelInOrgByName(0, "orglabel3")
|
||||
assert.True(t, IsErrOrgLabelNotExist(err))
|
||||
|
||||
_, err = GetLabelInOrgByName(-1, "orglabel3")
|
||||
assert.True(t, IsErrOrgLabelNotExist(err))
|
||||
|
||||
_, err = GetLabelInOrgByName(NonexistentID, "nonexistent")
|
||||
assert.True(t, IsErrOrgLabelNotExist(err))
|
||||
}
|
||||
|
||||
func TestGetLabelInOrgByNames(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
labelIDs, err := GetLabelIDsInOrgByNames(3, []string{"orglabel3", "orglabel4"})
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Len(t, labelIDs, 2)
|
||||
|
||||
assert.Equal(t, int64(3), labelIDs[0])
|
||||
assert.Equal(t, int64(4), labelIDs[1])
|
||||
}
|
||||
|
||||
func TestGetLabelInOrgByNamesDiscardsNonExistentLabels(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
// orglabel99 doesn't exists.. See labels.yml
|
||||
labelIDs, err := GetLabelIDsInOrgByNames(3, []string{"orglabel3", "orglabel4", "orglabel99"})
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Len(t, labelIDs, 2)
|
||||
|
||||
assert.Equal(t, int64(3), labelIDs[0])
|
||||
assert.Equal(t, int64(4), labelIDs[1])
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestGetLabelInOrgByID(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
label, err := GetLabelInOrgByID(3, 3)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 3, label.ID)
|
||||
|
||||
_, err = GetLabelInOrgByID(3, -1)
|
||||
assert.True(t, IsErrOrgLabelNotExist(err))
|
||||
|
||||
_, err = GetLabelInOrgByID(0, 3)
|
||||
assert.True(t, IsErrOrgLabelNotExist(err))
|
||||
|
||||
_, err = GetLabelInOrgByID(-1, 3)
|
||||
assert.True(t, IsErrOrgLabelNotExist(err))
|
||||
|
||||
_, err = GetLabelInOrgByID(NonexistentID, NonexistentID)
|
||||
assert.True(t, IsErrOrgLabelNotExist(err))
|
||||
}
|
||||
|
||||
func TestGetLabelsInOrgByIDs(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
labels, err := GetLabelsInOrgByIDs(3, []int64{3, 4, NonexistentID})
|
||||
assert.NoError(t, err)
|
||||
if assert.Len(t, labels, 2) {
|
||||
assert.EqualValues(t, 3, labels[0].ID)
|
||||
assert.EqualValues(t, 4, labels[1].ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetLabelsByOrgID(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
testSuccess := func(orgID int64, sortType string, expectedIssueIDs []int64) {
|
||||
labels, err := GetLabelsByOrgID(orgID, sortType, ListOptions{})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, labels, len(expectedIssueIDs))
|
||||
for i, label := range labels {
|
||||
assert.EqualValues(t, expectedIssueIDs[i], label.ID)
|
||||
}
|
||||
}
|
||||
testSuccess(3, "leastissues", []int64{3, 4})
|
||||
testSuccess(3, "mostissues", []int64{4, 3})
|
||||
testSuccess(3, "reversealphabetically", []int64{4, 3})
|
||||
testSuccess(3, "default", []int64{3, 4})
|
||||
|
||||
var err error
|
||||
_, err = GetLabelsByOrgID(0, "leastissues", ListOptions{})
|
||||
assert.True(t, IsErrOrgLabelNotExist(err))
|
||||
|
||||
_, err = GetLabelsByOrgID(-1, "leastissues", ListOptions{})
|
||||
assert.True(t, IsErrOrgLabelNotExist(err))
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
func TestGetLabelsByIssueID(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
labels, err := GetLabelsByIssueID(1)
|
||||
@ -166,7 +267,7 @@ func TestDeleteLabel(t *testing.T) {
|
||||
AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID})
|
||||
|
||||
assert.NoError(t, DeleteLabel(label.RepoID, label.ID))
|
||||
AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID})
|
||||
AssertNotExistsBean(t, &Label{ID: label.ID})
|
||||
|
||||
assert.NoError(t, DeleteLabel(NonexistentID, NonexistentID))
|
||||
CheckConsistencyFor(t, &Label{}, &Repository{})
|
||||
|
@ -34,7 +34,6 @@ func TestIssueList_LoadAttributes(t *testing.T) {
|
||||
setting.Service.EnableTimetracking = true
|
||||
issueList := IssueList{
|
||||
AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue),
|
||||
AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue),
|
||||
AssertExistsAndLoadBean(t, &Issue{ID: 4}).(*Issue),
|
||||
}
|
||||
|
||||
|
@ -202,6 +202,8 @@ var migrations = []Migration{
|
||||
NewMigration("Add EmailHash Table", addEmailHashTable),
|
||||
// v134 -> v135
|
||||
NewMigration("Refix merge base for merged pull requests", refixMergeBase),
|
||||
// v135 -> 136
|
||||
NewMigration("Add OrgID column to Labels table", addOrgIDLabelColumn),
|
||||
}
|
||||
|
||||
// Migrate database to current version
|
||||
|
22
models/migrations/v135.go
Normal file
22
models/migrations/v135.go
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func addOrgIDLabelColumn(x *xorm.Engine) error {
|
||||
type Label struct {
|
||||
OrgID int64 `xorm:"INDEX"`
|
||||
}
|
||||
|
||||
if err := x.Sync2(new(Label)); err != nil {
|
||||
return fmt.Errorf("Sync2: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
@ -58,7 +58,7 @@ func CreateRepository(doer, u *models.User, opts models.CreateRepoOptions) (_ *m
|
||||
|
||||
// Initialize Issue Labels if selected
|
||||
if len(opts.IssueLabels) > 0 {
|
||||
if err = models.InitializeLabels(ctx, repo.ID, opts.IssueLabels); err != nil {
|
||||
if err = models.InitializeLabels(ctx, repo.ID, opts.IssueLabels, false); err != nil {
|
||||
return fmt.Errorf("InitializeLabels: %v", err)
|
||||
}
|
||||
}
|
||||
|
@ -45,8 +45,10 @@ func MockContext(t *testing.T, path string) *context.Context {
|
||||
func LoadRepo(t *testing.T, ctx *context.Context, repoID int64) {
|
||||
ctx.Repo = &context.Repository{}
|
||||
ctx.Repo.Repository = models.AssertExistsAndLoadBean(t, &models.Repository{ID: repoID}).(*models.Repository)
|
||||
ctx.Repo.RepoLink = ctx.Repo.Repository.Link()
|
||||
var err error
|
||||
ctx.Repo.Owner, err = models.GetUserByID(ctx.Repo.Repository.OwnerID)
|
||||
assert.NoError(t, err)
|
||||
ctx.Repo.RepoLink = ctx.Repo.Repository.Link()
|
||||
ctx.Repo.Permission, err = models.GetUserRepoPermission(ctx.Repo.Repository, ctx.User)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
@ -725,6 +725,9 @@ tags = Tags
|
||||
issues = Issues
|
||||
pulls = Pull Requests
|
||||
labels = Labels
|
||||
org_labels_desc = Organization level labels that can be used with <strong>all repositories</strong> under this organization
|
||||
org_labels_desc_manage = manage
|
||||
|
||||
milestones = Milestones
|
||||
commits = Commits
|
||||
commit = Commit
|
||||
@ -1699,6 +1702,8 @@ settings.delete_org_title = Delete Organization
|
||||
settings.delete_org_desc = This organization will be deleted permanently. Continue?
|
||||
settings.hooks_desc = Add webhooks which will be triggered for <strong>all repositories</strong> under this organization.
|
||||
|
||||
settings.labels_desc = Add labels which can be used on issues for <strong>all repositories</strong> under this organization.
|
||||
|
||||
members.membership_visibility = Membership Visibility:
|
||||
members.public = Visible
|
||||
members.public_helper = make hidden
|
||||
|
@ -861,6 +861,13 @@ func RegisterRoutes(m *macaron.Macaron) {
|
||||
Post(reqOrgOwnership(), bind(api.CreateTeamOption{}), org.CreateTeam)
|
||||
m.Get("/search", org.SearchTeam)
|
||||
}, reqOrgMembership())
|
||||
m.Group("/labels", func() {
|
||||
m.Get("", org.ListLabels)
|
||||
m.Post("", reqToken(), reqOrgOwnership(), bind(api.CreateLabelOption{}), org.CreateLabel)
|
||||
m.Combo("/:id").Get(org.GetLabel).
|
||||
Patch(reqToken(), reqOrgOwnership(), bind(api.EditLabelOption{}), org.EditLabel).
|
||||
Delete(reqToken(), reqOrgOwnership(), org.DeleteLabel)
|
||||
})
|
||||
m.Group("/hooks", func() {
|
||||
m.Combo("").Get(org.ListHooks).
|
||||
Post(bind(api.CreateHookOption{}), org.CreateHook)
|
||||
|
237
routers/api/v1/org/label.go
Normal file
237
routers/api/v1/org/label.go
Normal file
@ -0,0 +1,237 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package org
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/convert"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/routers/api/v1/utils"
|
||||
)
|
||||
|
||||
// ListLabels list all the labels of an organization
|
||||
func ListLabels(ctx *context.APIContext) {
|
||||
// swagger:operation GET /orgs/{org}/labels organization orgListLabels
|
||||
// ---
|
||||
// summary: List an organization's labels
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: org
|
||||
// in: path
|
||||
// description: name of the organization
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: page
|
||||
// in: query
|
||||
// description: page number of results to return (1-based)
|
||||
// type: integer
|
||||
// - name: limit
|
||||
// in: query
|
||||
// description: page size of results, maximum page size is 50
|
||||
// type: integer
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/LabelList"
|
||||
|
||||
labels, err := models.GetLabelsByOrgID(ctx.Org.Organization.ID, ctx.Query("sort"), utils.GetListOptions(ctx))
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetLabelsByOrgID", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, convert.ToLabelList(labels))
|
||||
}
|
||||
|
||||
// CreateLabel create a label for a repository
|
||||
func CreateLabel(ctx *context.APIContext, form api.CreateLabelOption) {
|
||||
// swagger:operation POST /orgs/{org}/labels organization orgCreateLabel
|
||||
// ---
|
||||
// summary: Create a label for an organization
|
||||
// consumes:
|
||||
// - application/json
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: org
|
||||
// in: path
|
||||
// description: name of the organization
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: body
|
||||
// in: body
|
||||
// schema:
|
||||
// "$ref": "#/definitions/CreateLabelOption"
|
||||
// responses:
|
||||
// "201":
|
||||
// "$ref": "#/responses/Label"
|
||||
// "422":
|
||||
// "$ref": "#/responses/validationError"
|
||||
|
||||
form.Color = strings.Trim(form.Color, " ")
|
||||
if len(form.Color) == 6 {
|
||||
form.Color = "#" + form.Color
|
||||
}
|
||||
if !models.LabelColorPattern.MatchString(form.Color) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "ColorPattern", fmt.Errorf("bad color code: %s", form.Color))
|
||||
return
|
||||
}
|
||||
|
||||
label := &models.Label{
|
||||
Name: form.Name,
|
||||
Color: form.Color,
|
||||
OrgID: ctx.Org.Organization.ID,
|
||||
Description: form.Description,
|
||||
}
|
||||
if err := models.NewLabel(label); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "NewLabel", err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusCreated, convert.ToLabel(label))
|
||||
}
|
||||
|
||||
// GetLabel get label by organization and label id
|
||||
func GetLabel(ctx *context.APIContext) {
|
||||
// swagger:operation GET /orgs/{org}/labels/{id} organization orgGetLabel
|
||||
// ---
|
||||
// summary: Get a single label
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: org
|
||||
// in: path
|
||||
// description: name of the organization
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: id
|
||||
// in: path
|
||||
// description: id of the label to get
|
||||
// type: integer
|
||||
// format: int64
|
||||
// required: true
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/Label"
|
||||
|
||||
var (
|
||||
label *models.Label
|
||||
err error
|
||||
)
|
||||
strID := ctx.Params(":id")
|
||||
if intID, err2 := strconv.ParseInt(strID, 10, 64); err2 != nil {
|
||||
label, err = models.GetLabelInOrgByName(ctx.Org.Organization.ID, strID)
|
||||
} else {
|
||||
label, err = models.GetLabelInOrgByID(ctx.Org.Organization.ID, intID)
|
||||
}
|
||||
if err != nil {
|
||||
if models.IsErrOrgLabelNotExist(err) {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "GetLabelByOrgID", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, convert.ToLabel(label))
|
||||
}
|
||||
|
||||
// EditLabel modify a label for an Organization
|
||||
func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
|
||||
// swagger:operation PATCH /orgs/{org}/labels/{id} organization orgEditLabel
|
||||
// ---
|
||||
// summary: Update a label
|
||||
// consumes:
|
||||
// - application/json
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: org
|
||||
// in: path
|
||||
// description: name of the organization
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: id
|
||||
// in: path
|
||||
// description: id of the label to edit
|
||||
// type: integer
|
||||
// format: int64
|
||||
// required: true
|
||||
// - name: body
|
||||
// in: body
|
||||
// schema:
|
||||
// "$ref": "#/definitions/EditLabelOption"
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/Label"
|
||||
// "422":
|
||||
// "$ref": "#/responses/validationError"
|
||||
|
||||
label, err := models.GetLabelInOrgByID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
|
||||
if err != nil {
|
||||
if models.IsErrOrgLabelNotExist(err) {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "GetLabelByRepoID", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if form.Name != nil {
|
||||
label.Name = *form.Name
|
||||
}
|
||||
if form.Color != nil {
|
||||
label.Color = strings.Trim(*form.Color, " ")
|
||||
if len(label.Color) == 6 {
|
||||
label.Color = "#" + label.Color
|
||||
}
|
||||
if !models.LabelColorPattern.MatchString(label.Color) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "ColorPattern", fmt.Errorf("bad color code: %s", label.Color))
|
||||
return
|
||||
}
|
||||
}
|
||||
if form.Description != nil {
|
||||
label.Description = *form.Description
|
||||
}
|
||||
if err := models.UpdateLabel(label); err != nil {
|
||||
ctx.ServerError("UpdateLabel", err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, convert.ToLabel(label))
|
||||
}
|
||||
|
||||
// DeleteLabel delete a label for an organization
|
||||
func DeleteLabel(ctx *context.APIContext) {
|
||||
// swagger:operation DELETE /orgs/{org}/labels/{id} organization orgDeleteLabel
|
||||
// ---
|
||||
// summary: Delete a label
|
||||
// parameters:
|
||||
// - name: org
|
||||
// in: path
|
||||
// description: name of the organization
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: id
|
||||
// in: path
|
||||
// description: id of the label to delete
|
||||
// type: integer
|
||||
// format: int64
|
||||
// required: true
|
||||
// responses:
|
||||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
|
||||
if err := models.DeleteLabel(ctx.Org.Organization.ID, ctx.ParamsInt64(":id")); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "DeleteLabel", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
@ -81,8 +81,10 @@ func SearchIssues(ctx *context.APIContext) {
|
||||
AllPublic: true,
|
||||
TopicOnly: false,
|
||||
Collaborate: util.OptionalBoolNone,
|
||||
OrderBy: models.SearchOrderByRecentUpdated,
|
||||
Actor: ctx.User,
|
||||
// This needs to be a column that is not nil in fixtures or
|
||||
// MySQL will return different results when sorting by null in some cases
|
||||
OrderBy: models.SearchOrderByAlphabetically,
|
||||
Actor: ctx.User,
|
||||
}
|
||||
if ctx.IsSigned {
|
||||
opts.Private = true
|
||||
@ -152,6 +154,7 @@ func SearchIssues(ctx *context.APIContext) {
|
||||
Page: ctx.QueryInt("page"),
|
||||
PageSize: setting.UI.IssuePagingNum,
|
||||
},
|
||||
|
||||
RepoIDs: repoIDs,
|
||||
IsClosed: isClosed,
|
||||
IssueIDs: issueIDs,
|
||||
|
@ -171,12 +171,12 @@ func DeleteIssueLabel(ctx *context.APIContext) {
|
||||
return
|
||||
}
|
||||
|
||||
label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
|
||||
label, err := models.GetLabelByID(ctx.ParamsInt64(":id"))
|
||||
if err != nil {
|
||||
if models.IsErrLabelNotExist(err) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", err)
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "GetLabelInRepoByID", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetLabelByID", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -308,9 +308,9 @@ func prepareForReplaceOrAdd(ctx *context.APIContext, form api.IssueLabelsOption)
|
||||
return
|
||||
}
|
||||
|
||||
labels, err = models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
|
||||
labels, err = models.GetLabelsByIDs(form.Labels)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetLabelsInRepoByIDs", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetLabelsByIDs", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -96,7 +96,7 @@ func GetLabel(ctx *context.APIContext) {
|
||||
label, err = models.GetLabelInRepoByID(ctx.Repo.Repository.ID, intID)
|
||||
}
|
||||
if err != nil {
|
||||
if models.IsErrLabelNotExist(err) {
|
||||
if models.IsErrRepoLabelNotExist(err) {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "GetLabelByRepoID", err)
|
||||
@ -197,7 +197,7 @@ func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
|
||||
|
||||
label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
|
||||
if err != nil {
|
||||
if models.IsErrLabelNotExist(err) {
|
||||
if models.IsErrRepoLabelNotExist(err) {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "GetLabelByRepoID", err)
|
||||
|
106
routers/org/org_labels.go
Normal file
106
routers/org/org_labels.go
Normal file
@ -0,0 +1,106 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package org
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/auth"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
)
|
||||
|
||||
// RetrieveLabels find all the labels of an organization
|
||||
func RetrieveLabels(ctx *context.Context) {
|
||||
labels, err := models.GetLabelsByOrgID(ctx.Org.Organization.ID, ctx.Query("sort"), models.ListOptions{})
|
||||
if err != nil {
|
||||
ctx.ServerError("RetrieveLabels.GetLabels", err)
|
||||
return
|
||||
}
|
||||
for _, l := range labels {
|
||||
l.CalOpenIssues()
|
||||
}
|
||||
ctx.Data["Labels"] = labels
|
||||
ctx.Data["NumLabels"] = len(labels)
|
||||
ctx.Data["SortType"] = ctx.Query("sort")
|
||||
}
|
||||
|
||||
// NewLabel create new label for organization
|
||||
func NewLabel(ctx *context.Context, form auth.CreateLabelForm) {
|
||||
ctx.Data["Title"] = ctx.Tr("repo.labels")
|
||||
ctx.Data["PageIsLabels"] = true
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
|
||||
ctx.Redirect(ctx.Org.OrgLink + "/settings/labels")
|
||||
return
|
||||
}
|
||||
|
||||
l := &models.Label{
|
||||
OrgID: ctx.Org.Organization.ID,
|
||||
Name: form.Title,
|
||||
Description: form.Description,
|
||||
Color: form.Color,
|
||||
}
|
||||
if err := models.NewLabel(l); err != nil {
|
||||
ctx.ServerError("NewLabel", err)
|
||||
return
|
||||
}
|
||||
ctx.Redirect(ctx.Org.OrgLink + "/settings/labels")
|
||||
}
|
||||
|
||||
// UpdateLabel update a label's name and color
|
||||
func UpdateLabel(ctx *context.Context, form auth.CreateLabelForm) {
|
||||
l, err := models.GetLabelInOrgByID(ctx.Org.Organization.ID, form.ID)
|
||||
if err != nil {
|
||||
switch {
|
||||
case models.IsErrOrgLabelNotExist(err):
|
||||
ctx.Error(404)
|
||||
default:
|
||||
ctx.ServerError("UpdateLabel", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
l.Name = form.Title
|
||||
l.Description = form.Description
|
||||
l.Color = form.Color
|
||||
if err := models.UpdateLabel(l); err != nil {
|
||||
ctx.ServerError("UpdateLabel", err)
|
||||
return
|
||||
}
|
||||
ctx.Redirect(ctx.Org.OrgLink + "/settings/labels")
|
||||
}
|
||||
|
||||
// DeleteLabel delete a label
|
||||
func DeleteLabel(ctx *context.Context) {
|
||||
if err := models.DeleteLabel(ctx.Org.Organization.ID, ctx.QueryInt64("id")); err != nil {
|
||||
ctx.Flash.Error("DeleteLabel: " + err.Error())
|
||||
} else {
|
||||
ctx.Flash.Success(ctx.Tr("repo.issues.label_deletion_success"))
|
||||
}
|
||||
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"redirect": ctx.Org.OrgLink + "/settings/labels",
|
||||
})
|
||||
}
|
||||
|
||||
// InitializeLabels init labels for an organization
|
||||
func InitializeLabels(ctx *context.Context, form auth.InitializeLabelsForm) {
|
||||
if ctx.HasError() {
|
||||
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
|
||||
return
|
||||
}
|
||||
|
||||
if err := models.InitializeLabels(models.DefaultDBContext(), ctx.Org.Organization.ID, form.TemplateName, true); err != nil {
|
||||
if models.IsErrIssueLabelTemplateLoad(err) {
|
||||
originalErr := err.(models.ErrIssueLabelTemplateLoad).OriginalError
|
||||
ctx.Flash.Error(ctx.Tr("repo.issues.label_templates.fail_to_load_file", form.TemplateName, originalErr))
|
||||
ctx.Redirect(ctx.Org.OrgLink + "/settings/labels")
|
||||
return
|
||||
}
|
||||
ctx.ServerError("InitializeLabels", err)
|
||||
return
|
||||
}
|
||||
ctx.Redirect(ctx.Org.OrgLink + "/settings/labels")
|
||||
}
|
@ -24,6 +24,8 @@ const (
|
||||
tplSettingsDelete base.TplName = "org/settings/delete"
|
||||
// tplSettingsHooks template path for render hook settings
|
||||
tplSettingsHooks base.TplName = "org/settings/hooks"
|
||||
// tplSettingsLabels template path for render labels settings
|
||||
tplSettingsLabels base.TplName = "org/settings/labels"
|
||||
)
|
||||
|
||||
// Settings render the main settings page
|
||||
@ -177,3 +179,13 @@ func DeleteWebhook(ctx *context.Context) {
|
||||
"redirect": ctx.Org.OrgLink + "/settings/hooks",
|
||||
})
|
||||
}
|
||||
|
||||
// Labels render organization labels page
|
||||
func Labels(ctx *context.Context) {
|
||||
ctx.Data["Title"] = ctx.Tr("repo.labels")
|
||||
ctx.Data["PageIsOrgSettingsLabels"] = true
|
||||
ctx.Data["RequireMinicolors"] = true
|
||||
ctx.Data["RequireTribute"] = true
|
||||
ctx.Data["LabelTemplates"] = models.LabelTemplates
|
||||
ctx.HTML(200, tplSettingsLabels)
|
||||
}
|
||||
|
@ -335,7 +335,6 @@ func PrepareCompareDiff(
|
||||
} else {
|
||||
title = headBranch
|
||||
}
|
||||
|
||||
ctx.Data["title"] = title
|
||||
ctx.Data["Username"] = headUser.Name
|
||||
ctx.Data["Reponame"] = headRepo.Name
|
||||
|
@ -259,6 +259,18 @@ func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalB
|
||||
ctx.ServerError("GetLabelsByRepoID", err)
|
||||
return
|
||||
}
|
||||
|
||||
if repo.Owner.IsOrganization() {
|
||||
orgLabels, err := models.GetLabelsByOrgID(repo.Owner.ID, ctx.Query("sort"), models.ListOptions{})
|
||||
if err != nil {
|
||||
ctx.ServerError("GetLabelsByOrgID", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["OrgLabels"] = orgLabels
|
||||
labels = append(labels, orgLabels...)
|
||||
}
|
||||
|
||||
for _, l := range labels {
|
||||
l.LoadSelectedLabelsAfterClick(labelIDs)
|
||||
}
|
||||
@ -377,6 +389,15 @@ func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository, isPull boo
|
||||
return nil
|
||||
}
|
||||
ctx.Data["Labels"] = labels
|
||||
if repo.Owner.IsOrganization() {
|
||||
orgLabels, err := models.GetLabelsByOrgID(repo.Owner.ID, ctx.Query("sort"), models.ListOptions{})
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
ctx.Data["OrgLabels"] = orgLabels
|
||||
labels = append(labels, orgLabels...)
|
||||
}
|
||||
|
||||
RetrieveRepoMilestonesAndAssignees(ctx, repo)
|
||||
if ctx.Written() {
|
||||
@ -593,6 +614,7 @@ func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) {
|
||||
Content: form.Content,
|
||||
Ref: form.Ref,
|
||||
}
|
||||
|
||||
if err := issue_service.NewIssue(repo, issue, labelIDs, attachments, assigneeIDs); err != nil {
|
||||
if models.IsErrUserDoesNotHaveAccessToRepo(err) {
|
||||
ctx.Error(400, "UserDoesNotHaveAccessToRepo", err.Error())
|
||||
@ -761,6 +783,19 @@ func ViewIssue(ctx *context.Context) {
|
||||
ctx.ServerError("GetLabelsByRepoID", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Labels"] = labels
|
||||
|
||||
if repo.Owner.IsOrganization() {
|
||||
orgLabels, err := models.GetLabelsByOrgID(repo.Owner.ID, ctx.Query("sort"), models.ListOptions{})
|
||||
if err != nil {
|
||||
ctx.ServerError("GetLabelsByOrgID", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["OrgLabels"] = orgLabels
|
||||
|
||||
labels = append(labels, orgLabels...)
|
||||
}
|
||||
|
||||
hasSelected := false
|
||||
for i := range labels {
|
||||
if labelIDMark[labels[i].ID] {
|
||||
@ -769,7 +804,6 @@ func ViewIssue(ctx *context.Context) {
|
||||
}
|
||||
}
|
||||
ctx.Data["HasSelectedLabel"] = hasSelected
|
||||
ctx.Data["Labels"] = labels
|
||||
|
||||
// Check milestone and assignee.
|
||||
if ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
issue_service "code.gitea.io/gitea/services/issue"
|
||||
)
|
||||
|
||||
@ -35,7 +36,7 @@ func InitializeLabels(ctx *context.Context, form auth.InitializeLabelsForm) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := models.InitializeLabels(models.DefaultDBContext(), ctx.Repo.Repository.ID, form.TemplateName); err != nil {
|
||||
if err := models.InitializeLabels(models.DefaultDBContext(), ctx.Repo.Repository.ID, form.TemplateName, false); err != nil {
|
||||
if models.IsErrIssueLabelTemplateLoad(err) {
|
||||
originalErr := err.(models.ErrIssueLabelTemplateLoad).OriginalError
|
||||
ctx.Flash.Error(ctx.Tr("repo.issues.label_templates.fail_to_load_file", form.TemplateName, originalErr))
|
||||
@ -48,17 +49,47 @@ func InitializeLabels(ctx *context.Context, form auth.InitializeLabelsForm) {
|
||||
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
|
||||
}
|
||||
|
||||
// RetrieveLabels find all the labels of a repository
|
||||
// RetrieveLabels find all the labels of a repository and organization
|
||||
func RetrieveLabels(ctx *context.Context) {
|
||||
labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, ctx.Query("sort"), models.ListOptions{})
|
||||
if err != nil {
|
||||
ctx.ServerError("RetrieveLabels.GetLabels", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, l := range labels {
|
||||
l.CalOpenIssues()
|
||||
}
|
||||
|
||||
ctx.Data["Labels"] = labels
|
||||
|
||||
if ctx.Repo.Owner.IsOrganization() {
|
||||
orgLabels, err := models.GetLabelsByOrgID(ctx.Repo.Owner.ID, ctx.Query("sort"), models.ListOptions{})
|
||||
if err != nil {
|
||||
ctx.ServerError("GetLabelsByOrgID", err)
|
||||
return
|
||||
}
|
||||
for _, l := range orgLabels {
|
||||
l.CalOpenOrgIssues(ctx.Repo.Repository.ID, l.ID)
|
||||
}
|
||||
ctx.Data["OrgLabels"] = orgLabels
|
||||
|
||||
org, err := models.GetOrgByName(ctx.Repo.Owner.LowerName)
|
||||
if err != nil {
|
||||
ctx.ServerError("GetOrgByName", err)
|
||||
return
|
||||
}
|
||||
if ctx.User != nil {
|
||||
ctx.Org.IsOwner, err = org.IsOwnedBy(ctx.User.ID)
|
||||
if err != nil {
|
||||
ctx.ServerError("org.IsOwnedBy", err)
|
||||
return
|
||||
}
|
||||
ctx.Org.OrgLink = setting.AppSubURL + "/org/" + org.LowerName
|
||||
ctx.Data["IsOrganizationOwner"] = ctx.Org.IsOwner
|
||||
ctx.Data["OrganizationLink"] = ctx.Org.OrgLink
|
||||
}
|
||||
}
|
||||
ctx.Data["NumLabels"] = len(labels)
|
||||
ctx.Data["SortType"] = ctx.Query("sort")
|
||||
}
|
||||
@ -89,10 +120,10 @@ func NewLabel(ctx *context.Context, form auth.CreateLabelForm) {
|
||||
|
||||
// UpdateLabel update a label's name and color
|
||||
func UpdateLabel(ctx *context.Context, form auth.CreateLabelForm) {
|
||||
l, err := models.GetLabelByID(form.ID)
|
||||
l, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, form.ID)
|
||||
if err != nil {
|
||||
switch {
|
||||
case models.IsErrLabelNotExist(err):
|
||||
case models.IsErrRepoLabelNotExist(err):
|
||||
ctx.Error(404)
|
||||
default:
|
||||
ctx.ServerError("UpdateLabel", err)
|
||||
@ -141,7 +172,7 @@ func UpdateIssueLabel(ctx *context.Context) {
|
||||
case "attach", "detach", "toggle":
|
||||
label, err := models.GetLabelByID(ctx.QueryInt64("id"))
|
||||
if err != nil {
|
||||
if models.IsErrLabelNotExist(err) {
|
||||
if models.IsErrRepoLabelNotExist(err) {
|
||||
ctx.Error(404, "GetLabelByID")
|
||||
} else {
|
||||
ctx.ServerError("GetLabelByID", err)
|
||||
|
@ -594,6 +594,14 @@ func RegisterRoutes(m *macaron.Macaron) {
|
||||
m.Post("/feishu/:id", bindIgnErr(auth.NewFeishuHookForm{}), repo.FeishuHooksEditPost)
|
||||
})
|
||||
|
||||
m.Group("/labels", func() {
|
||||
m.Get("", org.RetrieveLabels, org.Labels)
|
||||
m.Post("/new", bindIgnErr(auth.CreateLabelForm{}), org.NewLabel)
|
||||
m.Post("/edit", bindIgnErr(auth.CreateLabelForm{}), org.UpdateLabel)
|
||||
m.Post("/delete", org.DeleteLabel)
|
||||
m.Post("/initialize", bindIgnErr(auth.InitializeLabelsForm{}), org.InitializeLabels)
|
||||
})
|
||||
|
||||
m.Route("/delete", "GET,POST", org.SettingsDelete)
|
||||
})
|
||||
}, context.OrgAssignment(true, true))
|
||||
|
@ -51,7 +51,10 @@ func RemoveLabel(issue *models.Issue, doer *models.User, label *models.Label) er
|
||||
return err
|
||||
}
|
||||
if !perm.CanWriteIssuesOrPulls(issue.IsPull) {
|
||||
return models.ErrLabelNotExist{}
|
||||
if label.OrgID > 0 {
|
||||
return models.ErrOrgLabelNotExist{}
|
||||
}
|
||||
return models.ErrRepoLabelNotExist{}
|
||||
}
|
||||
|
||||
if err := models.DeleteIssueLabel(issue, label, doer); err != nil {
|
||||
|
29
templates/org/settings/labels.tmpl
Normal file
29
templates/org/settings/labels.tmpl
Normal file
@ -0,0 +1,29 @@
|
||||
{{template "base/head" .}}
|
||||
<div class="organization settings labels">
|
||||
{{template "org/header" .}}
|
||||
<div class="ui container">
|
||||
<div class="ui grid">
|
||||
{{template "org/settings/navbar" .}}
|
||||
<div class="ui twelve wide column content">
|
||||
<div class="ui grid">
|
||||
<div class="left floated twelve wide column">
|
||||
{{$.i18n.Tr "org.settings.labels_desc" | Str2html}}
|
||||
</div>
|
||||
<div class="right floated three wide column">
|
||||
<div class="ui right">
|
||||
<div class="ui green new-label button">{{.i18n.Tr "repo.issues.new_label"}}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="ui divider"></div>
|
||||
{{template "repo/issue/labels/label_new" .}}
|
||||
{{template "base/alert" .}}
|
||||
{{template "repo/issue/labels/label_list" .}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{template "repo/issue/labels/edit_delete_label" .}}
|
||||
{{template "base/footer" .}}
|
@ -7,6 +7,9 @@
|
||||
<a class="{{if .PageIsSettingsHooks}}active{{end}} item" href="{{.OrgLink}}/settings/hooks">
|
||||
{{.i18n.Tr "repo.settings.hooks"}}
|
||||
</a>
|
||||
<a class="{{if .PageIsOrgSettingsLabels}}active{{end}} item" href="{{.OrgLink}}/settings/labels">
|
||||
{{.i18n.Tr "repo.labels"}}
|
||||
</a>
|
||||
<a class="{{if .PageIsSettingsDelete}}active{{end}} item" href="{{.OrgLink}}/settings/delete">
|
||||
{{.i18n.Tr "org.settings.delete"}}
|
||||
</a>
|
||||
|
@ -10,173 +10,17 @@
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
{{if not .Repository.IsArchived}}
|
||||
<div class="ui new-label segment hide">
|
||||
<form class="ui form" action="{{$.RepoLink}}/labels/new" method="post">
|
||||
{{.CsrfTokenHtml}}
|
||||
<div class="ui grid">
|
||||
<div class="three wide column">
|
||||
<div class="ui small input">
|
||||
<input class="new-label-input emoji-input" name="title" placeholder="{{.i18n.Tr "repo.issues.new_label_placeholder"}}" autofocus required maxlength="50">
|
||||
</div>
|
||||
</div>
|
||||
<div class="five wide column">
|
||||
<div class="ui small fluid input">
|
||||
<input class="new-label-desc-input" name="description" placeholder="{{.i18n.Tr "repo.issues.new_label_desc_placeholder"}}" maxlength="200">
|
||||
</div>
|
||||
</div>
|
||||
<div class="color picker column">
|
||||
<input class="color-picker" name="color" value="#70c24a" required maxlength="7">
|
||||
</div>
|
||||
<div class="column precolors">
|
||||
{{template "repo/issue/label_precolors"}}
|
||||
</div>
|
||||
<div class="buttons">
|
||||
<div class="ui blue small basic cancel button">{{.i18n.Tr "repo.milestones.cancel"}}</div>
|
||||
<button class="ui green small button">{{.i18n.Tr "repo.issues.create_label"}}</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
{{end}}
|
||||
<div class="ui divider"></div>
|
||||
|
||||
<div class="ui right floated secondary filter menu">
|
||||
<!-- Sort -->
|
||||
<div class="ui dropdown type jump item">
|
||||
<span class="text">
|
||||
{{.i18n.Tr "repo.issues.filter_sort"}}
|
||||
<i class="dropdown icon"></i>
|
||||
</span>
|
||||
<div class="menu">
|
||||
<a class="{{if or (eq .SortType "alphabetically") (not .SortType)}}active{{end}} item" href="{{$.Link}}?sort=alphabetically&state={{$.State}}">{{.i18n.Tr "repo.issues.label.filter_sort.alphabetically"}}</a>
|
||||
<a class="{{if eq .SortType "reversealphabetically"}}active{{end}} item" href="{{$.Link}}?sort=reversealphabetically&state={{$.State}}">{{.i18n.Tr "repo.issues.label.filter_sort.reverse_alphabetically"}}</a>
|
||||
<a class="{{if eq .SortType "leastissues"}}active{{end}} item" href="{{$.Link}}?sort=leastissues&state={{$.State}}">{{.i18n.Tr "repo.milestones.filter_sort.least_issues"}}</a>
|
||||
<a class="{{if eq .SortType "mostissues"}}active{{end}} item" href="{{$.Link}}?sort=mostissues&state={{$.State}}">{{.i18n.Tr "repo.milestones.filter_sort.most_issues"}}</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{if and (or .CanWriteIssues .CanWritePulls) (not .Repository.IsArchived)}}
|
||||
{{template "repo/issue/labels/label_new" .}}
|
||||
{{end}}
|
||||
{{template "base/alert" .}}
|
||||
<div class="ui black basic label">{{.i18n.Tr "repo.issues.label_count" .NumLabels}}</div>
|
||||
<div class="label list">
|
||||
{{if and (or $.CanWriteIssues $.CanWritePulls) (eq .NumLabels 0) (not $.Repository.IsArchived) }}
|
||||
<div class="ui centered grid">
|
||||
<div class="twelve wide column eight wide computer column">
|
||||
<div class="ui attached left aligned segment">
|
||||
<!-- <h4 class="ui header">
|
||||
{{.i18n.Tr "repo.issues.label_templates.title"}}
|
||||
<a target="_blank" rel="noopener noreferrer"
|
||||
href="https://discuss.gogs.io/t/how-to-use-predefined-label-templates/599">
|
||||
<span class="octicon octicon-question"></span>
|
||||
</a>
|
||||
</h4> -->
|
||||
<p>{{.i18n.Tr "repo.issues.label_templates.info"}}</p>
|
||||
<br/>
|
||||
<form class="ui form center" action="{{.Link}}/initialize" method="post">
|
||||
{{.CsrfTokenHtml}}
|
||||
<div class="field">
|
||||
<div class="ui selection dropdown">
|
||||
<input type="hidden" name="template_name" value="Default">
|
||||
<div class="default text">{{.i18n.Tr "repo.issues.label_templates.helper"}}</div>
|
||||
<div class="menu">
|
||||
{{range $template, $labels := .LabelTemplates}}
|
||||
<div class="item" data-value="{{$template}}">{{$template}}<br/><i>({{$labels}})</i></div>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit" class="ui blue button">{{.i18n.Tr "repo.issues.label_templates.use"}}</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
<div class="ui divider"></div>
|
||||
|
||||
{{range .Labels}}
|
||||
<li class="item">
|
||||
<div class="ui grid">
|
||||
<div class="three wide column">
|
||||
<div class="ui label has-emoji" style="color: {{.ForegroundColor}}; background-color: {{.Color}}">{{svg "octicon-tag" 16}} {{.Name}}</div>
|
||||
</div>
|
||||
<div class="seven wide column">
|
||||
{{.Description}}
|
||||
</div>
|
||||
<div class="three wide column">
|
||||
<a class="ui right open-issues" href="{{$.RepoLink}}/issues?labels={{.ID}}">{{svg "octicon-issue-opened" 16}} {{$.i18n.Tr "repo.issues.label_open_issues" .NumOpenIssues}}</a>
|
||||
</div>
|
||||
<div class="three wide column">
|
||||
{{if and (not $.Repository.IsArchived) (or $.CanWriteIssues $.CanWritePulls)}}
|
||||
<a class="ui right delete-button" href="#" data-url="{{$.RepoLink}}/labels/delete" data-id="{{.ID}}">{{svg "octicon-trashcan" 16}} {{$.i18n.Tr "repo.issues.label_delete"}}</a>
|
||||
<a class="ui right edit-label-button" href="#" data-id="{{.ID}}" data-title="{{.Name}}" data-description="{{.Description}}" data-color={{.Color}}>{{svg "octicon-pencil" 16}} {{$.i18n.Tr "repo.issues.label_edit"}}</a>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
{{end}}
|
||||
</div>
|
||||
{{template "repo/issue/labels/label_list" .}}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{if and (or .CanWriteIssues .CanWritePulls) (not .Repository.IsArchived)}}
|
||||
<div class="ui small basic delete modal">
|
||||
<div class="ui icon header">
|
||||
<i class="trash icon"></i>
|
||||
{{.i18n.Tr "repo.issues.label_deletion"}}
|
||||
</div>
|
||||
<div class="content">
|
||||
<p>{{.i18n.Tr "repo.issues.label_deletion_desc"}}</p>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<div class="ui red basic inverted cancel button">
|
||||
<i class="remove icon"></i>
|
||||
{{.i18n.Tr "modal.no"}}
|
||||
</div>
|
||||
<div class="ui green basic inverted ok button">
|
||||
<i class="checkmark icon"></i>
|
||||
{{.i18n.Tr "modal.yes"}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="ui small edit-label modal">
|
||||
<div class="header">
|
||||
{{.i18n.Tr "repo.issues.label_modify"}}
|
||||
</div>
|
||||
<div class="content">
|
||||
<form class="ui edit-label form" action="{{$.RepoLink}}/labels/edit" method="post">
|
||||
{{.CsrfTokenHtml}}
|
||||
<input id="label-modal-id" name="id" type="hidden">
|
||||
<div class="ui grid">
|
||||
<div class="three wide column">
|
||||
<div class="ui small input">
|
||||
<input class="new-label-input emoji-input" name="title" placeholder="{{.i18n.Tr "repo.issues.new_label_placeholder"}}" autofocus required maxlength="50">
|
||||
</div>
|
||||
</div>
|
||||
<div class="five wide column">
|
||||
<div class="ui small fluid input">
|
||||
<input class="new-label-desc-input" name="description" placeholder="{{.i18n.Tr "repo.issues.new_label_desc_placeholder"}}" maxlength="200">
|
||||
</div>
|
||||
</div>
|
||||
<div class="color picker column">
|
||||
<input class="color-picker" name="color" value="#70c24a" required maxlength="7">
|
||||
</div>
|
||||
<div class="column precolors">
|
||||
{{template "repo/issue/label_precolors"}}
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<div class="ui negative button">
|
||||
{{.i18n.Tr "modal.no"}}
|
||||
</div>
|
||||
<div class="ui positive right labeled icon button">
|
||||
{{.i18n.Tr "modal.modify"}}
|
||||
<i class="checkmark icon"></i>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{if and (or .CanWriteIssues .CanWritePulls) (not .Repository.IsArchived) }}
|
||||
{{template "repo/issue/labels/edit_delete_label" .}}
|
||||
{{end}}
|
||||
</div>
|
||||
{{template "base/footer" .}}
|
||||
|
59
templates/repo/issue/labels/edit_delete_label.tmpl
Normal file
59
templates/repo/issue/labels/edit_delete_label.tmpl
Normal file
@ -0,0 +1,59 @@
|
||||
<div class="ui small basic delete modal">
|
||||
<div class="ui icon header">
|
||||
<i class="trash icon"></i>
|
||||
{{.i18n.Tr "repo.issues.label_deletion"}}
|
||||
</div>
|
||||
<div class="content">
|
||||
<p>{{.i18n.Tr "repo.issues.label_deletion_desc"}}</p>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<div class="ui red basic inverted cancel button">
|
||||
<i class="remove icon"></i>
|
||||
{{.i18n.Tr "modal.no"}}
|
||||
</div>
|
||||
<div class="ui green basic inverted ok button">
|
||||
<i class="checkmark icon"></i>
|
||||
{{.i18n.Tr "modal.yes"}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="ui small edit-label modal">
|
||||
<div class="header">
|
||||
{{.i18n.Tr "repo.issues.label_modify"}}
|
||||
</div>
|
||||
<div class="content">
|
||||
<form class="ui edit-label form" action="{{$.Link}}/edit" method="post">
|
||||
{{.CsrfTokenHtml}}
|
||||
<input id="label-modal-id" name="id" type="hidden">
|
||||
<div class="ui grid">
|
||||
<div class="three wide column">
|
||||
<div class="ui small input">
|
||||
<input class="new-label-input emoji-input" name="title" placeholder="{{.i18n.Tr "repo.issues.new_label_placeholder"}}" autofocus required maxlength="50">
|
||||
</div>
|
||||
</div>
|
||||
<div class="five wide column">
|
||||
<div class="ui small fluid input">
|
||||
<input class="new-label-desc-input" name="description" placeholder="{{.i18n.Tr "repo.issues.new_label_desc_placeholder"}}" maxlength="200">
|
||||
</div>
|
||||
</div>
|
||||
<div class="color picker column">
|
||||
<input class="color-picker" name="color" value="#70c24a" required maxlength="7">
|
||||
</div>
|
||||
<div class="column precolors">
|
||||
{{template "repo/issue/label_precolors"}}
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<div class="ui negative button">
|
||||
{{.i18n.Tr "modal.no"}}
|
||||
</div>
|
||||
<div class="ui positive right labeled icon button">
|
||||
{{.i18n.Tr "modal.modify"}}
|
||||
<i class="checkmark icon"></i>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user