ADD get readme api

This commit is contained in:
Jasder 2021-01-15 15:38:49 +08:00
parent 2253dce4d7
commit 232f26ea3b
4 changed files with 90 additions and 0 deletions

View File

@ -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
---

View File

@ -133,6 +133,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

View File

@ -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 repositorys 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

View File

@ -327,6 +327,7 @@ Rails.application.routes.draw do
post :sync_mirror
get :top_counts
get 'commits/:sha', to: 'repositories#commit', as: 'commit'
get 'readme'
end
end