You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

template.go 3.7KB

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>
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repository
  4. import (
  5. "context"
  6. "code.gitea.io/gitea/models/db"
  7. git_model "code.gitea.io/gitea/models/git"
  8. issues_model "code.gitea.io/gitea/models/issues"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. user_model "code.gitea.io/gitea/models/user"
  11. notify_service "code.gitea.io/gitea/services/notify"
  12. )
  13. // GenerateIssueLabels generates issue labels from a template repository
  14. func GenerateIssueLabels(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
  15. templateLabels, err := issues_model.GetLabelsByRepoID(ctx, templateRepo.ID, "", db.ListOptions{})
  16. if err != nil {
  17. return err
  18. }
  19. // Prevent insert being called with an empty slice which would result in
  20. // err "no element on slice when insert".
  21. if len(templateLabels) == 0 {
  22. return nil
  23. }
  24. newLabels := make([]*issues_model.Label, 0, len(templateLabels))
  25. for _, templateLabel := range templateLabels {
  26. newLabels = append(newLabels, &issues_model.Label{
  27. RepoID: generateRepo.ID,
  28. Name: templateLabel.Name,
  29. Exclusive: templateLabel.Exclusive,
  30. Description: templateLabel.Description,
  31. Color: templateLabel.Color,
  32. })
  33. }
  34. return db.Insert(ctx, newLabels)
  35. }
  36. func GenerateProtectedBranch(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
  37. templateBranches, err := git_model.FindRepoProtectedBranchRules(ctx, templateRepo.ID)
  38. if err != nil {
  39. return err
  40. }
  41. // Prevent insert being called with an empty slice which would result in
  42. // err "no element on slice when insert".
  43. if len(templateBranches) == 0 {
  44. return nil
  45. }
  46. newBranches := make([]*git_model.ProtectedBranch, 0, len(templateBranches))
  47. for _, templateBranch := range templateBranches {
  48. templateBranch.ID = 0
  49. templateBranch.RepoID = generateRepo.ID
  50. templateBranch.UpdatedUnix = 0
  51. templateBranch.CreatedUnix = 0
  52. newBranches = append(newBranches, templateBranch)
  53. }
  54. return db.Insert(ctx, newBranches)
  55. }
  56. // GenerateRepository generates a repository from a template
  57. func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templateRepo *repo_model.Repository, opts GenerateRepoOptions) (_ *repo_model.Repository, err error) {
  58. if !doer.IsAdmin && !owner.CanCreateRepo() {
  59. return nil, repo_model.ErrReachLimitOfRepo{
  60. Limit: owner.MaxRepoCreation,
  61. }
  62. }
  63. var generateRepo *repo_model.Repository
  64. if err = db.WithTx(ctx, func(ctx context.Context) error {
  65. generateRepo, err = generateRepository(ctx, doer, owner, templateRepo, opts)
  66. if err != nil {
  67. return err
  68. }
  69. // Git Content
  70. if opts.GitContent && !templateRepo.IsEmpty {
  71. if err = GenerateGitContent(ctx, templateRepo, generateRepo); err != nil {
  72. return err
  73. }
  74. }
  75. // Topics
  76. if opts.Topics {
  77. if err = repo_model.GenerateTopics(ctx, templateRepo, generateRepo); err != nil {
  78. return err
  79. }
  80. }
  81. // Git Hooks
  82. if opts.GitHooks {
  83. if err = GenerateGitHooks(ctx, templateRepo, generateRepo); err != nil {
  84. return err
  85. }
  86. }
  87. // Webhooks
  88. if opts.Webhooks {
  89. if err = GenerateWebhooks(ctx, templateRepo, generateRepo); err != nil {
  90. return err
  91. }
  92. }
  93. // Avatar
  94. if opts.Avatar && len(templateRepo.Avatar) > 0 {
  95. if err = generateAvatar(ctx, templateRepo, generateRepo); err != nil {
  96. return err
  97. }
  98. }
  99. // Issue Labels
  100. if opts.IssueLabels {
  101. if err = GenerateIssueLabels(ctx, templateRepo, generateRepo); err != nil {
  102. return err
  103. }
  104. }
  105. if opts.ProtectedBranch {
  106. if err = GenerateProtectedBranch(ctx, templateRepo, generateRepo); err != nil {
  107. return err
  108. }
  109. }
  110. return nil
  111. }); err != nil {
  112. return nil, err
  113. }
  114. notify_service.CreateRepository(ctx, doer, owner, generateRepo)
  115. return generateRepo, nil
  116. }