新增: 批量更改文件

This commit is contained in:
yystopf 2022-07-19 16:47:01 +08:00
parent 1882120df3
commit 25afedcdfd
10 changed files with 816 additions and 261 deletions

View File

@ -0,0 +1,13 @@
class Api::V1::Projects::ContentsController < Api::V1::BaseController
before_action :require_operate_above, only: [:batch]
def batch
@result_object = Api::V1::Projects::Contents::BatchCreateService.call(@project, batch_content_params, current_user&.gitea_token)
puts @result_object
end
private
def batch_content_params
params.require(:content).permit(:author_email, :author_name, :author_timeunix, :branch, :committer_email, :committer_name, :committer_timeunix, :message, :new_branch, files: [ :action_type, :content, :encoding, :file_path])
end
end

View File

@ -1093,6 +1093,138 @@ await octokit.request('GET /api/yystopf/ceshi/files.json')
]
```
## 提交文件到仓库
提交文件到仓库,支持批量
> 示例:
```shell
curl -X POST \
-d "files[][action_type]=create" \
-d "files[][content]=jfksj" \
-d "files[][encoding]=text" \
-d "files[][file_path]=heihei8" \
-d "author_email=yystopf@163.com" \
-d "author_name=yystopf" \
-d "author_timeunix=1658214400" \
-d "committer_email=yystopf@163.com" \
-d "committer_name=yystopf" \
-d "committer_timeunix=1658214400" \
-d "branch=develop" \
-d "new_branch=develop_ceshi" \
-d "message=测试提交" \
http://localhost:3000/api/v1/yystopf/ceshi/contents/batch.json
```
```javascript
await octokit.request('POST /api/v1/yystopf/ceshi/contents/batch.json')
```
### HTTP 请求
`POST /api/v1/:owner/:repo/contents/batch`
### 请求参数:
参数 | 必选 | 默认 | 类型 | 字段说明
--------- | ------- | ------- | -------- | ----------
|owner |是| |string |用户登录名 |
|repo |是| |string |项目标识identifier |
|files.action_type |是| |string|操作类型 create: 创建 update: 更新 delete: 删除|
|files.content |是| |string|文件内容|
|files.encoding |是| |string|文件编码方式 text 文本 base64 加密|
|files.file_path |是| |string|文件路径|
|author_email |是| |string|作者邮箱|
|author_name |是| |string|作者名称|
|author_timeunix |是| |int|编码时间,精确到秒|
|committer_email |是| |string|提交者邮箱|
|committer_name |是| |string|提交者名称|
|committer_timeunix|是| |int|提交时间戳,精确到秒|
|branch |是| |string|提交分支|
|new_branch |否| |string|如果需要创建新分支,这个需要填|
|message |是| |string|提交信息|
> 请求的JSON示例:
```json
{
"files": [
{
"action_type": "create",
"content": "jfksj",
"encoding": "text",
"file_path": "heihei7"
}
],
"author_email": "yystopf@163.com",
"author_name": "yystopf",
"author_timeunix": 1658214400,
"committer_email": "yystopf@163.com",
"committer_name": "yystopf",
"committer_timeunix": 1658214400,
"branch": "hh_ceshi",
"message": "测试提交"
}
```
### 返回字段说明:
参数 | 类型 | 字段说明
--------- | ----------- | -----------
|commit.sha |string |提交ID|
|commit.author |object |作者|
|commit.committer |object |提交者|
|commit.commit_message|string |提交信息|
|commit.parent_shas |array |父节点commit ID|
|commit.authored_time|int |编码时间|
|commit.commited_time|int |提交时间|
|contents.name |string |文件名称|
|contents.path |string |文件路径|
|contents.sha |string |文件标识|
|contents.type |string |文件类型|
|contents.size |int |文件大小|
|contents.url |string |文件地址|
|contents.encoding |string |编码类型 text 文本 base64 加密 |
|contents.content |string |文件内容|
> 返回的JSON示例:
```json
{
"commit": {
"sha": "7c1e25f9b974e4b7a3816bd7f5e49b441078e999",
"author": {
"id": "2",
"login": "yystopf",
"name": "heh",
"type": "User",
"image_url": "system/lets/letter_avatars/2/H/188_239_142/120.png"
},
"committer": {
"id": "2",
"login": "yystopf",
"name": "heh",
"type": "User",
"image_url": "system/lets/letter_avatars/2/H/188_239_142/120.png"
},
"commit_message": "测试提交\n",
"parent_shas": [
"9aec816d0f3600082ca77893290a14bd29d805fe"
],
"authored_time": 1658214400,
"commited_time": 1658214400
},
"contents": [
{
"name": "heihei7",
"path": "heihei7",
"sha": "f0acac8efb3021b0f6a7b13b42d033d86e076a4b",
"type": "file",
"size": 5,
"encoding": "base64",
"content": "amZrc2o="
}
]
}
```
## 获取仓库代码目录
获取仓库代码目录

View File

@ -44,7 +44,7 @@ class Api::V1::Projects::Branches::CreateService < ApplicationService
end
def check_new_branch_exist
result = $gitea_client.get_repos_branch_name_set_by_owner_repo(owner, repo) rescue nil
result = $gitea_client.get_repos_branch_name_set_by_owner_repo(owner, repo, {query: request_params}) rescue nil
raise Error, '查询分支名称失败!' unless result.is_a?(Hash)
raise Error, '分支已存在!' if result['branch_name'].include?(@new_branch_name)
end

View File

@ -0,0 +1,91 @@
class Api::V1::Projects::Contents::BatchCreateService < ApplicationService
include ActiveModel::Model
attr_reader :project, :owner, :repo, :token
attr_reader :files, :author_email, :author_name, :author_timeunix, :branch, :committer_email, :committer_name, :committer_timeunix, :message, :new_branch
attr_accessor :gitea_data
validates :author_email, :committer_email, presence: true, format: { with: CustomRegexp::EMAIL }
validates :author_name, :committer_name, presence: true, format: { with: /\A(?!_)(?!.*?_$)[a-zA-Z0-9_-]{4,15}\z/ }
validates :author_timeunix, :committer_timeunix, presence: true
validates :branch, presence: true
validates :message, presence: true
def initialize(project, params, token=nil)
puts params
@project = project
@owner = project&.owner.login
@repo = project&.identifier
@token = token
@files = params[:files]
@author_email = params[:author_email]
@author_name = params[:author_name]
@author_timeunix = params[:author_timeunix]
@branch = params[:branch]
@committer_email = params[:committer_email]
@committer_name = params[:committer_name]
@committer_timeunix = params[:committer_timeunix]
@message = params[:message]
@new_branch = params[:new_branch]
end
def call
raise Error, '请输入正确的文件参数Files' unless valid_files?
raise Error, errors.full_messages.join(", ") unless valid?
check_branch_exist
excute_data_to_gitea
gitea_data
end
private
def request_params
{
access_token: token
}
end
def request_body
{
files: files,
header: {
author: {
email: author_email,
name: author_name
},
committer: {
email: committer_email,
name: committer_name
},
dates: {
author: Time.at(author_timeunix.to_i),
committer: Time.at(committer_timeunix.to_i)
},
message: message,
new_branch: new_branch,
signoff: false
}
}
end
def valid_files?
return false unless files.is_a?(Array) && !files.blank?
files.each do |file|
return false unless file.has_key?(:action_type) && file.has_key?(:content) && file.has_key?(:encoding) && file.has_key?(:file_path)
end
end
def excute_data_to_gitea
@gitea_data = $gitea_client.post_repos_contents_batch_by_owner_repo(owner, repo, {body: request_body.to_json, query: request_params}) rescue nil
raise Error, '创建文件失败!' unless @gitea_data.is_a?(Hash)
end
def check_branch_exist
result = $gitea_client.get_repos_branch_name_set_by_owner_repo(owner, repo, {query: request_params} ) rescue nil
raise Error, '查询分支名称失败!' unless result.is_a?(Hash)
raise Error, '分支不存在!' unless result['branch_name'].include?(branch)
raise Error, '分支已存在!' if result['branch_name'].include?(new_branch) && new_branch.nil?
end
end

View File

@ -12,9 +12,9 @@ json.blame_parts @result_object['blame_parts'] do |part|
json.partial! 'api/v1/users/commit_user', locals: { user: render_cache_commit_author(part['commit']['commiter']), name: part['commit']['commiter']['Name'] }
end
json.commit_message part['commit']['commit_message']
json.authored_time part['commit']['authored_time'].to_time.strftime("%Y-%m-%d %H:%M:%S")
json.committed_time part['commit']['committed_time'].to_time.strftime("%Y-%m-%d %H:%M:%S")
json.created_time part['commit']['created_time'].to_time.strftime("%Y-%m-%d %H:%M:%S")
json.authored_time render_unix_time(part['commit']['authored_time'])
json.committed_time render_unix_time(part['commit']['committed_time'])
json.created_time render_unix_time(part['commit']['created_time'])
end
json.current_number part['current_number']
json.effect_line part['effect_line']

View File

@ -0,0 +1,10 @@
json.sha commit['sha']
json.author do
json.partial! 'api/v1/users/commit_user', locals: { user: render_cache_commit_author(commit['author']), name: commit['author']['name'] }
end
json.committer do
json.partial! 'api/v1/users/commit_user', locals: { user: render_cache_commit_author(commit['committer']), name: commit['committer']['name'] }
end
json.commit_message commit['message']
json.parent_shas commit['parents'].map{|x|x['sha']}

View File

@ -12,6 +12,6 @@ json.commits @result_object[:data].each do |commit|
json.parent_shas commit['parents'].map{|x|x['sha']}
json.files commit['files'].map{|f|f['filename']}
json.commit_date commit['commit_date']
json.commit_time commit['commit']['committer']['date'].to_time.strftime("%Y-%m-%d %H:%M:%S")
json.commit_time render_unix_time(commit['commit']['committer']['date'])
json.branch commit['branch']
end

View File

@ -0,0 +1,14 @@
json.commit do
json.partial! "api/v1/projects/commits/simple_gitea_detail", commit: @result_object['commit']
json.authored_time render_unix_time(@result_object['commit']['author']['date'])
json.commited_time render_unix_time(@result_object['commit']['committer']['date'])
end
json.contents @result_object['contents'].each do |content|
json.name content['name']
json.path content['path']
json.sha content['sha']
json.type content['type']
json.size content['size']
json.encoding content['encoding']
json.content content['content']
end

View File

@ -37,6 +37,12 @@ defaults format: :json do
get '/commits/:sha/diff', to: 'commits#diff'
get '/git/blobs/:sha', to: 'git#blobs'
get '/git/trees/:sha', to: 'git#trees'
resources :contents, only: [:index] do
collection do
post :batch
end
end
end
end

File diff suppressed because it is too large Load Diff