2022-06-13 12:37:59 +03:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-06-13 12:37:59 +03:00
|
|
|
|
|
|
|
package issues_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2023-08-14 12:56:14 +03:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2022-06-13 12:37:59 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 22:41:10 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-13 12:37:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLabel_CalOpenIssues(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 05:22:25 +03:00
|
|
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1})
|
2022-06-13 12:37:59 +03:00
|
|
|
label.CalOpenIssues()
|
|
|
|
assert.EqualValues(t, 2, label.NumOpenIssues)
|
|
|
|
}
|
|
|
|
|
Optimization of labels handling in issue_search (#4228)
This PR optimizes the SQL query and de-duplicate the labels' ids when generating the query string, on the issue page.
<hr/>
### Background
Some time ago, BingBot and some other crawlers have been putting my instance on its knees with requests containing a lot of label ids, like this one :
```
[07/Aug/2023:11:28:37 +0200] "GET /Dolibarr/sendrecurringinvoicebymail/issues?q=&type=all&sort=&state=closed&labels=1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c2%2c10%2c2%2c1%2c1%2c10%2c10%2c7%2c6%2c10%2c10%2c3%2c2%2c1%2c5%2c10%2c1%2c6%2c2%2c7%2c3%2c7%2c6%2c10%2c1%2c10%2c1%2c1%2c7%2c7%2c1%2c1%2c1%2c1%2c10%2c10%2c1%2c2%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c2%2c1%2c12%2c6%2c6%2c10&milestone=0&project=-1&poster=0 HTTP/1.1" 499 0 "-" "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm) Chrome/103.0.5060.134 Safari/537.36"
```
Since each of the label ids implies a join, it grows exponentially expensive for the database engine (at least on PostgreSQL but SQLite suffers a little too).
Thus, this PR proposes two enhancements:
* rewrite the database query to use only one squashed condition,
* deduplicate the label ids when generating the URL.
### Performance comparison
Here are some timings on Postgresql-backed, Forgejo 7.0.4 instances :
```sh
$ time curl -s -o /dev/null "http://localhost:3000/toto/tata/issues?q=&type=all&sort=&labels=19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25&state=open&milestone=0&project=0&assignee=0&poster=0"
real 0m10,491s
user 0m0,017s
sys 0m0,008s
```
...and with the patch:
```sh
$ time curl -s -o /dev/null "http://localhost:3000/toto/tata/issues?q=&type=all&sort=&labels=19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25&state=open&milestone=0&project=0&assignee=0&poster=0"
real 0m0,094s
user 0m0,012s
sys 0m0,013s
```
### Annex
This issue was originally proposed to [Gitea](https://github.com/go-gitea/gitea/pull/26460) but didn't get much attention, and I switched to Forgejo in the meantime :)
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4228
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Chl <chl@xlii.si>
Co-committed-by: Chl <chl@xlii.si>
2024-06-28 08:11:57 +03:00
|
|
|
func TestLabel_LoadSelectedLabelsAfterClick(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
Optimization of labels handling in issue_search (#4228)
This PR optimizes the SQL query and de-duplicate the labels' ids when generating the query string, on the issue page.
<hr/>
### Background
Some time ago, BingBot and some other crawlers have been putting my instance on its knees with requests containing a lot of label ids, like this one :
```
[07/Aug/2023:11:28:37 +0200] "GET /Dolibarr/sendrecurringinvoicebymail/issues?q=&type=all&sort=&state=closed&labels=1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c2%2c10%2c2%2c1%2c1%2c10%2c10%2c7%2c6%2c10%2c10%2c3%2c2%2c1%2c5%2c10%2c1%2c6%2c2%2c7%2c3%2c7%2c6%2c10%2c1%2c10%2c1%2c1%2c7%2c7%2c1%2c1%2c1%2c1%2c10%2c10%2c1%2c2%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c2%2c1%2c12%2c6%2c6%2c10&milestone=0&project=-1&poster=0 HTTP/1.1" 499 0 "-" "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm) Chrome/103.0.5060.134 Safari/537.36"
```
Since each of the label ids implies a join, it grows exponentially expensive for the database engine (at least on PostgreSQL but SQLite suffers a little too).
Thus, this PR proposes two enhancements:
* rewrite the database query to use only one squashed condition,
* deduplicate the label ids when generating the URL.
### Performance comparison
Here are some timings on Postgresql-backed, Forgejo 7.0.4 instances :
```sh
$ time curl -s -o /dev/null "http://localhost:3000/toto/tata/issues?q=&type=all&sort=&labels=19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25&state=open&milestone=0&project=0&assignee=0&poster=0"
real 0m10,491s
user 0m0,017s
sys 0m0,008s
```
...and with the patch:
```sh
$ time curl -s -o /dev/null "http://localhost:3000/toto/tata/issues?q=&type=all&sort=&labels=19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25&state=open&milestone=0&project=0&assignee=0&poster=0"
real 0m0,094s
user 0m0,012s
sys 0m0,013s
```
### Annex
This issue was originally proposed to [Gitea](https://github.com/go-gitea/gitea/pull/26460) but didn't get much attention, and I switched to Forgejo in the meantime :)
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4228
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Chl <chl@xlii.si>
Co-committed-by: Chl <chl@xlii.si>
2024-06-28 08:11:57 +03:00
|
|
|
// Loading the label id:8 (scope/label2) which have a scope and an
|
|
|
|
// exclusivity with id:7 (scope/label1)
|
|
|
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 8})
|
|
|
|
|
|
|
|
// First test : with negative and scope
|
|
|
|
label.LoadSelectedLabelsAfterClick([]int64{1, -8}, []string{"", "scope"})
|
|
|
|
assert.Equal(t, "1", label.QueryString)
|
2024-07-30 22:41:10 +03:00
|
|
|
assert.True(t, label.IsSelected)
|
Optimization of labels handling in issue_search (#4228)
This PR optimizes the SQL query and de-duplicate the labels' ids when generating the query string, on the issue page.
<hr/>
### Background
Some time ago, BingBot and some other crawlers have been putting my instance on its knees with requests containing a lot of label ids, like this one :
```
[07/Aug/2023:11:28:37 +0200] "GET /Dolibarr/sendrecurringinvoicebymail/issues?q=&type=all&sort=&state=closed&labels=1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c2%2c10%2c2%2c1%2c1%2c10%2c10%2c7%2c6%2c10%2c10%2c3%2c2%2c1%2c5%2c10%2c1%2c6%2c2%2c7%2c3%2c7%2c6%2c10%2c1%2c10%2c1%2c1%2c7%2c7%2c1%2c1%2c1%2c1%2c10%2c10%2c1%2c2%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c2%2c1%2c12%2c6%2c6%2c10&milestone=0&project=-1&poster=0 HTTP/1.1" 499 0 "-" "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm) Chrome/103.0.5060.134 Safari/537.36"
```
Since each of the label ids implies a join, it grows exponentially expensive for the database engine (at least on PostgreSQL but SQLite suffers a little too).
Thus, this PR proposes two enhancements:
* rewrite the database query to use only one squashed condition,
* deduplicate the label ids when generating the URL.
### Performance comparison
Here are some timings on Postgresql-backed, Forgejo 7.0.4 instances :
```sh
$ time curl -s -o /dev/null "http://localhost:3000/toto/tata/issues?q=&type=all&sort=&labels=19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25&state=open&milestone=0&project=0&assignee=0&poster=0"
real 0m10,491s
user 0m0,017s
sys 0m0,008s
```
...and with the patch:
```sh
$ time curl -s -o /dev/null "http://localhost:3000/toto/tata/issues?q=&type=all&sort=&labels=19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25&state=open&milestone=0&project=0&assignee=0&poster=0"
real 0m0,094s
user 0m0,012s
sys 0m0,013s
```
### Annex
This issue was originally proposed to [Gitea](https://github.com/go-gitea/gitea/pull/26460) but didn't get much attention, and I switched to Forgejo in the meantime :)
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4228
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Chl <chl@xlii.si>
Co-committed-by: Chl <chl@xlii.si>
2024-06-28 08:11:57 +03:00
|
|
|
|
|
|
|
// Second test : with duplicates
|
|
|
|
label.LoadSelectedLabelsAfterClick([]int64{1, 7, 1, 7, 7}, []string{"", "scope", "", "scope", "scope"})
|
|
|
|
assert.Equal(t, "1,8", label.QueryString)
|
2024-07-30 22:41:10 +03:00
|
|
|
assert.False(t, label.IsSelected)
|
Optimization of labels handling in issue_search (#4228)
This PR optimizes the SQL query and de-duplicate the labels' ids when generating the query string, on the issue page.
<hr/>
### Background
Some time ago, BingBot and some other crawlers have been putting my instance on its knees with requests containing a lot of label ids, like this one :
```
[07/Aug/2023:11:28:37 +0200] "GET /Dolibarr/sendrecurringinvoicebymail/issues?q=&type=all&sort=&state=closed&labels=1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c2%2c10%2c2%2c1%2c1%2c10%2c10%2c7%2c6%2c10%2c10%2c3%2c2%2c1%2c5%2c10%2c1%2c6%2c2%2c7%2c3%2c7%2c6%2c10%2c1%2c10%2c1%2c1%2c7%2c7%2c1%2c1%2c1%2c1%2c10%2c10%2c1%2c2%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c1%2c2%2c1%2c12%2c6%2c6%2c10&milestone=0&project=-1&poster=0 HTTP/1.1" 499 0 "-" "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm) Chrome/103.0.5060.134 Safari/537.36"
```
Since each of the label ids implies a join, it grows exponentially expensive for the database engine (at least on PostgreSQL but SQLite suffers a little too).
Thus, this PR proposes two enhancements:
* rewrite the database query to use only one squashed condition,
* deduplicate the label ids when generating the URL.
### Performance comparison
Here are some timings on Postgresql-backed, Forgejo 7.0.4 instances :
```sh
$ time curl -s -o /dev/null "http://localhost:3000/toto/tata/issues?q=&type=all&sort=&labels=19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25&state=open&milestone=0&project=0&assignee=0&poster=0"
real 0m10,491s
user 0m0,017s
sys 0m0,008s
```
...and with the patch:
```sh
$ time curl -s -o /dev/null "http://localhost:3000/toto/tata/issues?q=&type=all&sort=&labels=19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25%2c19%2c25&state=open&milestone=0&project=0&assignee=0&poster=0"
real 0m0,094s
user 0m0,012s
sys 0m0,013s
```
### Annex
This issue was originally proposed to [Gitea](https://github.com/go-gitea/gitea/pull/26460) but didn't get much attention, and I switched to Forgejo in the meantime :)
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4228
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Chl <chl@xlii.si>
Co-committed-by: Chl <chl@xlii.si>
2024-06-28 08:11:57 +03:00
|
|
|
|
|
|
|
// Third test : empty set
|
|
|
|
label.LoadSelectedLabelsAfterClick([]int64{}, []string{})
|
|
|
|
assert.False(t, label.IsSelected)
|
|
|
|
assert.Equal(t, "8", label.QueryString)
|
|
|
|
}
|
|
|
|
|
Scoped labels (#22585)
Add a new "exclusive" option per label. This makes it so that when the
label is named `scope/name`, no other label with the same `scope/`
prefix can be set on an issue.
The scope is determined by the last occurence of `/`, so for example
`scope/alpha/name` and `scope/beta/name` are considered to be in
different scopes and can coexist.
Exclusive scopes are not enforced by any database rules, however they
are enforced when editing labels at the models level, automatically
removing any existing labels in the same scope when either attaching a
new label or replacing all labels.
In menus use a circle instead of checkbox to indicate they function as
radio buttons per scope. Issue filtering by label ensures that only a
single scoped label is selected at a time. Clicking with alt key can be
used to remove a scoped label, both when editing individual issues and
batch editing.
Label rendering refactor for consistency and code simplification:
* Labels now consistently have the same shape, emojis and tooltips
everywhere. This includes the label list and label assignment menus.
* In label list, show description below label same as label menus.
* Don't use exactly black/white text colors to look a bit nicer.
* Simplify text color computation. There is no point computing luminance
in linear color space, as this is a perceptual problem and sRGB is
closer to perceptually linear.
* Increase height of label assignment menus to show more labels. Showing
only 3-4 labels at a time leads to a lot of scrolling.
* Render all labels with a new RenderLabel template helper function.
Label creation and editing in multiline modal menu:
* Change label creation to open a modal menu like label editing.
* Change menu layout to place name, description and colors on separate
lines.
* Don't color cancel button red in label editing modal menu.
* Align text to the left in model menu for better readability and
consistent with settings layout elsewhere.
Custom exclusive scoped label rendering:
* Display scoped label prefix and suffix with slightly darker and
lighter background color respectively, and a slanted edge between them
similar to the `/` symbol.
* In menus exclusive labels are grouped with a divider line.
---------
Co-authored-by: Yarden Shoham <hrsi88@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
2023-02-18 22:17:39 +03:00
|
|
|
func TestLabel_ExclusiveScope(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
Scoped labels (#22585)
Add a new "exclusive" option per label. This makes it so that when the
label is named `scope/name`, no other label with the same `scope/`
prefix can be set on an issue.
The scope is determined by the last occurence of `/`, so for example
`scope/alpha/name` and `scope/beta/name` are considered to be in
different scopes and can coexist.
Exclusive scopes are not enforced by any database rules, however they
are enforced when editing labels at the models level, automatically
removing any existing labels in the same scope when either attaching a
new label or replacing all labels.
In menus use a circle instead of checkbox to indicate they function as
radio buttons per scope. Issue filtering by label ensures that only a
single scoped label is selected at a time. Clicking with alt key can be
used to remove a scoped label, both when editing individual issues and
batch editing.
Label rendering refactor for consistency and code simplification:
* Labels now consistently have the same shape, emojis and tooltips
everywhere. This includes the label list and label assignment menus.
* In label list, show description below label same as label menus.
* Don't use exactly black/white text colors to look a bit nicer.
* Simplify text color computation. There is no point computing luminance
in linear color space, as this is a perceptual problem and sRGB is
closer to perceptually linear.
* Increase height of label assignment menus to show more labels. Showing
only 3-4 labels at a time leads to a lot of scrolling.
* Render all labels with a new RenderLabel template helper function.
Label creation and editing in multiline modal menu:
* Change label creation to open a modal menu like label editing.
* Change menu layout to place name, description and colors on separate
lines.
* Don't color cancel button red in label editing modal menu.
* Align text to the left in model menu for better readability and
consistent with settings layout elsewhere.
Custom exclusive scoped label rendering:
* Display scoped label prefix and suffix with slightly darker and
lighter background color respectively, and a slanted edge between them
similar to the `/` symbol.
* In menus exclusive labels are grouped with a divider line.
---------
Co-authored-by: Yarden Shoham <hrsi88@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
2023-02-18 22:17:39 +03:00
|
|
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 7})
|
|
|
|
assert.Equal(t, "scope", label.ExclusiveScope())
|
|
|
|
|
|
|
|
label = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 9})
|
|
|
|
assert.Equal(t, "scope/subscope", label.ExclusiveScope())
|
2022-06-13 12:37:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewLabels(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
labels := []*issues_model.Label{
|
|
|
|
{RepoID: 2, Name: "labelName2", Color: "#123456"},
|
|
|
|
{RepoID: 3, Name: "labelName3", Color: "#123"},
|
|
|
|
{RepoID: 4, Name: "labelName4", Color: "ABCDEF"},
|
|
|
|
{RepoID: 5, Name: "labelName5", Color: "DEF"},
|
|
|
|
}
|
2024-07-30 22:41:10 +03:00
|
|
|
require.Error(t, issues_model.NewLabel(db.DefaultContext, &issues_model.Label{RepoID: 3, Name: "invalid Color", Color: ""}))
|
|
|
|
require.Error(t, issues_model.NewLabel(db.DefaultContext, &issues_model.Label{RepoID: 3, Name: "invalid Color", Color: "#45G"}))
|
|
|
|
require.Error(t, issues_model.NewLabel(db.DefaultContext, &issues_model.Label{RepoID: 3, Name: "invalid Color", Color: "#12345G"}))
|
|
|
|
require.Error(t, issues_model.NewLabel(db.DefaultContext, &issues_model.Label{RepoID: 3, Name: "invalid Color", Color: "45G"}))
|
|
|
|
require.Error(t, issues_model.NewLabel(db.DefaultContext, &issues_model.Label{RepoID: 3, Name: "invalid Color", Color: "12345G"}))
|
2022-06-13 12:37:59 +03:00
|
|
|
for _, label := range labels {
|
|
|
|
unittest.AssertNotExistsBean(t, label)
|
|
|
|
}
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.NewLabels(db.DefaultContext, labels...))
|
2022-06-13 12:37:59 +03:00
|
|
|
for _, label := range labels {
|
|
|
|
unittest.AssertExistsAndLoadBean(t, label, unittest.Cond("id = ?", label.ID))
|
|
|
|
}
|
|
|
|
unittest.CheckConsistencyFor(t, &issues_model.Label{}, &repo_model.Repository{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLabelByID(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
label, err := issues_model.GetLabelByID(db.DefaultContext, 1)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.EqualValues(t, 1, label.ID)
|
|
|
|
|
|
|
|
_, err = issues_model.GetLabelByID(db.DefaultContext, unittest.NonexistentID)
|
|
|
|
assert.True(t, issues_model.IsErrLabelNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLabelInRepoByName(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
label, err := issues_model.GetLabelInRepoByName(db.DefaultContext, 1, "label1")
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.EqualValues(t, 1, label.ID)
|
|
|
|
assert.Equal(t, "label1", label.Name)
|
|
|
|
|
|
|
|
_, err = issues_model.GetLabelInRepoByName(db.DefaultContext, 1, "")
|
|
|
|
assert.True(t, issues_model.IsErrRepoLabelNotExist(err))
|
|
|
|
|
|
|
|
_, err = issues_model.GetLabelInRepoByName(db.DefaultContext, unittest.NonexistentID, "nonexistent")
|
|
|
|
assert.True(t, issues_model.IsErrRepoLabelNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLabelInRepoByNames(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2023-09-16 17:39:12 +03:00
|
|
|
labelIDs, err := issues_model.GetLabelIDsInRepoByNames(db.DefaultContext, 1, []string{"label1", "label2"})
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
|
|
|
|
assert.Len(t, labelIDs, 2)
|
|
|
|
|
|
|
|
assert.Equal(t, int64(1), labelIDs[0])
|
|
|
|
assert.Equal(t, int64(2), labelIDs[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLabelInRepoByNamesDiscardsNonExistentLabels(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
// label3 doesn't exists.. See labels.yml
|
2023-09-16 17:39:12 +03:00
|
|
|
labelIDs, err := issues_model.GetLabelIDsInRepoByNames(db.DefaultContext, 1, []string{"label1", "label2", "label3"})
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
|
|
|
|
assert.Len(t, labelIDs, 2)
|
|
|
|
|
|
|
|
assert.Equal(t, int64(1), labelIDs[0])
|
|
|
|
assert.Equal(t, int64(2), labelIDs[1])
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLabelInRepoByID(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
label, err := issues_model.GetLabelInRepoByID(db.DefaultContext, 1, 1)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.EqualValues(t, 1, label.ID)
|
|
|
|
|
|
|
|
_, err = issues_model.GetLabelInRepoByID(db.DefaultContext, 1, -1)
|
|
|
|
assert.True(t, issues_model.IsErrRepoLabelNotExist(err))
|
|
|
|
|
|
|
|
_, err = issues_model.GetLabelInRepoByID(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID)
|
|
|
|
assert.True(t, issues_model.IsErrRepoLabelNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLabelsInRepoByIDs(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-11-19 11:12:33 +03:00
|
|
|
labels, err := issues_model.GetLabelsInRepoByIDs(db.DefaultContext, 1, []int64{1, 2, unittest.NonexistentID})
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
if assert.Len(t, labels, 2) {
|
|
|
|
assert.EqualValues(t, 1, labels[0].ID)
|
|
|
|
assert.EqualValues(t, 2, labels[1].ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLabelsByRepoID(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
testSuccess := func(repoID int64, sortType string, expectedIssueIDs []int64) {
|
|
|
|
labels, err := issues_model.GetLabelsByRepoID(db.DefaultContext, repoID, sortType, db.ListOptions{})
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.Len(t, labels, len(expectedIssueIDs))
|
|
|
|
for i, label := range labels {
|
|
|
|
assert.EqualValues(t, expectedIssueIDs[i], label.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
testSuccess(1, "leastissues", []int64{2, 1})
|
|
|
|
testSuccess(1, "mostissues", []int64{1, 2})
|
|
|
|
testSuccess(1, "reversealphabetically", []int64{2, 1})
|
|
|
|
testSuccess(1, "default", []int64{1, 2})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Org versions
|
|
|
|
|
|
|
|
func TestGetLabelInOrgByName(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
label, err := issues_model.GetLabelInOrgByName(db.DefaultContext, 3, "orglabel3")
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.EqualValues(t, 3, label.ID)
|
|
|
|
assert.Equal(t, "orglabel3", label.Name)
|
|
|
|
|
|
|
|
_, err = issues_model.GetLabelInOrgByName(db.DefaultContext, 3, "")
|
|
|
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err))
|
|
|
|
|
|
|
|
_, err = issues_model.GetLabelInOrgByName(db.DefaultContext, 0, "orglabel3")
|
|
|
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err))
|
|
|
|
|
|
|
|
_, err = issues_model.GetLabelInOrgByName(db.DefaultContext, -1, "orglabel3")
|
|
|
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err))
|
|
|
|
|
|
|
|
_, err = issues_model.GetLabelInOrgByName(db.DefaultContext, unittest.NonexistentID, "nonexistent")
|
|
|
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLabelInOrgByID(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
label, err := issues_model.GetLabelInOrgByID(db.DefaultContext, 3, 3)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.EqualValues(t, 3, label.ID)
|
|
|
|
|
|
|
|
_, err = issues_model.GetLabelInOrgByID(db.DefaultContext, 3, -1)
|
|
|
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err))
|
|
|
|
|
|
|
|
_, err = issues_model.GetLabelInOrgByID(db.DefaultContext, 0, 3)
|
|
|
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err))
|
|
|
|
|
|
|
|
_, err = issues_model.GetLabelInOrgByID(db.DefaultContext, -1, 3)
|
|
|
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err))
|
|
|
|
|
|
|
|
_, err = issues_model.GetLabelInOrgByID(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID)
|
|
|
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLabelsInOrgByIDs(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-11-19 11:12:33 +03:00
|
|
|
labels, err := issues_model.GetLabelsInOrgByIDs(db.DefaultContext, 3, []int64{3, 4, unittest.NonexistentID})
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
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) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
testSuccess := func(orgID int64, sortType string, expectedIssueIDs []int64) {
|
|
|
|
labels, err := issues_model.GetLabelsByOrgID(db.DefaultContext, orgID, sortType, db.ListOptions{})
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
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})
|
|
|
|
|
2024-10-30 22:36:24 +03:00
|
|
|
_, err := issues_model.GetLabelsByOrgID(db.DefaultContext, 0, "leastissues", db.ListOptions{})
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err))
|
|
|
|
|
|
|
|
_, err = issues_model.GetLabelsByOrgID(db.DefaultContext, -1, "leastissues", db.ListOptions{})
|
|
|
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
func TestGetLabelsByIssueID(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
labels, err := issues_model.GetLabelsByIssueID(db.DefaultContext, 1)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
if assert.Len(t, labels, 1) {
|
|
|
|
assert.EqualValues(t, 1, labels[0].ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
labels, err = issues_model.GetLabelsByIssueID(db.DefaultContext, unittest.NonexistentID)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, labels)
|
2022-06-13 12:37:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateLabel(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 05:22:25 +03:00
|
|
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1})
|
2024-05-09 16:49:37 +03:00
|
|
|
// make sure update won't overwrite it
|
2022-06-13 12:37:59 +03:00
|
|
|
update := &issues_model.Label{
|
2023-08-14 12:56:14 +03:00
|
|
|
ID: label.ID,
|
|
|
|
Color: "#ffff00",
|
|
|
|
Name: "newLabelName",
|
|
|
|
Description: label.Description,
|
|
|
|
Exclusive: false,
|
|
|
|
ArchivedUnix: timeutil.TimeStamp(0),
|
2022-06-13 12:37:59 +03:00
|
|
|
}
|
|
|
|
label.Color = update.Color
|
|
|
|
label.Name = update.Name
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.UpdateLabel(db.DefaultContext, update))
|
2022-08-16 05:22:25 +03:00
|
|
|
newLabel := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1})
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.EqualValues(t, label.ID, newLabel.ID)
|
|
|
|
assert.EqualValues(t, label.Color, newLabel.Color)
|
|
|
|
assert.EqualValues(t, label.Name, newLabel.Name)
|
|
|
|
assert.EqualValues(t, label.Description, newLabel.Description)
|
2024-07-30 22:41:10 +03:00
|
|
|
assert.EqualValues(t, 0, newLabel.ArchivedUnix)
|
2022-06-13 12:37:59 +03:00
|
|
|
unittest.CheckConsistencyFor(t, &issues_model.Label{}, &repo_model.Repository{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteLabel(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 05:22:25 +03:00
|
|
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1})
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.DeleteLabel(db.DefaultContext, label.RepoID, label.ID))
|
2022-06-13 12:37:59 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &issues_model.Label{ID: label.ID, RepoID: label.RepoID})
|
|
|
|
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.DeleteLabel(db.DefaultContext, label.RepoID, label.ID))
|
2022-06-13 12:37:59 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &issues_model.Label{ID: label.ID})
|
|
|
|
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.DeleteLabel(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID))
|
2022-06-13 12:37:59 +03:00
|
|
|
unittest.CheckConsistencyFor(t, &issues_model.Label{}, &repo_model.Repository{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasIssueLabel(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.True(t, issues_model.HasIssueLabel(db.DefaultContext, 1, 1))
|
|
|
|
assert.False(t, issues_model.HasIssueLabel(db.DefaultContext, 1, 2))
|
|
|
|
assert.False(t, issues_model.HasIssueLabel(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewIssueLabel(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 05:22:25 +03:00
|
|
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 2})
|
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2022-06-13 12:37:59 +03:00
|
|
|
|
|
|
|
// add new IssueLabel
|
|
|
|
prevNumIssues := label.NumIssues
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.NewIssueLabel(db.DefaultContext, issue, label, doer))
|
2022-06-13 12:37:59 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: label.ID})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{
|
|
|
|
Type: issues_model.CommentTypeLabel,
|
|
|
|
PosterID: doer.ID,
|
|
|
|
IssueID: issue.ID,
|
|
|
|
LabelID: label.ID,
|
|
|
|
Content: "1",
|
|
|
|
})
|
2022-08-16 05:22:25 +03:00
|
|
|
label = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 2})
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.EqualValues(t, prevNumIssues+1, label.NumIssues)
|
|
|
|
|
|
|
|
// re-add existing IssueLabel
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.NewIssueLabel(db.DefaultContext, issue, label, doer))
|
2022-06-13 12:37:59 +03:00
|
|
|
unittest.CheckConsistencyFor(t, &issues_model.Issue{}, &issues_model.Label{})
|
|
|
|
}
|
|
|
|
|
Scoped labels (#22585)
Add a new "exclusive" option per label. This makes it so that when the
label is named `scope/name`, no other label with the same `scope/`
prefix can be set on an issue.
The scope is determined by the last occurence of `/`, so for example
`scope/alpha/name` and `scope/beta/name` are considered to be in
different scopes and can coexist.
Exclusive scopes are not enforced by any database rules, however they
are enforced when editing labels at the models level, automatically
removing any existing labels in the same scope when either attaching a
new label or replacing all labels.
In menus use a circle instead of checkbox to indicate they function as
radio buttons per scope. Issue filtering by label ensures that only a
single scoped label is selected at a time. Clicking with alt key can be
used to remove a scoped label, both when editing individual issues and
batch editing.
Label rendering refactor for consistency and code simplification:
* Labels now consistently have the same shape, emojis and tooltips
everywhere. This includes the label list and label assignment menus.
* In label list, show description below label same as label menus.
* Don't use exactly black/white text colors to look a bit nicer.
* Simplify text color computation. There is no point computing luminance
in linear color space, as this is a perceptual problem and sRGB is
closer to perceptually linear.
* Increase height of label assignment menus to show more labels. Showing
only 3-4 labels at a time leads to a lot of scrolling.
* Render all labels with a new RenderLabel template helper function.
Label creation and editing in multiline modal menu:
* Change label creation to open a modal menu like label editing.
* Change menu layout to place name, description and colors on separate
lines.
* Don't color cancel button red in label editing modal menu.
* Align text to the left in model menu for better readability and
consistent with settings layout elsewhere.
Custom exclusive scoped label rendering:
* Display scoped label prefix and suffix with slightly darker and
lighter background color respectively, and a slanted edge between them
similar to the `/` symbol.
* In menus exclusive labels are grouped with a divider line.
---------
Co-authored-by: Yarden Shoham <hrsi88@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
2023-02-18 22:17:39 +03:00
|
|
|
func TestNewIssueExclusiveLabel(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
Scoped labels (#22585)
Add a new "exclusive" option per label. This makes it so that when the
label is named `scope/name`, no other label with the same `scope/`
prefix can be set on an issue.
The scope is determined by the last occurence of `/`, so for example
`scope/alpha/name` and `scope/beta/name` are considered to be in
different scopes and can coexist.
Exclusive scopes are not enforced by any database rules, however they
are enforced when editing labels at the models level, automatically
removing any existing labels in the same scope when either attaching a
new label or replacing all labels.
In menus use a circle instead of checkbox to indicate they function as
radio buttons per scope. Issue filtering by label ensures that only a
single scoped label is selected at a time. Clicking with alt key can be
used to remove a scoped label, both when editing individual issues and
batch editing.
Label rendering refactor for consistency and code simplification:
* Labels now consistently have the same shape, emojis and tooltips
everywhere. This includes the label list and label assignment menus.
* In label list, show description below label same as label menus.
* Don't use exactly black/white text colors to look a bit nicer.
* Simplify text color computation. There is no point computing luminance
in linear color space, as this is a perceptual problem and sRGB is
closer to perceptually linear.
* Increase height of label assignment menus to show more labels. Showing
only 3-4 labels at a time leads to a lot of scrolling.
* Render all labels with a new RenderLabel template helper function.
Label creation and editing in multiline modal menu:
* Change label creation to open a modal menu like label editing.
* Change menu layout to place name, description and colors on separate
lines.
* Don't color cancel button red in label editing modal menu.
* Align text to the left in model menu for better readability and
consistent with settings layout elsewhere.
Custom exclusive scoped label rendering:
* Display scoped label prefix and suffix with slightly darker and
lighter background color respectively, and a slanted edge between them
similar to the `/` symbol.
* In menus exclusive labels are grouped with a divider line.
---------
Co-authored-by: Yarden Shoham <hrsi88@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
2023-02-18 22:17:39 +03:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 18})
|
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
|
|
|
|
otherLabel := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 6})
|
|
|
|
exclusiveLabelA := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 7})
|
|
|
|
exclusiveLabelB := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 8})
|
|
|
|
|
|
|
|
// coexisting regular and exclusive label
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.NewIssueLabel(db.DefaultContext, issue, otherLabel, doer))
|
|
|
|
require.NoError(t, issues_model.NewIssueLabel(db.DefaultContext, issue, exclusiveLabelA, doer))
|
Scoped labels (#22585)
Add a new "exclusive" option per label. This makes it so that when the
label is named `scope/name`, no other label with the same `scope/`
prefix can be set on an issue.
The scope is determined by the last occurence of `/`, so for example
`scope/alpha/name` and `scope/beta/name` are considered to be in
different scopes and can coexist.
Exclusive scopes are not enforced by any database rules, however they
are enforced when editing labels at the models level, automatically
removing any existing labels in the same scope when either attaching a
new label or replacing all labels.
In menus use a circle instead of checkbox to indicate they function as
radio buttons per scope. Issue filtering by label ensures that only a
single scoped label is selected at a time. Clicking with alt key can be
used to remove a scoped label, both when editing individual issues and
batch editing.
Label rendering refactor for consistency and code simplification:
* Labels now consistently have the same shape, emojis and tooltips
everywhere. This includes the label list and label assignment menus.
* In label list, show description below label same as label menus.
* Don't use exactly black/white text colors to look a bit nicer.
* Simplify text color computation. There is no point computing luminance
in linear color space, as this is a perceptual problem and sRGB is
closer to perceptually linear.
* Increase height of label assignment menus to show more labels. Showing
only 3-4 labels at a time leads to a lot of scrolling.
* Render all labels with a new RenderLabel template helper function.
Label creation and editing in multiline modal menu:
* Change label creation to open a modal menu like label editing.
* Change menu layout to place name, description and colors on separate
lines.
* Don't color cancel button red in label editing modal menu.
* Align text to the left in model menu for better readability and
consistent with settings layout elsewhere.
Custom exclusive scoped label rendering:
* Display scoped label prefix and suffix with slightly darker and
lighter background color respectively, and a slanted edge between them
similar to the `/` symbol.
* In menus exclusive labels are grouped with a divider line.
---------
Co-authored-by: Yarden Shoham <hrsi88@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
2023-02-18 22:17:39 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: otherLabel.ID})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: exclusiveLabelA.ID})
|
|
|
|
|
|
|
|
// exclusive label replaces existing one
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.NewIssueLabel(db.DefaultContext, issue, exclusiveLabelB, doer))
|
Scoped labels (#22585)
Add a new "exclusive" option per label. This makes it so that when the
label is named `scope/name`, no other label with the same `scope/`
prefix can be set on an issue.
The scope is determined by the last occurence of `/`, so for example
`scope/alpha/name` and `scope/beta/name` are considered to be in
different scopes and can coexist.
Exclusive scopes are not enforced by any database rules, however they
are enforced when editing labels at the models level, automatically
removing any existing labels in the same scope when either attaching a
new label or replacing all labels.
In menus use a circle instead of checkbox to indicate they function as
radio buttons per scope. Issue filtering by label ensures that only a
single scoped label is selected at a time. Clicking with alt key can be
used to remove a scoped label, both when editing individual issues and
batch editing.
Label rendering refactor for consistency and code simplification:
* Labels now consistently have the same shape, emojis and tooltips
everywhere. This includes the label list and label assignment menus.
* In label list, show description below label same as label menus.
* Don't use exactly black/white text colors to look a bit nicer.
* Simplify text color computation. There is no point computing luminance
in linear color space, as this is a perceptual problem and sRGB is
closer to perceptually linear.
* Increase height of label assignment menus to show more labels. Showing
only 3-4 labels at a time leads to a lot of scrolling.
* Render all labels with a new RenderLabel template helper function.
Label creation and editing in multiline modal menu:
* Change label creation to open a modal menu like label editing.
* Change menu layout to place name, description and colors on separate
lines.
* Don't color cancel button red in label editing modal menu.
* Align text to the left in model menu for better readability and
consistent with settings layout elsewhere.
Custom exclusive scoped label rendering:
* Display scoped label prefix and suffix with slightly darker and
lighter background color respectively, and a slanted edge between them
similar to the `/` symbol.
* In menus exclusive labels are grouped with a divider line.
---------
Co-authored-by: Yarden Shoham <hrsi88@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
2023-02-18 22:17:39 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: otherLabel.ID})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: exclusiveLabelB.ID})
|
|
|
|
unittest.AssertNotExistsBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: exclusiveLabelA.ID})
|
|
|
|
|
|
|
|
// exclusive label replaces existing one again
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.NewIssueLabel(db.DefaultContext, issue, exclusiveLabelA, doer))
|
Scoped labels (#22585)
Add a new "exclusive" option per label. This makes it so that when the
label is named `scope/name`, no other label with the same `scope/`
prefix can be set on an issue.
The scope is determined by the last occurence of `/`, so for example
`scope/alpha/name` and `scope/beta/name` are considered to be in
different scopes and can coexist.
Exclusive scopes are not enforced by any database rules, however they
are enforced when editing labels at the models level, automatically
removing any existing labels in the same scope when either attaching a
new label or replacing all labels.
In menus use a circle instead of checkbox to indicate they function as
radio buttons per scope. Issue filtering by label ensures that only a
single scoped label is selected at a time. Clicking with alt key can be
used to remove a scoped label, both when editing individual issues and
batch editing.
Label rendering refactor for consistency and code simplification:
* Labels now consistently have the same shape, emojis and tooltips
everywhere. This includes the label list and label assignment menus.
* In label list, show description below label same as label menus.
* Don't use exactly black/white text colors to look a bit nicer.
* Simplify text color computation. There is no point computing luminance
in linear color space, as this is a perceptual problem and sRGB is
closer to perceptually linear.
* Increase height of label assignment menus to show more labels. Showing
only 3-4 labels at a time leads to a lot of scrolling.
* Render all labels with a new RenderLabel template helper function.
Label creation and editing in multiline modal menu:
* Change label creation to open a modal menu like label editing.
* Change menu layout to place name, description and colors on separate
lines.
* Don't color cancel button red in label editing modal menu.
* Align text to the left in model menu for better readability and
consistent with settings layout elsewhere.
Custom exclusive scoped label rendering:
* Display scoped label prefix and suffix with slightly darker and
lighter background color respectively, and a slanted edge between them
similar to the `/` symbol.
* In menus exclusive labels are grouped with a divider line.
---------
Co-authored-by: Yarden Shoham <hrsi88@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
2023-02-18 22:17:39 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: otherLabel.ID})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: exclusiveLabelA.ID})
|
|
|
|
unittest.AssertNotExistsBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: exclusiveLabelB.ID})
|
|
|
|
}
|
|
|
|
|
2022-06-13 12:37:59 +03:00
|
|
|
func TestNewIssueLabels(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 05:22:25 +03:00
|
|
|
label1 := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1})
|
|
|
|
label2 := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 2})
|
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 5})
|
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2022-06-13 12:37:59 +03:00
|
|
|
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.NewIssueLabels(db.DefaultContext, issue, []*issues_model.Label{label1, label2}, doer))
|
2022-06-13 12:37:59 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: label1.ID})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{
|
|
|
|
Type: issues_model.CommentTypeLabel,
|
|
|
|
PosterID: doer.ID,
|
|
|
|
IssueID: issue.ID,
|
|
|
|
LabelID: label1.ID,
|
|
|
|
Content: "1",
|
|
|
|
})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: label1.ID})
|
2022-08-16 05:22:25 +03:00
|
|
|
label1 = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1})
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.EqualValues(t, 3, label1.NumIssues)
|
|
|
|
assert.EqualValues(t, 1, label1.NumClosedIssues)
|
2022-08-16 05:22:25 +03:00
|
|
|
label2 = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 2})
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.EqualValues(t, 1, label2.NumIssues)
|
|
|
|
assert.EqualValues(t, 1, label2.NumClosedIssues)
|
|
|
|
|
|
|
|
// corner case: test empty slice
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.NewIssueLabels(db.DefaultContext, issue, []*issues_model.Label{}, doer))
|
2022-06-13 12:37:59 +03:00
|
|
|
|
|
|
|
unittest.CheckConsistencyFor(t, &issues_model.Issue{}, &issues_model.Label{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteIssueLabel(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
testSuccess := func(labelID, issueID, doerID int64) {
|
2022-08-16 05:22:25 +03:00
|
|
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: labelID})
|
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issueID})
|
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: doerID})
|
2022-06-13 12:37:59 +03:00
|
|
|
|
|
|
|
expectedNumIssues := label.NumIssues
|
|
|
|
expectedNumClosedIssues := label.NumClosedIssues
|
|
|
|
if unittest.BeanExists(t, &issues_model.IssueLabel{IssueID: issueID, LabelID: labelID}) {
|
|
|
|
expectedNumIssues--
|
|
|
|
if issue.IsClosed {
|
|
|
|
expectedNumClosedIssues--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 23:18:50 +03:00
|
|
|
ctx, committer, err := db.TxContext(db.DefaultContext)
|
2022-06-13 12:37:59 +03:00
|
|
|
defer committer.Close()
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, issues_model.DeleteIssueLabel(ctx, issue, label, doer))
|
|
|
|
require.NoError(t, committer.Commit())
|
2022-06-13 12:37:59 +03:00
|
|
|
|
|
|
|
unittest.AssertNotExistsBean(t, &issues_model.IssueLabel{IssueID: issueID, LabelID: labelID})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{
|
|
|
|
Type: issues_model.CommentTypeLabel,
|
|
|
|
PosterID: doerID,
|
|
|
|
IssueID: issueID,
|
|
|
|
LabelID: labelID,
|
|
|
|
}, `content=""`)
|
2022-08-16 05:22:25 +03:00
|
|
|
label = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: labelID})
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.EqualValues(t, expectedNumIssues, label.NumIssues)
|
|
|
|
assert.EqualValues(t, expectedNumClosedIssues, label.NumClosedIssues)
|
|
|
|
}
|
|
|
|
testSuccess(1, 1, 2)
|
|
|
|
testSuccess(2, 5, 2)
|
|
|
|
testSuccess(1, 1, 2) // delete non-existent IssueLabel
|
|
|
|
|
|
|
|
unittest.CheckConsistencyFor(t, &issues_model.Issue{}, &issues_model.Label{})
|
|
|
|
}
|