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