gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package conan
  4. import (
  5. "context"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "code.gitea.io/gitea/models/db"
  10. "code.gitea.io/gitea/models/packages"
  11. "code.gitea.io/gitea/modules/container"
  12. conan_module "code.gitea.io/gitea/modules/packages/conan"
  13. "xorm.io/builder"
  14. )
  15. // buildCondition creates a Like condition if a wildcard is present. Otherwise Eq is used.
  16. func buildCondition(name, value string) builder.Cond {
  17. if strings.Contains(value, "*") {
  18. return builder.Like{name, strings.ReplaceAll(strings.ReplaceAll(value, "_", "\\_"), "*", "%")}
  19. }
  20. return builder.Eq{name: value}
  21. }
  22. type RecipeSearchOptions struct {
  23. OwnerID int64
  24. Name string
  25. Version string
  26. User string
  27. Channel string
  28. }
  29. // SearchRecipes gets all recipes matching the search options
  30. func SearchRecipes(ctx context.Context, opts *RecipeSearchOptions) ([]string, error) {
  31. var cond builder.Cond = builder.Eq{
  32. "package_file.is_lead": true,
  33. "package.type": packages.TypeConan,
  34. "package.owner_id": opts.OwnerID,
  35. "package_version.is_internal": false,
  36. }
  37. if opts.Name != "" {
  38. cond = cond.And(buildCondition("package.lower_name", strings.ToLower(opts.Name)))
  39. }
  40. if opts.Version != "" {
  41. cond = cond.And(buildCondition("package_version.lower_version", strings.ToLower(opts.Version)))
  42. }
  43. if opts.User != "" || opts.Channel != "" {
  44. var propsCond builder.Cond = builder.Eq{
  45. "package_property.ref_type": packages.PropertyTypeFile,
  46. }
  47. propsCond = propsCond.And(builder.Expr("package_property.ref_id = package_file.id"))
  48. count := 0
  49. propsCondBlock := builder.NewCond()
  50. if opts.User != "" {
  51. count++
  52. propsCondBlock = propsCondBlock.Or(builder.Eq{"package_property.name": conan_module.PropertyRecipeUser}.And(buildCondition("package_property.value", opts.User)))
  53. }
  54. if opts.Channel != "" {
  55. count++
  56. propsCondBlock = propsCondBlock.Or(builder.Eq{"package_property.name": conan_module.PropertyRecipeChannel}.And(buildCondition("package_property.value", opts.Channel)))
  57. }
  58. propsCond = propsCond.And(propsCondBlock)
  59. cond = cond.And(builder.Eq{
  60. strconv.Itoa(count): builder.Select("COUNT(*)").Where(propsCond).From("package_property"),
  61. })
  62. }
  63. query := builder.
  64. Select("package.name, package_version.version, package_file.id").
  65. From("package_file").
  66. InnerJoin("package_version", "package_version.id = package_file.version_id").
  67. InnerJoin("package", "package.id = package_version.package_id").
  68. Where(cond)
  69. results := make([]struct {
  70. Name string
  71. Version string
  72. ID int64
  73. }, 0, 5)
  74. err := db.GetEngine(ctx).SQL(query).Find(&results)
  75. if err != nil {
  76. return nil, err
  77. }
  78. unique := make(container.Set[string])
  79. for _, info := range results {
  80. recipe := fmt.Sprintf("%s/%s", info.Name, info.Version)
  81. props, _ := packages.GetProperties(ctx, packages.PropertyTypeFile, info.ID)
  82. if len(props) > 0 {
  83. var (
  84. user = ""
  85. channel = ""
  86. )
  87. for _, prop := range props {
  88. if prop.Name == conan_module.PropertyRecipeUser {
  89. user = prop.Value
  90. }
  91. if prop.Name == conan_module.PropertyRecipeChannel {
  92. channel = prop.Value
  93. }
  94. }
  95. if user != "" && channel != "" {
  96. recipe = fmt.Sprintf("%s@%s/%s", recipe, user, channel)
  97. }
  98. }
  99. unique.Add(recipe)
  100. }
  101. recipes := make([]string, 0, len(unique))
  102. for recipe := range unique {
  103. recipes = append(recipes, recipe)
  104. }
  105. return recipes, nil
  106. }
  107. // GetPackageInfo gets the Conaninfo for a package
  108. func GetPackageInfo(ctx context.Context, ownerID int64, ref *conan_module.PackageReference) (string, error) {
  109. values, err := findPropertyValues(
  110. ctx,
  111. conan_module.PropertyPackageInfo,
  112. ownerID,
  113. ref.Recipe.Name,
  114. ref.Recipe.Version,
  115. map[string]string{
  116. conan_module.PropertyRecipeUser: ref.Recipe.User,
  117. conan_module.PropertyRecipeChannel: ref.Recipe.Channel,
  118. conan_module.PropertyRecipeRevision: ref.Recipe.Revision,
  119. conan_module.PropertyPackageReference: ref.Reference,
  120. conan_module.PropertyPackageRevision: ref.Revision,
  121. },
  122. )
  123. if err != nil {
  124. return "", err
  125. }
  126. if len(values) == 0 {
  127. return "", ErrPackageReferenceNotExist
  128. }
  129. return values[0].Value, nil
  130. }