Merge branch 'develop' into standalone_develop

This commit is contained in:
yystopf 2024-03-05 09:47:48 +08:00
commit 5e1c1d8c87
22 changed files with 319 additions and 16 deletions

View File

@ -141,4 +141,4 @@ gem 'doorkeeper'
gem 'doorkeeper-jwt'
gem 'gitea-client', '~> 1.4.3'
gem 'gitea-client', '~> 1.4.6'

View File

@ -0,0 +1,31 @@
class Api::V1::Projects::Actions::ActionsController < Api::V1::Projects::Actions::BaseController
def index
begin
gitea_result = $gitea_hat_client.get_repos_actions_by_owner_repo(@project&.owner&.login, @project&.identifier)
@data = gitea_result[:data]["Workflows"]
rescue
@data = []
end
end
def disable
return render_error("请输入正确的流水线文件!") if params[:workflow].blank?
gitea_result = $gitea_hat_client.post_repos_actions_disable(@project&.owner&.login, @project&.identifier, {query: {workflow: params[:workflow]}}) rescue nil
if gitea_result
render_ok
else
render_error("禁用流水线失败")
end
end
def enable
return render_error("请输入正确的流水线文件!") if params[:workflow].blank?
gitea_result = $gitea_hat_client.post_repos_actions_enable(@project&.owner&.login, @project&.identifier, {query: {workflow: params[:workflow]}}) rescue nil
if gitea_result
render_ok
else
render_error("取消禁用流水线失败")
end
end
end

View File

@ -0,0 +1,4 @@
class Api::V1::Projects::Actions::BaseController < Api::V1::BaseController
before_action :require_public_and_member_above
end

View File

@ -0,0 +1,12 @@
class Api::V1::Projects::Actions::RunsController < Api::V1::Projects::Actions::BaseController
def index
@result_object = Api::V1::Projects::Actions::Runs::ListService.call(@project, params[:workflow], current_user&.gitea_token)
end
def job_show
@result_object = Api::V1::Projects::Actions::Runs::JobShowService.call(@project, params[:run_id], params[:job], params[:log_cursors], current_user&.gitea_token)
puts @result_object
end
end

View File

@ -2,14 +2,14 @@ class Api::V1::Projects::BranchesController < Api::V1::BaseController
before_action :require_public_and_member_above, only: [:index, :all]
def index
@result_object = Api::V1::Projects::Branches::ListService.call(@project, {name: params[:keyword], page: page, limit: limit}, current_user&.gitea_token)
@result_object = Api::V1::Projects::Branches::ListService.call(@project, {name: params[:keyword], state: params[:state], page: page, limit: limit}, current_user&.gitea_token)
end
def all
@result_object = Api::V1::Projects::Branches::AllListService.call(@project, current_user&.gitea_token)
end
before_action :require_operate_above, only: [:create, :destroy]
before_action :require_operate_above, only: [:create, :destroy, :restore]
def create
@result_object = Api::V1::Projects::Branches::CreateService.call(@project, branch_params, current_user&.gitea_token)
@ -33,6 +33,15 @@ class Api::V1::Projects::BranchesController < Api::V1::BaseController
end
end
def restore
@result_object = Api::V1::Projects::Branches::RestoreService.call(@project, params[:branch_id], params[:branch_name], current_user&.gitea_token)
if @result_object
return render_ok
else
return render_error('恢复分支失败!')
end
end
before_action :require_manager_above, only: [:update_default_branch]
def update_default_branch

View File

@ -1,5 +1,5 @@
class Api::V1::Projects::CommitsController < Api::V1::BaseController
before_action :require_public_and_member_above, only: [:index, :diff]
before_action :require_public_and_member_above, only: [:index, :diff, :recent]
def index
@result_object = Api::V1::Projects::Commits::ListService.call(@project, {page: page, limit: limit, sha: params[:sha]}, current_user&.gitea_token)
@ -9,4 +9,8 @@ class Api::V1::Projects::CommitsController < Api::V1::BaseController
def diff
@result_object = Api::V1::Projects::Commits::DiffService.call(@project, params[:sha], current_user&.gitea_token)
end
def recent
@result_object = Api::V1::Projects::Commits::RecentService.call(@project, {page: page, limit: limit}, current_user&.gitea_token)
end
end

View File

@ -465,7 +465,7 @@ class User < Owner
$gitea_client.delete_users_tokens_by_username_token(self.login, e["name"], {query: {sudo: self.login} })
}
end
new_result = $gitea_client.post_users_tokens_by_username(self.login, { query: {sudo: self.login}, body:{ name: self.login} })
new_result = $gitea_client.post_users_tokens_by_username(self.login, { query: {sudo: self.login}, body:{ name: "#{self.login}-#{SecureRandom.hex(6)}", scopes: ["all"]}.to_json })
if new_result["sha1"].present?
update(gitea_token: new_result["sha1"])
end

View File

@ -0,0 +1,42 @@
class Api::V1::Projects::Actions::Runs::JobShowService < ApplicationService
include ActiveModel::Model
attr_reader :project, :token, :owner, :repo, :run, :job, :log_cursors
attr_accessor :gitea_data
validates :run, :job, :log_cursors, presence: true
def initialize(project, run, job, log_cursors, token = nil)
@project = project
@owner = project&.owner.login
@repo = project&.identifier
@run = run
@job = job
@log_cursors = log_cursors
@token = token
end
def call
raise Error, errors.full_messages.join(",") unless valid?
load_gitea_data
@gitea_data
end
private
def request_params
{
access_token: token
}
end
def request_body
{
logCursors: log_cursors
}
end
def load_gitea_data
@gitea_data = $gitea_hat_client.post_repos_actions_runs_jobs_by_owner_repo_run_job(owner, repo, run, job, {query: request_params, body: request_body.to_json})
end
end

View File

@ -0,0 +1,36 @@
class Api::V1::Projects::Actions::Runs::ListService < ApplicationService
include ActiveModel::Model
attr_reader :project, :token, :owner, :repo, :workflow
attr_accessor :gitea_data
validates :workflow, presence: true
def initialize(project, workflow, token =nil)
@project = project
@owner = project&.owner.login
@repo = project&.identifier
@workflow = workflow
@token = token
end
def call
raise Error, errors.full_messages.join(",") unless valid?
load_gitea_data
@gitea_data
end
private
def request_params
{
access_token: token,
workflow: workflow
}
end
def load_gitea_data
@gitea_data = $gitea_hat_client.get_repos_actions_by_owner_repo(owner, repo, {query: request_params}) rescue nil
raise Error, '获取流水线执行记录失败!' unless @gitea_data.is_a?(Hash)
end
end

View File

@ -1,6 +1,6 @@
class Api::V1::Projects::Branches::ListService < ApplicationService
attr_accessor :project, :token, :owner, :repo, :name, :page, :limit
attr_accessor :project, :token, :owner, :repo, :name, :state, :page, :limit
attr_accessor :gitea_data, :gitea_repo_data
def initialize(project, params, token=nil)
@ -9,6 +9,7 @@ class Api::V1::Projects::Branches::ListService < ApplicationService
@repo = project&.identifier
@token = token
@name = params[:name]
@state = params[:state]
@page = params[:page]
@limit = params[:limit]
end
@ -18,7 +19,6 @@ class Api::V1::Projects::Branches::ListService < ApplicationService
load_default_branch
@gitea_data[:default_branch] = @gitea_repo_data["default_branch"]
@gitea_data
end
@ -30,6 +30,7 @@ class Api::V1::Projects::Branches::ListService < ApplicationService
limit: limit
}
params.merge!({name: name}) if name.present?
params.merge!({state: state}) if state.present?
params
end

View File

@ -0,0 +1,47 @@
class Api::V1::Projects::Branches::RestoreService < ApplicationService
include ActiveModel::Model
attr_accessor :project, :token, :owner, :repo, :branch_id, :branch_name
attr_accessor :gitea_data
validates :branch_id, :branch_name, presence: true
def initialize(project, branch_id, branch_name, token= nil)
@project = project
@owner = project&.owner&.login
@repo = project&.identifier
@branch_id = branch_id
@branch_name = branch_name
@token = token
end
def call
raise Error, errors.full_messages.join(",") unless valid?
excute_data_to_gitea
true
end
private
def request_params
{
access_token: token
}
end
def request_body
{
branch_id: branch_id,
name: branch_name,
}
end
def excute_data_to_gitea
begin
@gitea_data = $gitea_hat_client.post_repos_branches_restore_by_owner_repo(owner, repo, {query: request_params, body: request_body.to_json})
rescue => e
raise Error, '恢复分支失败!'
end
end
end

View File

@ -0,0 +1,37 @@
class Api::V1::Projects::Commits::RecentService < ApplicationService
attr_reader :project, :page, :limit, :owner, :repo, :token
attr_accessor :gitea_data
def initialize(project, params, token=nil)
@project = project
@page = params[:page] || 1
@limit = params[:limit] || 15
@owner = project&.owner&.login
@repo = project&.identifier
@token = token
end
def call
load_gitea_data
gitea_data
end
private
def request_params
param = {
access_token: token,
page: page,
limit: limit
}
param
end
def load_gitea_data
@gitea_data = $gitea_hat_client.get_repos_recent_commits_by_owner_repo(owner, repo, {query: request_params}) rescue nil
raise Error, "获取最近提交列表失败" unless @gitea_data.is_a?(Hash)
end
end

View File

@ -29,6 +29,6 @@ class Api::V1::Projects::CompareService < ApplicationService
end
def load_gitea_data
@gitea_data = $gitea_client.get_repos_compare_by_owner_repo_from_to(owner, repo, from, to, {query: request_params}) rescue nil
@gitea_data = $gitea_hat_client.get_repos_compare_by_owner_repo_baseref_headref(owner, repo, to, from, {query: request_params}) rescue nil
end
end

View File

@ -29,7 +29,7 @@ class Gitea::User::GenerateTokenService < Gitea::ClientService
end
def request_params
{ name: "#{@username}-#{token_name}" }
{ name: "#{@username}-#{token_name}", scopes: ["all"] }
end
def token_name

View File

@ -0,0 +1,4 @@
json.total_count @data.size
json.files @data.each do |file|
json.name file["Name"]
end

View File

@ -0,0 +1,19 @@
json.total_data @result_object[:total_data].to_i
json.runs @result_object[:data]["Runs"].each do |run|
json.workflow run["WorkflowID"]
json.index run["Index"]
json.title run["Title"]
json.trigger_user do
json.partial! 'api/v1/users/commit_user', locals: { user: render_cache_commit_author(run['TriggerUser']), name: run['TriggerUser']['Name'] }
end
if run["Ref"].starts_with?("refs/tags")
json.ref run["Ref"].gsub!("/refs/tags/", "")
else
json.ref run["Ref"].gsub!("refs/heads/", "")
end
json.status run["Status"]
json.time_ago time_from_now(run["Stopped"])
json.holding_time run["Stopped"]-run["Started"]
end

View File

@ -0,0 +1,16 @@
json.state do
json.run do
json.title @result_object["state"]["run"]["title"]
json.status @result_object["state"]["run"]["status"]
json.done @result_object["state"]["run"]["done"]
json.jobs @result_object["state"]["run"]["jobs"]
json.current_job do
json.title @result_object["state"]["currentJob"]["title"]
json.detail @result_object["state"]["currentJob"]["detail"]
json.steps @result_object["state"]["currentJob"]["steps"]
end
end
end
json.logs do
json.steps_log @result_object["logs"]["stepsLog"]
end

View File

@ -24,3 +24,13 @@ json.default_branch default_branch || nil
json.http_url render_http_url(@project)
json.zip_url render_zip_url(@owner, @project.repository, branch['name'])
json.tar_url render_tar_url(@owner, @project.repository, branch['name'])
json.branch_id branch['id']
json.is_deleted branch['is_deleted']
json.deleted_unix branch['deleted_unix']
json.deleted_by do
if branch['is_deleted']
json.partial! 'api/v1/users/commit_user', locals: { user: render_cache_commit_author(branch['deleted_by']), name: branch['deleted_by']['name'] }
else
json.nil!
end
end

View File

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

View File

@ -14,5 +14,9 @@ json.commits @result_object['Commits'] do |commit|
json.parent_shas commit['Sha']['ParentShas']
end
json.diff do
if @result_object['Diff'].present?
json.partial! "api/v1/projects/simple_gitea_diff_detail", diff: @result_object['Diff']
else
json.nil!
end
end

View File

@ -1,6 +1,6 @@
json.total_count @result_object['total_count']
json.sha @result_object['sha']
json.entries @result_object['tree'].each do |entry|
json.total_count @result_object[:data]['total_count'].to_i
json.sha @result_object[:data]['sha']
json.entries @result_object[:data]['tree'].each do |entry|
json.name entry['path']
json.mode entry['mode']
json.type entry['type'] === 'blob' ? 'file' : 'dir'

View File

@ -78,6 +78,15 @@ defaults format: :json do
# projects文件夹下的
scope module: :projects do
resources :actions, module: 'actions' do
collection do
post :disable
post :enable
resources :runs, only: [:index] do
post '/jobs/:job', to: 'runs#job_show'
end
end
end
resources :pulls, module: 'pulls' do
resources :versions, only: [:index] do
member do
@ -98,6 +107,7 @@ defaults format: :json do
resources :branches, param: :name, only:[:index, :create, :destroy] do
collection do
get :all
post :restore
patch :update_default_branch
end
end
@ -107,7 +117,11 @@ defaults format: :json do
delete 'tags/*name', to: "tags#destroy", via: :all
get 'tags/*name', to: "tags#show", via: :all
resources :commits, only: [:index]
resources :commits, only: [:index] do
collection do
get :recent
end
end
resources :code_stats, only: [:index]
resources :contributors, only: [:index] do
collection do