From 6fa4ee6c14833ca5869a77827fc6c819f841e9fa Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Fri, 15 Jan 2021 15:38:49 +0800 Subject: [PATCH] ADD get readme api (cherry picked from commit 232f26ea3bf12aaca4b20e664ef72161392d87c5) --- api_document.md | 43 +++++++++++++++++++ app/controllers/repositories_controller.rb | 7 +++ .../gitea/repository/readme/get_service.rb | 39 +++++++++++++++++ config/routes.rb | 1 + 4 files changed, 90 insertions(+) create mode 100644 app/services/gitea/repository/readme/get_service.rb diff --git a/api_document.md b/api_document.md index 35618f799..da19c47ef 100644 --- a/api_document.md +++ b/api_document.md @@ -3062,6 +3062,49 @@ http://localhost:3000/api/trustie/truesite/protected_branches/master.json | jq ``` --- +#### 获取仓库README文件 +``` +GET api/:owner/:repo/readme +``` +*示例* +```bash +curl -X GET http://localhost:3000/api/trusite/trusite/readme | jq +``` + +*请求参数说明:* + +|参数名|类型|说明| +|-|-|-| +|owner |是|string |项目拥有者登录名 | +|repo |否|boolean |仓库名称 | +|ref |否|string |分支、tag或commit。默认: 仓库的默认分支(通常是master)| + + +*返回参数说明:* + +|参数名|类型|说明| +|-|-|-| +|name |string|文件名称| +|path |string|文件相对路径| +|type |string|文件类型, file:文件| +|size |int|文件大小 单位KB| +|content |string|文件内容,base64加密| + +返回值 +```json +{ + "type": "file", + "encoding": "base64", + "size": 13544, + "name": "README.md", + "path": "README.md", + "content": "Q2hpbmVzZSAmbmJzcDsgfCAmbmJzcDsgW0VuZ7i9yZWFkbWUvaW5kZXgucG5" +} +``` +--- + + + ### DevOps相关api --- diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index c189bf24c..82f9e6c71 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -144,6 +144,13 @@ class RepositoriesController < ApplicationController render_ok end + def readme + result = Gitea::Repository::Readme::GetService.call(@owner.login, @repository.identifier, params[:ref], current_user&.gitea_token) + + @readme = result[:status] === :success ? result[:body] : nil + render json: @readme + end + private def find_project diff --git a/app/services/gitea/repository/readme/get_service.rb b/app/services/gitea/repository/readme/get_service.rb new file mode 100644 index 000000000..48e2d4475 --- /dev/null +++ b/app/services/gitea/repository/readme/get_service.rb @@ -0,0 +1,39 @@ +# Gets the preferred README for a repository. +class Gitea::Repository::Readme::GetService < Gitea::ClientService + attr_reader :owner, :repo, :ref, :token + + # owner: owner of the repo + # repo: name of the repo + # name: The name of the commit/branch/tag. Default: the repository’s default branch (usually master) + # eg: + # Gitea::Repository::Readme::GetService.call(user.login, repo.identifier, ref, user.gitea_token) + def initialize(owner, repo, ref, token=nil) + @owner = owner + @repo = repo + @ref = ref || 'master' + @token = token + end + + def call + response = get(url, params) + status, message, body = render_response(response) + json_format(status, message, body) + end + + private + def params + Hash.new.merge(token: token, ref: ref) + end + + def url + "/repos/#{owner}/#{repo}/readme".freeze + end + + def json_format(status, message, body) + case status + when 200 then success(body) + when 404 then error(message, 404) + else error(message, status) + end + end +end diff --git a/config/routes.rb b/config/routes.rb index c8d88d6dc..76a37dab0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -353,6 +353,7 @@ Rails.application.routes.draw do post :sync_mirror get :top_counts get 'commits/:sha', to: 'repositories#commit', as: 'commit' + get 'readme' end end