gitea源码

licenses.go 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package misc
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "code.gitea.io/gitea/modules/options"
  9. repo_module "code.gitea.io/gitea/modules/repository"
  10. "code.gitea.io/gitea/modules/setting"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "code.gitea.io/gitea/modules/util"
  13. "code.gitea.io/gitea/services/context"
  14. )
  15. // Returns a list of all License templates
  16. func ListLicenseTemplates(ctx *context.APIContext) {
  17. // swagger:operation GET /licenses miscellaneous listLicenseTemplates
  18. // ---
  19. // summary: Returns a list of all license templates
  20. // produces:
  21. // - application/json
  22. // responses:
  23. // "200":
  24. // "$ref": "#/responses/LicenseTemplateList"
  25. response := make([]api.LicensesTemplateListEntry, len(repo_module.Licenses))
  26. for i, license := range repo_module.Licenses {
  27. response[i] = api.LicensesTemplateListEntry{
  28. Key: license,
  29. Name: license,
  30. URL: fmt.Sprintf("%sapi/v1/licenses/%s", setting.AppURL, url.PathEscape(license)),
  31. }
  32. }
  33. ctx.JSON(http.StatusOK, response)
  34. }
  35. func GetLicenseTemplateInfo(ctx *context.APIContext) {
  36. // swagger:operation GET /licenses/{name} miscellaneous getLicenseTemplateInfo
  37. // ---
  38. // summary: Returns information about a license template
  39. // produces:
  40. // - application/json
  41. // parameters:
  42. // - name: name
  43. // in: path
  44. // description: name of the license
  45. // type: string
  46. // required: true
  47. // responses:
  48. // "200":
  49. // "$ref": "#/responses/LicenseTemplateInfo"
  50. // "404":
  51. // "$ref": "#/responses/notFound"
  52. name := util.PathJoinRelX(ctx.PathParam("name"))
  53. text, err := options.License(name)
  54. if err != nil {
  55. ctx.APIErrorNotFound()
  56. return
  57. }
  58. response := api.LicenseTemplateInfo{
  59. Key: name,
  60. Name: name,
  61. URL: fmt.Sprintf("%sapi/v1/licenses/%s", setting.AppURL, url.PathEscape(name)),
  62. Body: string(text),
  63. // This is for combatibilty with the GitHub API. This Text is for some reason added to each License response.
  64. Implementation: "Create a text file (typically named LICENSE or LICENSE.txt) in the root of your source code and copy the text of the license into the file",
  65. }
  66. ctx.JSON(http.StatusOK, response)
  67. }