parent
							
								
									ef25ccf3c4
								
							
						
					
					
						commit
						fabfe861ef
					
				| @ -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,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…
					
					
				
		Reference in new issue