From 2e128ba58946c7e968f88cfdd1a3056bfded1583 Mon Sep 17 00:00:00 2001 From: Norwin Date: Wed, 29 Sep 2021 22:09:43 +0200 Subject: [PATCH 1/4] put auth middleware before any parsing --- routers/api/v1/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 082381d31c0ec..a82581cd01115 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -753,7 +753,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route { Put(reqAdmin(), repo.AddTeam). Delete(reqAdmin(), repo.DeleteTeam) }, reqToken()) - m.Get("/raw/*", context.RepoRefForAPI, reqRepoReader(models.UnitTypeCode), repo.GetRawFile) + m.Get("/raw/*", reqRepoReader(models.UnitTypeCode), context.RepoRefForAPI, repo.GetRawFile) m.Get("/archive/*", reqRepoReader(models.UnitTypeCode), repo.GetArchive) m.Combo("/forks").Get(repo.ListForks). Post(reqToken(), reqRepoReader(models.UnitTypeCode), bind(api.CreateForkOption{}), repo.CreateFork) From d4fc683906994a9a98d960064da8fd48ab41aca9 Mon Sep 17 00:00:00 2001 From: Norwin Date: Wed, 29 Sep 2021 22:58:06 +0200 Subject: [PATCH 2/4] raw file api: clarify documentation the ?ref= param was added recently in #14602, and can fully replace the (previously undocumented) ref-prefix of the filepath --- routers/api/v1/repo/file.go | 35 ++++++++++++++++++++++- templates/swagger/v1_json.tmpl | 52 +++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index d9451b8090c6c..affab0edbb01b 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -23,6 +23,39 @@ import ( // GetRawFile get a file by path on a repository func GetRawFile(ctx *context.APIContext) { + // swagger:operation GET /repos/{owner}/{repo}/raw/{ref}/{filepath} repository repoGetRawFileOld + // --- + // summary: Get a file from a repository + // deprecated: true + // produces: + // - application/json + // parameters: + // - name: owner + // in: path + // description: owner of the repo + // type: string + // required: true + // - name: repo + // in: path + // description: name of the repo + // type: string + // required: true + // - name: filepath + // in: path + // description: filepath of the file to get + // type: string + // required: true + // - name: ref + // in: path + // description: "The name of the commit/branch/tag. Default to the repository’s default branch (usually master)" + // type: string + // deprecated: true + // responses: + // 200: + // description: success + // "404": + // "$ref": "#/responses/notFound" + // swagger:operation GET /repos/{owner}/{repo}/raw/{filepath} repository repoGetRawFile // --- // summary: Get a file from a repository @@ -46,7 +79,7 @@ func GetRawFile(ctx *context.APIContext) { // required: true // - name: ref // in: query - // description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)" + // description: "The name of the commit/branch/tag. Default to the repository’s default branch (usually master)" // type: string // required: false // responses: diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 63eba4583288e..63997fa60dad8 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -8214,7 +8214,7 @@ }, { "type": "string", - "description": "The name of the commit/branch/tag. Default the repository’s default branch (usually master)", + "description": "The name of the commit/branch/tag. Default to the repository’s default branch (usually master)", "name": "ref", "in": "query" } @@ -8229,6 +8229,56 @@ } } }, + "/repos/{owner}/{repo}/raw/{ref}/{filepath}": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "repository" + ], + "summary": "Get a file from a repository", + "operationId": "repoGetRawFileOld", + "deprecated": true, + "parameters": [ + { + "type": "string", + "description": "owner of the repo", + "name": "owner", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "name of the repo", + "name": "repo", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "filepath of the file to get", + "name": "filepath", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "The name of the commit/branch/tag. Default to the repository’s default branch (usually master)", + "name": "ref", + "in": "path" + } + ], + "responses": { + "200": { + "description": "success" + }, + "404": { + "$ref": "#/responses/notFound" + } + } + } + }, "/repos/{owner}/{repo}/releases": { "get": { "produces": [ From df834a7eb0b93ce09f2fc3fc446a17d3b10dcb74 Mon Sep 17 00:00:00 2001 From: Norwin Date: Wed, 29 Sep 2021 23:02:36 +0200 Subject: [PATCH 3/4] raw file API: add support for shortened commit SHA we just deprecated this API, but this is a short fix to #17179 A more minimal fix would be to check if len(parts) > 1 in line context/repo.go#L698 --- modules/context/api.go | 2 +- modules/context/repo.go | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/modules/context/api.go b/modules/context/api.go index e5216d911f8a6..25cb3f9aa465d 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -413,7 +413,7 @@ func RepoRefForAPI(next http.Handler) http.Handler { return } ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() - } else if len(refName) == 40 { + } else if len(refName) >= 7 && len(refName) <= 40 { ctx.Repo.CommitID = refName ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName) if err != nil { diff --git a/modules/context/repo.go b/modules/context/repo.go index eceefd9e5925d..2a76ea64a9661 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -693,11 +693,8 @@ func getRefName(ctx *Context, pathType RepoRefType) string { if refName := getRefName(ctx, RepoRefTag); len(refName) > 0 { return refName } - // For legacy and API support only full commit sha - parts := strings.Split(path, "/") - if len(parts) > 0 && len(parts[0]) == 40 { - ctx.Repo.TreePath = strings.Join(parts[1:], "/") - return parts[0] + if refName := getRefName(ctx, RepoRefCommit); len(refName) > 0 { + return refName } if refName := getRefName(ctx, RepoRefBlob); len(refName) > 0 { return refName @@ -710,7 +707,8 @@ func getRefName(ctx *Context, pathType RepoRefType) string { return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsTagExist) case RepoRefCommit: parts := strings.Split(path, "/") - if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= 40 { + hasShaLength := len(parts[0]) >= 7 && len(parts[0]) <= 40 + if len(parts) > 1 && hasShaLength && ctx.Repo.GitRepo.IsCommitExist(parts[0]) { ctx.Repo.TreePath = strings.Join(parts[1:], "/") return parts[0] } From e1e33a8520e4ea8fec7e9e5ead5b66e6a9487dd8 Mon Sep 17 00:00:00 2001 From: Norwin Date: Wed, 29 Sep 2021 23:04:51 +0200 Subject: [PATCH 4/4] add test for short shas --- integrations/api_repo_raw_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/integrations/api_repo_raw_test.go b/integrations/api_repo_raw_test.go index 77b33473c82dc..07533fe77e5d5 100644 --- a/integrations/api_repo_raw_test.go +++ b/integrations/api_repo_raw_test.go @@ -23,6 +23,7 @@ func TestAPIReposRaw(t *testing.T) { "master", // Branch "v1.1", // Tag "65f1bf27bc3bf70f64657658635e66094edbcb4d", // Commit + "65f1bf2", // Shortened Commit } { req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/raw/%s/README.md?token="+token, user.Name, ref) session.MakeRequest(t, req, http.StatusOK)