dev_forge
Jasder 5 years ago
parent ef25ccf3c4
commit fabfe861ef

@ -1078,10 +1078,11 @@ DELETE /api/:login/:repo_identifier/contents
*示例*
```
curl -X POST \
-d 'filepath=test_create_file.rb' \
-d 'branch=master' \
-d 'filepath=test1_create_file1.rb' \
-d 'branch=develop' \
-d 'content=ZnNmc2FkZg==' \
http://localhost:3000/api/18816895620/mirror_demo/contents | jq
-d 'message=test commit ' \
http://localhost:3000/api/18816895620/mirror_demo/contents.json | jq
```
*请求参数说明:*
@ -1100,14 +1101,41 @@ http://localhost:3000/api/18816895620/mirror_demo/contents | jq
|参数名|类型|说明|
-|-|-
|status |int|0:点赞成功,-1:操作失败2:表示还未点赞|
|name |string|文件名|
|sha |string|提交文件的sha值|
|size |int|文件大小, 单位B|
|content |string|base64编码后的文件内容|
|encoding |string|编码方式|
|commit |object||
|-- message |string|提交备注说明信息|
|-- committer|object||
|---- name |string|用户名|
|---- email |string|用户邮箱|
|---- date |string|文件创建时间|
返回值
```
{
"status": 0,
"message": "响应成功"
"name": "test1_create_file12.rb",
"sha": "7b70509105b587e71f5692b9e8ab70851e321f64",
"size": 12,
"content": "Wm5ObWMyRmtaZz09",
"encoding": "base64",
"commit": {
"message": "good luck\n",
"author": {
"name": "18816895620",
"email": "2456233122@qq.com",
"date": "2020-01-07T03:31:20Z"
},
"committer": {
"name": "18816895620",
"email": "2456233122@qq.com",
"date": "2020-01-07T03:31:20Z"
}
}
}
```
---

@ -0,0 +1,31 @@
class ContentsController < ApplicationController
before_action :find_user, :find_repository
before_action :require_login, only: %i[create]
def create
# request_params = {user: @user, repo_name: @repo.identifier, filepath: params[:filepath]}
logger.info "current_user ----> #{current_user.login}"
@user = current_user
interactor = Gitea::CreateFileInteractor.call(@user, content_params)
if interactor.success?
@file = interactor.result
logger.info "@file ========> #{@file}"
else
render_error(interactor.error)
end
end
def update_file
end
def delete_file
end
private
def content_params
params.permit(:login, :repo_identifier, :filepath, :branch, :content, :message, :new_branch)
end
end

@ -0,0 +1,13 @@
class Contents::CreateForm < BaseForm
attr_accessor :login, :repo_identifier, :filepath, :branch, :new_branch
validates :login, :repo_identifier, :filepath, presence: true
validate :check_branch
def check_branch
raise "branch和new_branch必须存在一个 " if branch.blank? && new_branch.blank?
raise "branch和new_branch只能存在一个" if !branch.blank? && !new_branch.blank?
end
end

@ -0,0 +1,2 @@
module ContentsHelper
end

@ -0,0 +1,64 @@
module Gitea
class CreateFileInteractor
def self.call(user, params={})
interactor = new(user, params)
interactor.run
interactor
end
attr_reader :error, :result
def initialize(user, params)
@user = user
@params = params
end
def success?
@error.nil?
end
def result
@result
end
def run
Contents::CreateForm.new(valid_params).validate!
response = Gitea::Repository::Entries::CreateService.new(user, @params[:repo_identifier], @params[:filepath], file_params).call
render_result(response)
rescue Exception => exception
Rails.logger.info "Exception ===========> #{exception.message}"
fail!(exception.message)
end
private
attr_reader :params, :user
def fail!(error)
@error = error
end
def render_result(response)
@result = response
end
def valid_params
{
login: @params[:login],
repo_identifier: @params[:repo_identifier],
filepath: @params[:filepath],
branch: @params[:branch],
new_branch: @params[:new_branch]
}
end
def file_params
file_params = {}
file_params = file_params.merge(branch: @params[:branch]) unless @params[:branch].blank?
file_params = file_params.merge(new_branch: @params[:new_branch]) unless @params[:new_branch].blank?
file_params = file_params.merge(content: Base64.encode64(@params[:content]))
file_params = file_params.merge(message: @params[:message]) unless @params[:message].blank?
end
end
end

@ -0,0 +1,43 @@
class Gitea::Repository::Entries::CreateService < Gitea::ClientService
attr_reader :user, :repo_name, :filepath, :body
# ref: The name of the commit/branch/tag. Default the repositorys default branch (usually master)
# filepath: path of the dir, file, symlink or submodule in the repo
# repo_name: the name of repository
# body:
# {
# "author": {
# "email": "user@example.com",
# "name": "string"
# },
# "branch": "string",
# "committer": {
# "email": "user@example.com",
# "name": "string"
# },
# "content": "string", # content must be base64 encoded
# "message": "string",
# "new_branch": "string"
# }
#
def initialize(user, repo_name, filepath, body)
@user = user
@repo_name = repo_name
@filepath = filepath
@body = body
end
def call
post(url, params)
end
private
def params
Hash.new.merge(token: user.gitea_token, data: body)
end
def url
"/repos/#{user.login}/#{repo_name}/contents/#{filepath}".freeze
end
end

@ -0,0 +1,33 @@
class Gitea::Repository::Tags::ListService < Gitea::ClientService
attr_reader :user, :repo_name
# ref: The name of the commit/branch/tag. Default the repositorys default branch (usually master)
# repo_name: the name of repository
def initialize(user, repo_name)
@user = user
@repo_name = repo_name
end
def call
response = get(url, params)
render_result(response)
end
private
def params
Hash.new.merge(token: user.gitea_token)
end
def url
"/repos/#{user.login}/#{repo_name}/tags".freeze
end
def render_result(response)
body = JSON.parse(response.body)
case response.status
when 200 then body
else
{status: -1, message: "#{body['message']}"}
end
end
end

@ -0,0 +1,10 @@
json.name @file['content']['name']
json.sha @file['content']['sha']
json.size @file['content']['size']
json.content @file['content']['content']
json.encoding @file['content']['encoding']
json.commit do
json.message @file['commit']['message']
json.author @file['commit']['author']
json.committer @file['commit']['committer']
end

@ -0,0 +1,9 @@
json.array! @tags do |tag|
json.name tag['name']
json.id tag['id']
json.zipball_url tag['zipball_url']
json.tarball_url tag['tarball_url']
json.commit do
json.sha tag['commit']['sha']
end
end
Loading…
Cancel
Save