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) diff --git a/modules/context/api.go b/modules/context/api.go index c978835af8749..7141bdf8272d3 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 88f4d2765825f..67fb2045fd074 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 @@ -731,7 +728,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] } diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 70d7cb40f888a..ce9ad7a495bd1 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -763,7 +763,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) 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 6bbc0934811b6..8b6257dfaf7d6 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -8237,7 +8237,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" } @@ -8252,6 +8252,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": [