diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 0c8aaf2c9..fdda05dee 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -1,7 +1,8 @@ class ProjectsController < ApplicationController include ApplicationHelper 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 :authorizate_user_can_edit_project!, only: %i[update] before_action :project_public?, only: %i[fork_users praise_users watch_user] @@ -98,6 +99,12 @@ class ProjectsController < ApplicationController @fork_users = paginate(fork_users) end + def simple + project = Project.includes(:owner).select(:id, :name, :identifier, :user_id).find params[:id] + + json_response(project) + end + private def project_params params.permit(:user_id, :name, :description, :repository_name, diff --git a/app/controllers/pull_requests_controller.rb b/app/controllers/pull_requests_controller.rb index b961f9f02..a4d802e09 100644 --- a/app/controllers/pull_requests_controller.rb +++ b/app/controllers/pull_requests_controller.rb @@ -2,15 +2,15 @@ class PullRequestsController < ApplicationController before_action :require_login, except: [:index, :show] before_action :find_project_with_id before_action :set_repository - before_action :find_pull_request, except: [:index, :new, :create, :check_can_merge] - before_action :get_relatived, only: [:new, :edit] + before_action :find_pull_request, except: [:index, :new, :create, :check_can_merge,:get_branches,:create_merge_infos] + # before_action :get_relatived, only: [:edit] include TagChosenHelper include ApplicationHelper def index # @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)) @all_issues_size = issues.size @open_issues_size = issues.joins(:pull_request).where(pull_requests: {status: 0}).size @@ -24,15 +24,28 @@ class PullRequestsController < ApplicationController end def new - @all_branches = [] - get_all_branches = Gitea::Repository::Branches::ListService.new(@user, @repository.try(:identifier)).call - if get_all_branches && get_all_branches.size > 0 - get_all_branches.each do |b| - @all_branches.push(b["name"]) - end + @all_branches = PullRequests::BranchesService.new(@user, @project).call + @is_fork = @project.forked_from_project_id.present? + @projects_names = [{ + project_name: "#{@user.try(:show_real_name)}/#{@repository.try(:identifier)}", + project_id: @project.id + }] + @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 + def get_branches + branch_result = PullRequests::BranchesService.new(@user, @project).call + render json: branch_result + # return json: branch_result + end + def create if params[:title].nil? normal_status(-1, "名称不能为空") @@ -81,6 +94,8 @@ class PullRequestsController < ApplicationController end def edit + @fork_project_user = @project&.fork_project&.owner.try(:show_real_name) + @fork_project_identifier = @project&.fork_project&.repository.try(:identifier) end @@ -102,7 +117,7 @@ class PullRequestsController < ApplicationController end 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 if gitea_request if params[:issue_tag_ids].present? @@ -143,6 +158,10 @@ class PullRequestsController < ApplicationController end end + def create_merge_infos + get_relatived + end + def show @user_permission = current_user.present? && current_user.logged? && (@issue.assigned_to_id == current_user.id || current_user.admin? ) @issue_user = @issue.user @@ -237,7 +256,9 @@ class PullRequestsController < ApplicationController assignee: current_user.try(:login), assignees: ["#{params[:assigned_login].to_s}"], 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 = { author_id: current_user.id, diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index f37a8e759..5143fcda8 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -8,14 +8,11 @@ class RepositoriesController < ApplicationController 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 :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 @user = current_user - @branches_count = Gitea::Repository::Branches::ListService.new(@project.owner, @project.identifier).call&.size - @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 + @repo = @project.repository @result = Gitea::Repository::GetService.new(@project.owner, @project.identifier).call @project_fork_id = @project.try(:forked_from_project_id) if @project_fork_id.present? @@ -96,11 +93,13 @@ class RepositoriesController < ApplicationController end def repo_hook - + end 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) render_ok end @@ -124,9 +123,17 @@ class RepositoriesController < ApplicationController # TODO 获取最新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 - @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 def get_ref diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index b9cd457e3..7079ae936 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -27,4 +27,18 @@ module ProjectsHelper def find_user_by_login_or_mail(identifier) (User.find_by_login identifier) || (User.find_by_mail identifier) 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 diff --git a/app/models/mirror.rb b/app/models/mirror.rb index 7ce4651ae..f5737b0f3 100644 --- a/app/models/mirror.rb +++ b/app/models/mirror.rb @@ -1,7 +1,7 @@ class Mirror < ApplicationRecord # 0 - succeeded, 1 - waiting, 2 - failed - # 0: 同步镜像成功;1: 正在同步镜像;2: 同步失败,默认值为0 + # 0: 同步镜像成功;1: 正在同步镜像;2: 同步失败; 默认值为0 enum status: { succeeded: 0, waiting: 1, failed: 2 } belongs_to :repository, foreign_key: :repo_id diff --git a/app/models/project.rb b/app/models/project.rb index 09f1705be..1d6fd0327 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -158,4 +158,8 @@ class Project < ApplicationRecord member&.roles&.last&.name || permission end + def fork_project + Project.find_by(id: self.forked_from_project_id) + end + end diff --git a/app/models/repository.rb b/app/models/repository.rb index 0cd5ed511..6d20afce5 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -15,4 +15,22 @@ class Repository < ApplicationRecord def set_mirror! self.build_mirror(status: Mirror.statuses[:waiting]).save 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 diff --git a/app/services/gitea/pull_request/create_service.rb b/app/services/gitea/pull_request/create_service.rb index c3a83d1b9..3cf8dde0a 100644 --- a/app/services/gitea/pull_request/create_service.rb +++ b/app/services/gitea/pull_request/create_service.rb @@ -1,16 +1,33 @@ - class Gitea::PullRequest::CreateService < Gitea::ClientService attr_reader :token, :user, :repo, :params - # params ex: - # { - # title: 'pull request title', - # body: 'pull request content', - # head: 'develop', // from branch 源分支 - # base: 'master' // to branch 目标分支 - # } - # 以上列子说明从develop分支合并到master分支 - # repo: 仓库名称 + # 同一个项目下发送pr例子,如下: + # 参数说明: + # user: 项目拥有者 + # repo: 项目名称 + # params: + # { + # title: 'pull request title', + # body: 'pull request content', + # 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={}) @token = token @user = user @@ -19,13 +36,11 @@ class Gitea::PullRequest::CreateService < Gitea::ClientService end def call - Rails.logger.info("######_____pr_url______#########{url}") post(url, request_params) end private - def url "/repos/#{@user.login}/#{@repo}/pulls".freeze end diff --git a/app/services/pull_requests/branches_service.rb b/app/services/pull_requests/branches_service.rb new file mode 100644 index 000000000..117f17729 --- /dev/null +++ b/app/services/pull_requests/branches_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/repositories/migrate_service.rb b/app/services/repositories/migrate_service.rb index dad7e3930..e2f520aeb 100644 --- a/app/services/repositories/migrate_service.rb +++ b/app/services/repositories/migrate_service.rb @@ -10,7 +10,7 @@ class Repositories::MigrateService < ApplicationService def call @repository = Repository.new(repository_params) if @repository.save! - @repository.set_mirror! if wrapper_mirror + @repository.set_mirror! MigrateRemoteRepositoryJob.perform_later(@repository.id, user.gitea_token, gitea_repository_params) end @repository diff --git a/app/views/pull_requests/create_merge_infos.json.jbuilder b/app/views/pull_requests/create_merge_infos.json.jbuilder new file mode 100644 index 000000000..1df3b7e97 --- /dev/null +++ b/app/views/pull_requests/create_merge_infos.json.jbuilder @@ -0,0 +1,2 @@ +json.partial! "commons/success" +json.partial! "pull_requests/merge_item" \ No newline at end of file diff --git a/app/views/pull_requests/edit.json.jbuilder b/app/views/pull_requests/edit.json.jbuilder index 373c88cd4..aad5c26b0 100644 --- a/app/views/pull_requests/edit.json.jbuilder +++ b/app/views/pull_requests/edit.json.jbuilder @@ -1,5 +1,9 @@ json.partial! "commons/success" -json.partial! "pull_requests/merge_item" -json.extract! @pull_request, :id, :title, :body, :milestone,:head,:base +# json.partial! "pull_requests/merge_item" +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.issue_tag_ids @issue.issue_tags_value.split(",") diff --git a/app/views/pull_requests/index.json.jbuilder b/app/views/pull_requests/index.json.jbuilder index 34b84dbff..5b46c77e3 100644 --- a/app/views/pull_requests/index.json.jbuilder +++ b/app/views/pull_requests/index.json.jbuilder @@ -16,6 +16,9 @@ json.issues do json.pull_request_head pr.head json.pull_request_base pr.base 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.name issue.subject diff --git a/app/views/pull_requests/new.json.jbuilder b/app/views/pull_requests/new.json.jbuilder index b2ab0c150..ab0e87135 100644 --- a/app/views/pull_requests/new.json.jbuilder +++ b/app/views/pull_requests/new.json.jbuilder @@ -1,3 +1,7 @@ json.partial! "commons/success" -json.partial! "pull_requests/merge_item" +json.project_id @project.id json.branches @all_branches +json.is_fork @is_fork +json.projects_names @projects_names +json.merge_projects @merge_projects +# json.merge_branches @merge_branches \ No newline at end of file diff --git a/app/views/pull_requests/show.json.jbuilder b/app/views/pull_requests/show.json.jbuilder index 31dec7d86..a294b91c3 100644 --- a/app/views/pull_requests/show.json.jbuilder +++ b/app/views/pull_requests/show.json.jbuilder @@ -1,9 +1,11 @@ json.partial! "commons/success" json.project_name @project.name json.pr_time time_from_now(@pull_request.updated_at) + 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_user @pull_request.user.try(:show_real_name) end json.issue do diff --git a/app/views/repositories/entries.json.jbuilder b/app/views/repositories/entries.json.jbuilder index b246de32f..7c7b5ef70 100644 --- a/app/views/repositories/entries.json.jbuilder +++ b/app/views/repositories/entries.json.jbuilder @@ -5,6 +5,9 @@ json.last_commit do json.nil! 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.tar_url render_tar_url(@project, @ref) json.entries do diff --git a/app/views/repositories/show.json.jbuilder b/app/views/repositories/show.json.jbuilder index d01d842e7..eecd37524 100644 --- a/app/views/repositories/show.json.jbuilder +++ b/app/views/repositories/show.json.jbuilder @@ -1,7 +1,7 @@ json.identifier @project.identifier json.name @project.name 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.pull_requests_count @project.pull_requests_count 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_releasesed_count @project.releases_size(@user.try(:id), "released") #已发行的版本 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.mirror_url @project&.repository.mirror_url json.mirror @project&.repository.mirror_url.present? 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.praised @project.praised_by? @user json.status @project.status @@ -32,12 +35,14 @@ json.fork_info do end end -json.size replace_bytes_to_b(number_to_human_size(@result['size'].to_i*1024)) -json.ssh_url @result['ssh_url'] -json.clone_url @result['clone_url'] -json.default_branch @result['default_branch'] -json.empty @result['empty'] -json.full_name @result['full_name'] +if @result + json.size replace_bytes_to_b(number_to_human_size(@result['size'].to_i*1024)) + json.ssh_url @result['ssh_url'] + json.clone_url @result['clone_url'] + json.default_branch @result['default_branch'] + 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 } diff --git a/app/views/repositories/sub_entries.json.jbuilder b/app/views/repositories/sub_entries.json.jbuilder index 4ac253465..43dbf764e 100644 --- a/app/views/repositories/sub_entries.json.jbuilder +++ b/app/views/repositories/sub_entries.json.jbuilder @@ -6,6 +6,9 @@ json.last_commit do json.nil! end end +json.tags_count @tags_count +json.branches_count @branches_count +json.commits_count @commits_count json.entries do json.array! @sub_entries do |entry| json.partial! 'repositories/simple_entry', locals: { entry: entry } diff --git a/config/routes.rb b/config/routes.rb index 974bef32d..f371cba6e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -32,8 +32,8 @@ Rails.application.routes.draw do delete 'commons/delete', to: 'commons#delete' resources :issues, except: [:index, :new,:create, :update, :edit, :destroy] do - resources :journals, only: [:index, :create, :destroy, :edit, :update] do - member do + resources :journals, only: [:index, :create, :destroy, :edit, :update] do + member do get :get_children_journals end end @@ -69,6 +69,8 @@ Rails.application.routes.draw do end collection do post :check_can_merge + get :create_merge_infos + get :get_branches end end resources :version_releases, only: [:index,:new, :create, :edit, :update, :destroy] @@ -118,6 +120,7 @@ Rails.application.routes.draw do get :watch_users get :praise_users get :fork_users + get :simple end end diff --git a/db/migrate/20200629020618_add_sync_num_to_mirrors.rb b/db/migrate/20200629020618_add_sync_num_to_mirrors.rb new file mode 100644 index 000000000..2e80687f9 --- /dev/null +++ b/db/migrate/20200629020618_add_sync_num_to_mirrors.rb @@ -0,0 +1,5 @@ +class AddSyncNumToMirrors < ActiveRecord::Migration[5.2] + def change + add_column :mirrors, :sync_num, :integer, default: 1 + end +end diff --git a/db/migrate/20200629081111_add_origin_project_id_to_pull_request.rb b/db/migrate/20200629081111_add_origin_project_id_to_pull_request.rb new file mode 100644 index 000000000..fa02f66ae --- /dev/null +++ b/db/migrate/20200629081111_add_origin_project_id_to_pull_request.rb @@ -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