Merge branch 'dev_trustie' into dev_chain

This commit is contained in:
sylor_huang@126.com 2020-06-29 17:07:38 +08:00
commit 86c6a7bbdd
21 changed files with 213 additions and 53 deletions

View File

@ -1,7 +1,8 @@
class ProjectsController < ApplicationController class ProjectsController < ApplicationController
include ApplicationHelper include ApplicationHelper
include OperateProjectAbilityAble include OperateProjectAbilityAble
before_action :require_login, except: %i[index branches group_type_list] include ProjectsHelper
before_action :require_login, except: %i[index branches group_type_list simple]
before_action :find_project_with_id, only: %i[show branches update destroy fork_users praise_users watch_users] before_action :find_project_with_id, only: %i[show branches update destroy fork_users praise_users watch_users]
before_action :authorizate_user_can_edit_project!, only: %i[update] before_action :authorizate_user_can_edit_project!, only: %i[update]
before_action :project_public?, only: %i[fork_users praise_users watch_user] before_action :project_public?, only: %i[fork_users praise_users watch_user]
@ -98,6 +99,12 @@ class ProjectsController < ApplicationController
@fork_users = paginate(fork_users) @fork_users = paginate(fork_users)
end end
def simple
project = Project.includes(:owner).select(:id, :name, :identifier, :user_id).find params[:id]
json_response(project)
end
private private
def project_params def project_params
params.permit(:user_id, :name, :description, :repository_name, params.permit(:user_id, :name, :description, :repository_name,

View File

@ -2,15 +2,15 @@ class PullRequestsController < ApplicationController
before_action :require_login, except: [:index, :show] before_action :require_login, except: [:index, :show]
before_action :find_project_with_id before_action :find_project_with_id
before_action :set_repository before_action :set_repository
before_action :find_pull_request, except: [:index, :new, :create, :check_can_merge] before_action :find_pull_request, except: [:index, :new, :create, :check_can_merge,:get_branches,:create_merge_infos]
before_action :get_relatived, only: [:new, :edit] # before_action :get_relatived, only: [:edit]
include TagChosenHelper include TagChosenHelper
include ApplicationHelper include ApplicationHelper
def index def index
# @issues = Gitea::PullRequest::ListService.new(@user,@repository.try(:identifier)).call #通过gitea获取 # @issues = Gitea::PullRequest::ListService.new(@user,@repository.try(:identifier)).call #通过gitea获取
issues = @project.issues.issue_pull_request.issue_index_includes.includes(:pull_request) issues = @project.issues.issue_pull_request.issue_index_includes.includes(pull_request: :user)
issues = issues.where(is_private: false) unless current_user.present? && (current_user.admin? || @project.member?(current_user)) issues = issues.where(is_private: false) unless current_user.present? && (current_user.admin? || @project.member?(current_user))
@all_issues_size = issues.size @all_issues_size = issues.size
@open_issues_size = issues.joins(:pull_request).where(pull_requests: {status: 0}).size @open_issues_size = issues.joins(:pull_request).where(pull_requests: {status: 0}).size
@ -24,15 +24,28 @@ class PullRequestsController < ApplicationController
end end
def new def new
@all_branches = [] @all_branches = PullRequests::BranchesService.new(@user, @project).call
get_all_branches = Gitea::Repository::Branches::ListService.new(@user, @repository.try(:identifier)).call @is_fork = @project.forked_from_project_id.present?
if get_all_branches && get_all_branches.size > 0 @projects_names = [{
get_all_branches.each do |b| project_name: "#{@user.try(:show_real_name)}/#{@repository.try(:identifier)}",
@all_branches.push(b["name"]) project_id: @project.id
end }]
@merge_projects = @projects_names
fork_project = @project.fork_project if @is_fork
if fork_project.present?
@merge_projects.push({
project_name: "#{fork_project.owner.try(:show_real_name)}/#{fork_project.repository.try(:identifier)}",
project_id: fork_project.id
})
end end
end end
def get_branches
branch_result = PullRequests::BranchesService.new(@user, @project).call
render json: branch_result
# return json: branch_result
end
def create def create
if params[:title].nil? if params[:title].nil?
normal_status(-1, "名称不能为空") normal_status(-1, "名称不能为空")
@ -81,6 +94,8 @@ class PullRequestsController < ApplicationController
end end
def edit def edit
@fork_project_user = @project&.fork_project&.owner.try(:show_real_name)
@fork_project_identifier = @project&.fork_project&.repository.try(:identifier)
end end
@ -102,7 +117,7 @@ class PullRequestsController < ApplicationController
end end
if @issue.update_attributes(@issue_params) if @issue.update_attributes(@issue_params)
if @pull_request.update_attributes(@local_params) if @pull_request.update_attributes(@local_params.compact)
gitea_request = Gitea::PullRequest::UpdateService.new(current_user, @repository.try(:identifier), @requests_params, @pull_request.try(:gpid)).call gitea_request = Gitea::PullRequest::UpdateService.new(current_user, @repository.try(:identifier), @requests_params, @pull_request.try(:gpid)).call
if gitea_request if gitea_request
if params[:issue_tag_ids].present? if params[:issue_tag_ids].present?
@ -143,6 +158,10 @@ class PullRequestsController < ApplicationController
end end
end end
def create_merge_infos
get_relatived
end
def show def show
@user_permission = current_user.present? && current_user.logged? && (@issue.assigned_to_id == current_user.id || current_user.admin? ) @user_permission = current_user.present? && current_user.logged? && (@issue.assigned_to_id == current_user.id || current_user.admin? )
@issue_user = @issue.user @issue_user = @issue.user
@ -237,7 +256,9 @@ class PullRequestsController < ApplicationController
assignee: current_user.try(:login), assignee: current_user.try(:login),
assignees: ["#{params[:assigned_login].to_s}"], assignees: ["#{params[:assigned_login].to_s}"],
labels: params[:issue_tag_ids], labels: params[:issue_tag_ids],
due_date: Time.now due_date: Time.now,
fork_project_id: params[:fork_project_id],
is_original: params[:is_original]
}) })
@issue_params = { @issue_params = {
author_id: current_user.id, author_id: current_user.id,

View File

@ -8,14 +8,11 @@ class RepositoriesController < ApplicationController
before_action :find_repository_by_id, only: %i[commit sync_mirror tags] before_action :find_repository_by_id, only: %i[commit sync_mirror tags]
before_action :authorizate_user_can_edit_repo!, only: %i[sync_mirror] before_action :authorizate_user_can_edit_repo!, only: %i[sync_mirror]
before_action :get_ref, only: %i[entries sub_entries] before_action :get_ref, only: %i[entries sub_entries]
before_action :get_latest_commit, :get_ref, only: %i[entries sub_entries] before_action :get_statistics, only: %i[entries sub_entries]
def show def show
@user = current_user @user = current_user
@branches_count = Gitea::Repository::Branches::ListService.new(@project.owner, @project.identifier).call&.size @repo = @project.repository
@commits_count = Gitea::Repository::Commits::ListService.new(@project.owner.login, @project.identifier,
sha: params[:sha], page: params[:page], limit: params[:limit], token: current_user&.gitea_token).call[:total_count]
@tags_count = Gitea::Repository::Tags::ListService.new(current_user&.gitea_token, @project.owner.login, @project.identifier).call&.size
@result = Gitea::Repository::GetService.new(@project.owner, @project.identifier).call @result = Gitea::Repository::GetService.new(@project.owner, @project.identifier).call
@project_fork_id = @project.try(:forked_from_project_id) @project_fork_id = @project.try(:forked_from_project_id)
if @project_fork_id.present? if @project_fork_id.present?
@ -96,11 +93,13 @@ class RepositoriesController < ApplicationController
end end
def repo_hook def repo_hook
end end
def sync_mirror def sync_mirror
@repo&.mirror.set_status!(Mirror.statuses[:waiting]) return render_error("正在镜像中..") if @repo.mirror.warning?
@repo.sync_mirror!
SyncMirroredRepositoryJob.perform_later(@repo.id, current_user.id) SyncMirroredRepositoryJob.perform_later(@repo.id, current_user.id)
render_ok render_ok
end end
@ -124,9 +123,17 @@ class RepositoriesController < ApplicationController
# TODO 获取最新commit信息 # TODO 获取最新commit信息
def get_latest_commit def get_latest_commit
@latest_commit = Gitea::Repository::Commits::ListService.new(@project.owner.login, @project.identifier, Gitea::Repository::Commits::ListService.new(@project.owner.login, @project.identifier,
sha: get_ref, page: 1, limit: 1, token: current_user&.gitea_token).call sha: get_ref, page: 1, limit: 1, token: current_user&.gitea_token).call
@latest_commit = @latest_commit.blank? ? nil : @latest_commit[:body][0] end
def get_statistics
@branches_count = Gitea::Repository::Branches::ListService.new(@project.owner, @project.identifier).call&.size
@tags_count = Gitea::Repository::Tags::ListService.new(current_user&.gitea_token, @project.owner.login, @project.identifier).call&.size
latest_commit = get_latest_commit
@latest_commit = latest_commit[:body][0]
@commits_count = latest_commit[:total_count]
end end
def get_ref def get_ref

View File

@ -27,4 +27,18 @@ module ProjectsHelper
def find_user_by_login_or_mail(identifier) def find_user_by_login_or_mail(identifier)
(User.find_by_login identifier) || (User.find_by_mail identifier) (User.find_by_login identifier) || (User.find_by_mail identifier)
end end
def json_response(project)
json = {
identifier: project.identifier,
name: project.name,
id: project.id,
author: {
login: project.owner.login,
name: project.owner.real_name,
image_url: url_to_avatar(project.owner)
}
}
render json: json
end
end end

View File

@ -1,7 +1,7 @@
class Mirror < ApplicationRecord class Mirror < ApplicationRecord
# 0 - succeeded, 1 - waiting, 2 - failed # 0 - succeeded, 1 - waiting, 2 - failed
# 0: 同步镜像成功1: 正在同步镜像2: 同步失败默认值为0 # 0: 同步镜像成功1: 正在同步镜像2: 同步失败; 默认值为0
enum status: { succeeded: 0, waiting: 1, failed: 2 } enum status: { succeeded: 0, waiting: 1, failed: 2 }
belongs_to :repository, foreign_key: :repo_id belongs_to :repository, foreign_key: :repo_id

View File

@ -158,4 +158,8 @@ class Project < ApplicationRecord
member&.roles&.last&.name || permission member&.roles&.last&.name || permission
end end
def fork_project
Project.find_by(id: self.forked_from_project_id)
end
end end

View File

@ -15,4 +15,22 @@ class Repository < ApplicationRecord
def set_mirror! def set_mirror!
self.build_mirror(status: Mirror.statuses[:waiting]).save self.build_mirror(status: Mirror.statuses[:waiting]).save
end end
def mirror_status
self&.mirror&.numerical_for_status
end
def mirror_num
self&.mirror&.sync_num
end
def first_sync?
self&.mirror&.sync_num === 1
end
def sync_mirror!
repo_mirror = self.mirror
repo_mirror.set_status!(Mirror.statuses[:waiting])
repo_mirror.increment!(:sync_num)
end
end end

View File

@ -1,16 +1,33 @@
class Gitea::PullRequest::CreateService < Gitea::ClientService class Gitea::PullRequest::CreateService < Gitea::ClientService
attr_reader :token, :user, :repo, :params attr_reader :token, :user, :repo, :params
# params ex: # 同一个项目下发送pr例子如下
# { # 参数说明:
# title: 'pull request title', # user: 项目拥有者
# body: 'pull request content', # repo 项目名称
# head: 'develop', // from branch 源分支 # params:
# base: 'master' // to branch 目标分支 # {
# } # title: 'pull request title',
# 以上列子说明从develop分支合并到master分支 # body: 'pull request content',
# repo: 仓库名称 # head: 'develop', // from branch 源分支, 格式branch
# base: 'master' // to branch 目标分支
# }
# 以上列子说明从develop分支合并到master分支
# Gitea::PullRequest::CreateService.call('token', '项目拥有者', '项目名称', params)
# fork的项目向源项目发送pr例子如下
# 参数说明:
# user源项目拥有者
# repo源项目仓库名称
# params:
# {
# "base": "develop", // to branch 目标分支
# "head": "jasder:master", // from branch 源分支格式username:branch
# "body": "像源项目发送pr",
# "title": "jasder用户向源项目发送pr"
# }
# 以上例子说明jasder用户fork的项目master分支向源项目的develop分支发送pr
# Gitea::PullRequest::CreateService.call('token', '源项目拥有者', '源项目名称', params)
def initialize(token, user, repo, params={}) def initialize(token, user, repo, params={})
@token = token @token = token
@user = user @user = user
@ -19,13 +36,11 @@ class Gitea::PullRequest::CreateService < Gitea::ClientService
end end
def call def call
Rails.logger.info("######_____pr_url______#########{url}")
post(url, request_params) post(url, request_params)
end end
private private
def url def url
"/repos/#{@user.login}/#{@repo}/pulls".freeze "/repos/#{@user.login}/#{@repo}/pulls".freeze
end end

View File

@ -0,0 +1,34 @@
class PullRequests::BranchesService < ApplicationService
attr_reader :user, :project
def initialize(user, project)
@user = user
@project = project
end
def call
all_branches = []
user_name = user.try(:show_real_name)
identifier = project.repository.try(:identifier)
get_all_branches = Gitea::Repository::Branches::ListService.new(user, identifier).call
all_branches = branch_lists(user_name,user.try(:login), identifier, get_all_branches) if get_all_branches && get_all_branches.size > 0
return all_branches
end
def branch_lists(user_name,user_login, identifier, branches)
branches_array = []
branches.each do |b|
branch_params = {
user_name: user_name,
user_login: user_login,
identifier: identifier,
name: b["name"],
can_merge: b["user_can_merge"],
}
branches_array.push(branch_params)
end
return branches_array
end
end

View File

@ -10,7 +10,7 @@ class Repositories::MigrateService < ApplicationService
def call def call
@repository = Repository.new(repository_params) @repository = Repository.new(repository_params)
if @repository.save! if @repository.save!
@repository.set_mirror! if wrapper_mirror @repository.set_mirror!
MigrateRemoteRepositoryJob.perform_later(@repository.id, user.gitea_token, gitea_repository_params) MigrateRemoteRepositoryJob.perform_later(@repository.id, user.gitea_token, gitea_repository_params)
end end
@repository @repository

View File

@ -0,0 +1,2 @@
json.partial! "commons/success"
json.partial! "pull_requests/merge_item"

View File

@ -1,5 +1,9 @@
json.partial! "commons/success" json.partial! "commons/success"
json.partial! "pull_requests/merge_item" # json.partial! "pull_requests/merge_item"
json.extract! @pull_request, :id, :title, :body, :milestone,:head,:base json.fork_project_user @fork_project_user
json.fork_project_identifier @fork_project_identifier
json.project_author @project.owner.try(:show_real_name)
json.project_name @project.repository.try(:identifier)
json.extract! @pull_request, :id, :title, :body, :milestone,:head,:base,:is_original
json.extract! @issue, :assigned_to_id, :fixed_version_id, :priority_id json.extract! @issue, :assigned_to_id, :fixed_version_id, :priority_id
json.issue_tag_ids @issue.issue_tags_value.split(",") json.issue_tag_ids @issue.issue_tags_value.split(",")

View File

@ -16,6 +16,9 @@ json.issues do
json.pull_request_head pr.head json.pull_request_head pr.head
json.pull_request_base pr.base json.pull_request_base pr.base
json.pull_request_staus pr.status == 1 ? "merged" : (pr.status == 2 ? "closed" : "open") json.pull_request_staus pr.status == 1 ? "merged" : (pr.status == 2 ? "closed" : "open")
json.is_original pr.is_original
json.fork_project_id pr.fork_project_id
json.pull_request_user pr.user.try(:show_real_name)
json.id issue.id json.id issue.id
json.name issue.subject json.name issue.subject

View File

@ -1,3 +1,7 @@
json.partial! "commons/success" json.partial! "commons/success"
json.partial! "pull_requests/merge_item" json.project_id @project.id
json.branches @all_branches json.branches @all_branches
json.is_fork @is_fork
json.projects_names @projects_names
json.merge_projects @merge_projects
# json.merge_branches @merge_branches

View File

@ -1,9 +1,11 @@
json.partial! "commons/success" json.partial! "commons/success"
json.project_name @project.name json.project_name @project.name
json.pr_time time_from_now(@pull_request.updated_at) json.pr_time time_from_now(@pull_request.updated_at)
json.pull_request do json.pull_request do
json.extract! @pull_request, :id,:base, :head, :status json.extract! @pull_request, :id,:base, :head, :status,:fork_project_id, :is_original
json.pull_request_staus @pull_request.status == 1 ? "merged" : (@pull_request.status == 2 ? "closed" : "open") json.pull_request_staus @pull_request.status == 1 ? "merged" : (@pull_request.status == 2 ? "closed" : "open")
json.pull_request_user @pull_request.user.try(:show_real_name)
end end
json.issue do json.issue do

View File

@ -5,6 +5,9 @@ json.last_commit do
json.nil! json.nil!
end end
end end
json.tags_count @tags_count
json.branches_count @branches_count
json.commits_count @commits_count
json.zip_url render_zip_url(@project, @ref) json.zip_url render_zip_url(@project, @ref)
json.tar_url render_tar_url(@project, @ref) json.tar_url render_tar_url(@project, @ref)
json.entries do json.entries do

View File

@ -1,7 +1,7 @@
json.identifier @project.identifier json.identifier @project.identifier
json.name @project.name json.name @project.name
json.project_id @project.id json.project_id @project.id
json.repo_id @project.repository.id json.repo_id @repo.id
json.issues_count @project.issues_count.to_i - @project.pull_requests_count.to_i json.issues_count @project.issues_count.to_i - @project.pull_requests_count.to_i
json.pull_requests_count @project.pull_requests_count json.pull_requests_count @project.pull_requests_count
json.project_identifier @project.identifier json.project_identifier @project.identifier
@ -12,14 +12,17 @@ json.versions_count @project.versions_count #里程碑数量
json.version_releases_count @project.releases_size(@user.try(:id), "all") json.version_releases_count @project.releases_size(@user.try(:id), "all")
json.version_releasesed_count @project.releases_size(@user.try(:id), "released") #已发行的版本 json.version_releasesed_count @project.releases_size(@user.try(:id), "released") #已发行的版本
json.contributor_users_count @project.contributor_users json.contributor_users_count @project.contributor_users
json.issue_tags_count @tags_count
json.branches_count @branches_count
json.commits_count @commits_count
json.permission @project.get_premission(@user) json.permission @project.get_premission(@user)
json.mirror_url @project&.repository.mirror_url json.mirror_url @project&.repository.mirror_url
json.mirror @project&.repository.mirror_url.present? json.mirror @project&.repository.mirror_url.present?
json.type @project.numerical_for_project_type json.type @project.numerical_for_project_type
json.mirror_status @project.repository&.mirror&.numerical_for_status if @project.sync_mirror?
unless @project.common?
json.mirror_status @repo.mirror_status
json.mirror_num @repo.mirror_num
json.first_sync @repo.first_sync?
end
json.watched @project.watched_by? @user json.watched @project.watched_by? @user
json.praised @project.praised_by? @user json.praised @project.praised_by? @user
json.status @project.status json.status @project.status
@ -32,12 +35,14 @@ json.fork_info do
end end
end end
json.size replace_bytes_to_b(number_to_human_size(@result['size'].to_i*1024)) if @result
json.ssh_url @result['ssh_url'] json.size replace_bytes_to_b(number_to_human_size(@result['size'].to_i*1024))
json.clone_url @result['clone_url'] json.ssh_url @result['ssh_url']
json.default_branch @result['default_branch'] json.clone_url @result['clone_url']
json.empty @result['empty'] json.default_branch @result['default_branch']
json.full_name @result['full_name'] json.empty @result['empty']
json.full_name @result['full_name']
json.private @result['private']
end
json.private @result['private']
json.partial! 'author', locals: { user: @project.owner } json.partial! 'author', locals: { user: @project.owner }

View File

@ -6,6 +6,9 @@ json.last_commit do
json.nil! json.nil!
end end
end end
json.tags_count @tags_count
json.branches_count @branches_count
json.commits_count @commits_count
json.entries do json.entries do
json.array! @sub_entries do |entry| json.array! @sub_entries do |entry|
json.partial! 'repositories/simple_entry', locals: { entry: entry } json.partial! 'repositories/simple_entry', locals: { entry: entry }

View File

@ -32,8 +32,8 @@ Rails.application.routes.draw do
delete 'commons/delete', to: 'commons#delete' delete 'commons/delete', to: 'commons#delete'
resources :issues, except: [:index, :new,:create, :update, :edit, :destroy] do resources :issues, except: [:index, :new,:create, :update, :edit, :destroy] do
resources :journals, only: [:index, :create, :destroy, :edit, :update] do resources :journals, only: [:index, :create, :destroy, :edit, :update] do
member do member do
get :get_children_journals get :get_children_journals
end end
end end
@ -69,6 +69,8 @@ Rails.application.routes.draw do
end end
collection do collection do
post :check_can_merge post :check_can_merge
get :create_merge_infos
get :get_branches
end end
end end
resources :version_releases, only: [:index,:new, :create, :edit, :update, :destroy] resources :version_releases, only: [:index,:new, :create, :edit, :update, :destroy]
@ -118,6 +120,7 @@ Rails.application.routes.draw do
get :watch_users get :watch_users
get :praise_users get :praise_users
get :fork_users get :fork_users
get :simple
end end
end end

View File

@ -0,0 +1,5 @@
class AddSyncNumToMirrors < ActiveRecord::Migration[5.2]
def change
add_column :mirrors, :sync_num, :integer, default: 1
end
end

View File

@ -0,0 +1,6 @@
class AddOriginProjectIdToPullRequest < ActiveRecord::Migration[5.2]
def change
add_column :pull_requests, :fork_project_id, :integer
add_column :pull_requests, :is_original, :boolean, default: false
end
end