Improve CBF topics #36

Closed
Gusted wants to merge 7 commits from improve-cbf-topics into codeberg-1.17
Image
Owner
  • Topics that are prefix with cbf- will now receive special care within the code. It can only be removed and viewed by Instance admins. Repository owners are no longer able to remove these topics or even see that this topic was added to their repository(just the warning banner).
  • Because users don't receive the cbf- prefixed topics we need to give some special care when users do update their topics, this PR adds the AddMissingRepositoryTopics function which will make sure that all current cbf- prefixed topics will be added to the updated topics list.
  • The template to show the error will now call the HasMissingLicenseTopic function as the Topics slice no longer contains the cbf- prefixed topics.
  • Users are no longer able to search keywords that includes the cbf- text, this to avoid accidental leakage of repository that has cbf- topics.
- Topics that are prefix with `cbf-` will now receive special care within the code. It can only be removed and viewed by Instance admins. Repository owners are no longer able to remove these topics or even see that this topic was added to their repository(just the warning banner). - Because users don't receive the `cbf-` prefixed topics we need to give some special care when users do update their topics, this PR adds the `AddMissingRepositoryTopics` function which will make sure that all current `cbf-` prefixed topics will be added to the updated topics list. - The template to show the error will now call the `HasMissingLicenseTopic` function as the Topics slice no longer contains the `cbf-` prefixed topics. - Users are no longer able to search keywords that includes the `cbf-` text, this to avoid accidental leakage of repository that has `cbf-` topics.
- Topics that are prefix with `cbf-` will now receive special care
within the code. It can only be removed and viewed by Instance admins.
Repository owners are no longer able to remove these topics or even see
that this topic was added to their repository(just the warning banner).
- Because users don't receive the `cbf-` prefixed topics we need to give
some special care when users do update their topics, this PR adds the
`AddMissingRepositoryTopics` function which will make sure that all
current `cbf-` prefixed topics will be added to the updated topics list.
- The template to show the error will now call the
`HasMissingLicenseTopic` function as the Topics slice no longer contains
the `cbf-` prefixed topics.
- Users are no longer able to search keywords that includes the `cbf-`
text, this to avoid accidental leakage of repository that has `cbf-`
topics.
Image
Member

as normal repoAdmin (not instance admin) you can add cbf-... topics but not remove or see them ... (via webUI)

as normal repoAdmin (not instance admin) you can add `cbf-...` topics but not remove or see them ... (via webUI)
@ -18,0 +39,4 @@
// HasMissingLicenseTopic returns if repository has a "cbf-nolicense" or "cbf-abuse-nolicense" topic.
func (r *Repository) HasMissingLicenseTopic() bool {
for _, topic := range r.Topics {
if topic == "cbf-nolicense" || topic == "cbf-abuse-nolicense" {
Member

can we create a const block where we put these strings to?

can we create a const block where we put these strings to?
Owner

Why? The function is specific to that flag, I don't see a need to define this somewhere. For other labels, you obviously define new functions anyway.

Why? The function is specific to *that* flag, I don't see a need to define this somewhere. For other labels, you obviously define new functions anyway.
Owner

I just somehow think that these functions are obviously less performant than scanning in the template, aren't they? I mean, previously I looped through the topics twice, once to print them and once to display banners.

Now we iterate through them for each check. e.g. in the future, if the templates do check
HasMissingLicenseTopic(), HasStorageAbuseTopic(), HasStorageExceptionTopic, HasEventXyParticipationTopic(), HasBonusFeatureAbcTopic() ... we iterate through the topics many times, right?

I just somehow think that these functions are obviously less performant than scanning in the template, aren't they? I mean, previously I looped through the topics twice, once to print them and once to display banners. Now we iterate through them for each check. e.g. in the future, if the templates do check HasMissingLicenseTopic(), HasStorageAbuseTopic(), HasStorageExceptionTopic, HasEventXyParticipationTopic(), HasBonusFeatureAbcTopic() ... we iterate through the topics many times, right?
Member
diff --git a/models/repo/codeberg.go b/models/repo/codeberg.go
index a7f1b7eb4..c212d8322 100644
--- a/models/repo/codeberg.go
+++ b/models/repo/codeberg.go
@@ -13,6 +13,12 @@ import (
        user_model "code.gitea.io/gitea/models/user"
 )
 
+const (
+       codebergTopicTrefix  = "cbf-"
+       missingLicenseTopic1 = "cbf-nolicense"
+       missingLicenseTopic2 = "cbf-abuse-nolicense"
+)
+
 // check if a repo topic is used as a Codeberg flag
 func (t *Topic) IsCodebergFlag() bool {
        return strings.HasPrefix(t.Name, "cbf-")
@@ -23,7 +29,7 @@ func (t *Topic) IsCodebergFlag() bool {
 func (r *Repository) AddMissingRepositoryTopics(topics []string) []string {
 outercheck:
        for _, topic := range r.Topics {
-               if strings.HasPrefix(topic, "cbf-") {
+               if strings.HasPrefix(topic, codebergTopicTrefix) {
                        // Avoid duplicates.
                        for _, vTopic := range topics {
                                if vTopic == topic {
@@ -39,7 +45,7 @@ outercheck:
 // HasMissingLicenseTopic returns if repository has a "cbf-nolicense" or "cbf-abuse-nolicense" topic.
 func (r *Repository) HasMissingLicenseTopic() bool {
        for _, topic := range r.Topics {
-               if topic == "cbf-nolicense" || topic == "cbf-abuse-nolicense" {
+               if topic == missingLicenseTopic1 || topic == missingLicenseTopic2 {
                        return true
                }
        }

it improves codestile ...

```diff diff --git a/models/repo/codeberg.go b/models/repo/codeberg.go index a7f1b7eb4..c212d8322 100644 --- a/models/repo/codeberg.go +++ b/models/repo/codeberg.go @@ -13,6 +13,12 @@ import ( user_model "code.gitea.io/gitea/models/user" ) +const ( + codebergTopicTrefix = "cbf-" + missingLicenseTopic1 = "cbf-nolicense" + missingLicenseTopic2 = "cbf-abuse-nolicense" +) + // check if a repo topic is used as a Codeberg flag func (t *Topic) IsCodebergFlag() bool { return strings.HasPrefix(t.Name, "cbf-") @@ -23,7 +29,7 @@ func (t *Topic) IsCodebergFlag() bool { func (r *Repository) AddMissingRepositoryTopics(topics []string) []string { outercheck: for _, topic := range r.Topics { - if strings.HasPrefix(topic, "cbf-") { + if strings.HasPrefix(topic, codebergTopicTrefix) { // Avoid duplicates. for _, vTopic := range topics { if vTopic == topic { @@ -39,7 +45,7 @@ outercheck: // HasMissingLicenseTopic returns if repository has a "cbf-nolicense" or "cbf-abuse-nolicense" topic. func (r *Repository) HasMissingLicenseTopic() bool { for _, topic := range r.Topics { - if topic == "cbf-nolicense" || topic == "cbf-abuse-nolicense" { + if topic == missingLicenseTopic1 || topic == missingLicenseTopic2 { return true } } ``` it improves codestile ...
Author
Owner

I'm fine with making cbf- a const. But the specific licenses is either a global slice or just hardcoded within the function.

I'm fine with making `cbf-` a const. But the specific licenses is either a global slice or just hardcoded within the function.
6543 marked this conversation as resolved
Image
Author
Owner

as normal repoAdmin (not instance admin) you can add cbf-... topics but not remove or see them ... (via webUI)

Yeah, if users want to break their repository. They should free to do so! (Forget to check for this endpoint).

> as normal repoAdmin (not instance admin) you can add cbf-... topics but not remove or see them ... (via webUI) Yeah, if users want to break their repository. They should free to do so! (Forget to check for this endpoint).
Image
Member

from @fnetX

"... I just somehow think that these functions are obviously less performant than scanning in the template, aren't they? I mean, previously I looped through the topics twice, once to print them and once to display banners."

from @fnetX > "... I just somehow think that these functions are obviously less performant than scanning in the template, aren't they? I mean, previously I looped through the topics twice, once to print them and once to display banners."
Image
Member

I would say it depends how mouch overhead the template engine does add to in-template-functions.

I would say it depends how mouch overhead the template engine does add to in-template-functions.
Image
Author
Owner

Templates have huge overhead when doing computations(especially for-loops). It's not a general purposed dynamic scripting language. It's to construct a HTML page.

If you would like to run your benchmarks: https://codeberg.org/Gusted/benchmark-loops-go-template

-> % go test -bench .           
goos: linux
goarch: amd64
pkg: gusted-bench
cpu: AMD Ryzen 5 3600X 6-Core Processor             
BenchmarkLoopNative-12            740875              2700 ns/op
BenchmarkLoopTempl-12             104628             10189 ns/op
PASS
ok      gusted-bench    3.203s
Templates have huge overhead when doing computations(especially for-loops). It's not a general purposed dynamic scripting language. It's to construct a HTML page. If you would like to run your benchmarks: https://codeberg.org/Gusted/benchmark-loops-go-template ``` -> % go test -bench . goos: linux goarch: amd64 pkg: gusted-bench cpu: AMD Ryzen 5 3600X 6-Core Processor BenchmarkLoopNative-12 740875 2700 ns/op BenchmarkLoopTempl-12 104628 10189 ns/op PASS ok gusted-bench 3.203s ```
Image
Member

we can run a lot of loops before we have to worry about golang overhead 😆

we can run a lot of loops before we have to worry about golang overhead 😆
@ -44,2 +39,3 @@
{{end}}
</div>
{{end}}
Member

why rm last \n ?

why rm last \n ?
Author
Owner

Not sure WTF Gitea is doing here(try to click on expanding the code below this hunk). But there is another new line there(this just removes the other new line). So 2 empty last new lines -> 1 empty last new line.

Not sure WTF Gitea is doing here(try to click on expanding the code below this hunk). But there is another new line there(this just removes the other new line). So 2 empty last new lines -> 1 empty last new line.
6543 marked this conversation as resolved
Image 6543 approved these changes 2022-07-25 00:08:05 +02:00
Image 6543 left a comment
Member

beside file format nit

beside file format nit
Image 6543 approved these changes 2022-07-25 00:31:40 +02:00
Image
Member
@Gusted 58a0298bc0c16aca62757e4ce58b5f3157e4885b
Image
Author
Owner

Please check if the rendered HTML still looks fine. I just copied pasted it.

Please check if the rendered HTML still looks fine. I just copied pasted it.
Image
Member

Image

![](https://codeberg.org/attachments/4ae84a7a-38ad-4ef6-a861-ac005af292ce)
Image
Author
Owner

Weird, it current isn't horizontal centered:
image

Ref: https://codeberg.org/nogerine/solar

Weird, it current isn't horizontal centered: ![image](/attachments/6e002fc6-e194-424a-b0af-efc4ff5b9c6c) Ref: https://codeberg.org/nogerine/solar
Image
Owner

Yeah, horizontal centering is hard to read with multiline text, so I made it align left. But maybe I forgot to add that to some of the banners.

Yeah, horizontal centering is hard to read with multiline text, so I made it align left. But maybe I forgot to add that to some of the banners.
Image
Member

I think we can ad the fix in here

I think we can ad the fix in here
Image
Author
Owner

I'm not sure which banners are incorrect. So @6543 feel free to mess with my PR to add a fix.

I'm not sure which banners are incorrect. So @6543 feel free to mess with my PR to add a fix.
Image 6543 self-assigned this 2022-07-27 12:22:21 +02:00
Image
Owner

My proposal for next steps with this (I'm fine to merge as-is in case of missing agreement)

  • HasMissingLicenseTopic(...) is rewritten to be more generic. My ideal solution would be to just loop once and filter the prefixed topics, then only loop through them in a template; or HasTopic(x) returns true/false and doesn't need to be created per topic
  • cbf- prefix is renamed to something not codeberg-specific, so this patch can be used by other instances and the Codeberg Moderation/Admin tool will be usable without disabling codeberg-specific features like that (because it can be used by every other instance then)
My proposal for next steps with this (I'm fine to merge as-is in case of missing agreement) - HasMissingLicenseTopic(...) is rewritten to be more generic. My ideal solution would be to just loop once and filter the prefixed topics, then only loop through them in a template; or HasTopic(x) returns true/false and doesn't need to be created per topic - `cbf-` prefix is renamed to something not codeberg-specific, so this patch can be used by other instances and the Codeberg Moderation/Admin tool will be usable without disabling codeberg-specific features like that (because it can be used by every other instance then)
Image
Author
Owner

cbf- prefix is renamed to something not codeberg-specific, so this patch can be used by other instances and the Codeberg Moderation/Admin tool will be usable without disabling codeberg-specific features like that (because it can be used by every other instance then)

What about internal- or reserved-?

HasMissingLicenseTopic(...) is rewritten to be more generic. My ideal solution would be to just loop once and filter the prefixed topics, then only loop through them in a template; or HasTopic(x) returns true/false and doesn't need to be created per topic

Alright moving to HasTopic(x) interface.

> cbf- prefix is renamed to something not codeberg-specific, so this patch can be used by other instances and the Codeberg Moderation/Admin tool will be usable without disabling codeberg-specific features like that (because it can be used by every other instance then) What about `internal-` or `reserved-`? > HasMissingLicenseTopic(...) is rewritten to be more generic. My ideal solution would be to just loop once and filter the prefixed topics, then only loop through them in a template; or HasTopic(x) returns true/false and doesn't need to be created per topic Alright moving to `HasTopic(x)` interface.
Image
Member

we might merge it ... as we can itterate on it later

Y/N?

we might merge it ... as we can itterate on it later Y/N?
Image
Owner

@Gusted since there is a limit in length and I prefer if the label names are descriptive, we should choose a short prefix IMHO. It could be "iflag" (internal flag) or we could use some disallowed name if this is not a problem (starting with - for example).

Otherwise, what about 0-- or something like this? (0--abuse ...)

@Gusted since there is a limit in length and I prefer if the label names are descriptive, we should choose a short prefix IMHO. It could be "iflag" (internal flag) or we could use some disallowed name if this is not a problem (starting with - for example). Otherwise, what about 0-- or something like this? (0--abuse ...)
Image
Author
Owner

iflag is fine with me.

iflag is fine with me.
Image
Owner

The disadvantage of choosing a term is that the "iflag" project that comes up in two years faces unexpected behaviour if they want to label a repo with iflag-official, iflag-meta and iflag-contribution-welcome ^^

(Feel free to go ahead, anyway, this idea just came to my mind)

The disadvantage of choosing a term is that the "iflag" project that comes up in two years faces unexpected behaviour if they want to label a repo with iflag-official, iflag-meta and iflag-contribution-welcome ^^ (Feel free to go ahead, anyway, this idea just came to my mind)
Image
Member

should I rebase it to v18 ?!?

should I rebase it to v18 ?!?
Image
Owner

Once Gusted has some free cycles, this is probably best forwarded to Forgejo and fixed for the iflag- prefix.

Once Gusted has some free cycles, this is probably best forwarded to Forgejo and fixed for the iflag- prefix.
Image fnetX closed this pull request 2023-11-30 00:40:16 +01:00

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Codeberg-Infrastructure/forgejo!36
No description provided.