| @@ -1,6 +1,7 @@ | |||
| class HooksController < ApplicationController | |||
| before_action :require_login, except: [:index, :show] | |||
| before_action :require_login | |||
| before_action :find_project_with_id | |||
| before_action :check_user | |||
| before_action :set_repository | |||
| def index | |||
| @@ -15,6 +16,7 @@ class HooksController < ApplicationController | |||
| end | |||
| def create | |||
| #根据gitea的api | |||
| # hook_params = { | |||
| # active: true, | |||
| # type: "gitea", | |||
| @@ -26,6 +28,7 @@ class HooksController < ApplicationController | |||
| # }, | |||
| # events: ["create", "pull", "push"], | |||
| # } | |||
| #根据gitea上hook的字段测试的 | |||
| # hook_params = { | |||
| # is_active: params[:is_active] || false, | |||
| # type: params[:type], | |||
| @@ -53,6 +56,8 @@ class HooksController < ApplicationController | |||
| hook_params = params[:hook_params] | |||
| Gitea::Hooks::CreateService.new(@user, @repository.try(:identifier), hook_params).call #创建gitea的hook功能 | |||
| Gitea::Hooks::CreateService.new(user, p.try(:identifier), hook_params).call #创建gitea的hook功能 | |||
| end | |||
| def update | |||
| @@ -82,4 +87,10 @@ class HooksController < ApplicationController | |||
| normal_status(-1, "仓库不存在") unless @repository.present? | |||
| normal_status(-1, "用户不存在") unless @user.present? | |||
| end | |||
| def check_user | |||
| unless @project.user_id == current_user.id | |||
| tip_exception(403, "您没有权限进入") | |||
| end | |||
| end | |||
| end | |||
| @@ -10,6 +10,7 @@ class RepositoriesController < ApplicationController | |||
| def show | |||
| @branches_count = Gitea::Repository::BranchesService.new(@project.owner, @project.identifier).call&.size | |||
| @commits_count = Gitea::Repository::Commits::ListService.new(@project.owner, @project.identifier).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 | |||
| @project_fork_id = @project.try(:forked_from_project_id) | |||
| if @project_fork_id.present? | |||
| @@ -8,7 +8,7 @@ class VersionReleasesController < ApplicationController | |||
| version_releases = Gitea::Versions::ListService.new(@user.gitea_token, @user.try(:login), @repository.try(:identifier)).call | |||
| @version_releases = version_releases | |||
| @user_permission = current_user.present? && (current_user == @user || current_user.admin?) | |||
| @forge_releases = @repository.version_releases.select(:id,:version_gid).includes(:attachments) | |||
| end | |||
| def new | |||
| @@ -34,14 +34,7 @@ class VersionReleasesController < ApplicationController | |||
| else | |||
| ActiveRecord::Base.transaction do | |||
| begin | |||
| version_params = { | |||
| body: params[:body], | |||
| draft: params[:draft] || false, | |||
| name: params[:name].to_s.first(32), | |||
| prerelease: params[:prerelease] || false, | |||
| tag_name: params[:tag_name], | |||
| target_commitish: params[:target_commitish] || "master" #分支 | |||
| } | |||
| version_params = releases_params | |||
| version_release = VersionRelease.new(version_params.merge(user_id: current_user.id, repository_id: @repository.id)) | |||
| if version_release.save! | |||
| git_version_release = Gitea::Versions::CreateService.new(@user.gitea_token, @user.try(:login), @repository.try(:identifier), version_params).call | |||
| @@ -54,6 +47,9 @@ class VersionReleasesController < ApplicationController | |||
| } | |||
| version_release.update_attributes!(update_params) | |||
| version_release.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "create") | |||
| if params[:attachment_ids].present? | |||
| create_attachments(params[:attachment_ids], version_release) | |||
| end | |||
| normal_status(0, "发布成功") | |||
| else | |||
| normal_status(-1, "发布失败") | |||
| @@ -70,7 +66,7 @@ class VersionReleasesController < ApplicationController | |||
| end | |||
| def edit | |||
| @version_attachments = @version.attachments | |||
| end | |||
| def update | |||
| @@ -81,19 +77,15 @@ class VersionReleasesController < ApplicationController | |||
| else | |||
| ActiveRecord::Base.transaction do | |||
| begin | |||
| version_params = { | |||
| body: params[:body], | |||
| draft: params[:draft] || false, | |||
| name: params[:name], | |||
| prerelease: params[:prerelease], | |||
| tag_name: params[:tag_name], | |||
| target_commitish: params[:target_commitish] || "master" #分支 | |||
| } | |||
| version_params = releases_params | |||
| if @version.update_attributes!(version_params) | |||
| create_attachments(params[:attachment_ids], @version) if params[:attachment_ids].present? | |||
| git_version_release = Gitea::Versions::UpdateService.new(@user.gitea_token, @user.try(:login), @repository.try(:identifier), version_params, @version.try(:version_gid)).call | |||
| unless git_version_release | |||
| raise Error, "更新失败" | |||
| end | |||
| normal_status(0, "更新成功") | |||
| else | |||
| normal_status(-1, "更新失败") | |||
| @@ -148,4 +140,27 @@ class VersionReleasesController < ApplicationController | |||
| end | |||
| end | |||
| def releases_params | |||
| { | |||
| body: params[:body], | |||
| draft: params[:draft] || false, | |||
| name: params[:name], | |||
| prerelease: params[:prerelease], | |||
| tag_name: params[:tag_name], | |||
| target_commitish: params[:target_commitish] || "master" #分支 | |||
| } | |||
| end | |||
| def create_attachments(attachment_ids, target) | |||
| attachment_ids.each do |id| | |||
| attachment = Attachment.select(:id, :container_id, :container_type)&.find_by_id(id) | |||
| unless attachment.blank? | |||
| attachment.container = target | |||
| attachment.author_id = current_user.id | |||
| attachment.description = "" | |||
| attachment.save | |||
| end | |||
| end | |||
| end | |||
| end | |||
| @@ -3,5 +3,5 @@ class VersionRelease < ApplicationRecord | |||
| belongs_to :user | |||
| has_many :project_trends, as: :trend, dependent: :destroy | |||
| scope :releases_size, ->{where(draft: false, prerelease: false).size} | |||
| # has_many :attachments, as: :container, dependent: :destroy | |||
| has_many :attachments, as: :container, dependent: :destroy | |||
| end | |||
| @@ -39,7 +39,7 @@ class Issues::ListQueryService < ApplicationService | |||
| issues = issues.joins(:issue_tags).where(issue_tags: {id: params[:issue_tag_id].to_i}) if params[:issue_tag_id].present? && params[:issue_tag_id].to_s != "all" | |||
| order_type = params[:order_type] || "desc" #或者"asc" | |||
| order_name = params[:order_name] || "created_on" #或者"updated_on" | |||
| order_name = params[:order_name] || "updated_on" #或者"updated_on" | |||
| issues.reorder("issues.#{order_name} #{order_type}") | |||
| end | |||
| @@ -14,7 +14,7 @@ json.issue_status @issue.issue_status.try(:name) | |||
| json.priority @issue.priority.try(:name) | |||
| json.version @issue.version.try(:name) | |||
| json.issue_tags @issue.get_issue_tags | |||
| json.done_ratio @issue.done_ratio.to_s + "%" | |||
| json.done_ratio @issue.done_ratio.to_s + "%" | |||
| json.issue_type @issue.issue_type == "1" ? "普通" : "悬赏" | |||
| json.token @issue.issue_type == "2" ? @issue.token : "" | |||
| json.join_users @join_users | |||
| @@ -12,7 +12,7 @@ json.versions_count @project.versions_count #里程碑数量 | |||
| json.version_releases_count @project.releases_size(current_user.try(:id), "all") | |||
| json.version_releasesed_count @project.releases_size(current_user.try(:id), "released") #已发行的版本 | |||
| json.contributor_users_count @project.contributor_users | |||
| json.issue_tags_count @project.issue_tags_count | |||
| json.issue_tags_count @tags_count | |||
| json.branches_count @branches_count | |||
| json.commits_count @commits_count | |||
| json.permission render_edit_project_permission(current_user, @project) if current_user | |||
| @@ -1,9 +1,12 @@ | |||
| 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'] | |||
| if tag.present? | |||
| 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 | |||
| end | |||
| @@ -1 +1,7 @@ | |||
| json.extract! @version, :id, :name, :body, :tag_name, :target_commitish, :draft, :prerelease,:version_gid | |||
| json.extract! @version, :id, :name, :body, :tag_name, :target_commitish, :draft, :prerelease,:version_gid | |||
| json.attachments do | |||
| json.array! @version_attachments do |attachment| | |||
| json.partial! "attachments/attachment_simple", locals: {attachment: attachment} | |||
| end | |||
| end | |||
| @@ -3,25 +3,10 @@ json.user_permission @user_permission | |||
| # json.releases @version_releases | |||
| json.releases do | |||
| json.array! @version_releases.to_a.each do |re| | |||
| user = User.select(:id, :gitea_uid, :login, :lastname,:firstname, :nickname).find_by_gitea_uid(re["author"]["id"]) | |||
| version = VersionRelease.select(:id).find_by_version_gid(re["id"]) | |||
| if @user_permission && re["draft"] | |||
| json.version_id version.try(:id) | |||
| json.id re["id"] | |||
| json.tag_name re["tag_name"] | |||
| json.target_commitish re["target_commitish"] | |||
| json.name re["name"] | |||
| json.body re["body"] | |||
| json.url re["url"] | |||
| json.tarball_url re["tarball_url"] | |||
| json.zipball_url re["zipball_url"] | |||
| json.draft re["draft"] ? "草稿" : (re["prerelease"] ? "预发行" : "稳定") | |||
| json.created_at format_time(re["created_at"].to_s.to_time) | |||
| json.published_at format_time(re["published_at"].to_s.to_time) | |||
| json.user_name user.present? ? user.try(:show_real_name) : "" | |||
| json.image_url user.present? ? url_to_avatar(user) : "" | |||
| else | |||
| unless re["draft"] | |||
| if re.present? | |||
| user = User.select(:id, :gitea_uid, :login, :lastname,:firstname, :nickname).find_by_gitea_uid(re["author"]["id"]) | |||
| version = @forge_releases.find_by(version_gid: re["id"]) | |||
| if @user_permission && re["draft"] | |||
| json.version_id version.try(:id) | |||
| json.id re["id"] | |||
| json.tag_name re["tag_name"] | |||
| @@ -36,8 +21,31 @@ json.releases do | |||
| json.published_at format_time(re["published_at"].to_s.to_time) | |||
| json.user_name user.present? ? user.try(:show_real_name) : "" | |||
| json.image_url user.present? ? url_to_avatar(user) : "" | |||
| else | |||
| unless re["draft"] | |||
| json.version_id version.try(:id) | |||
| json.id re["id"] | |||
| json.tag_name re["tag_name"] | |||
| json.target_commitish re["target_commitish"] | |||
| json.name re["name"] | |||
| json.body re["body"] | |||
| json.url re["url"] | |||
| json.tarball_url re["tarball_url"] | |||
| json.zipball_url re["zipball_url"] | |||
| json.draft re["draft"] ? "草稿" : (re["prerelease"] ? "预发行" : "稳定") | |||
| json.created_at format_time(re["created_at"].to_s.to_time) | |||
| json.published_at format_time(re["published_at"].to_s.to_time) | |||
| json.user_name user.present? ? user.try(:show_real_name) : "" | |||
| json.image_url user.present? ? url_to_avatar(user) : "" | |||
| end | |||
| end | |||
| json.attachments do | |||
| json.array! version.try(:attachments) do |attachment| | |||
| json.partial! "attachments/attachment_simple", locals: {attachment: attachment} | |||
| end | |||
| end | |||
| end | |||
| end | |||
| end | |||
| @@ -0,0 +1,12 @@ | |||
| class ChangeRepositoryReleasesCount < ActiveRecord::Migration[5.2] | |||
| def change | |||
| release_ids = VersionRelease.select(:id,:repository_id).pluck(:repository_id).uniq | |||
| release_ids.each do |i| | |||
| puts "#######____update_repository_releases_id____##############{i}" | |||
| p = Repository.includes(:version_releases).select(:id, :version_releases_count).find_by(id:i) | |||
| if p.present? | |||
| Repository.reset_counters(i, :version_releases) | |||
| end | |||
| end | |||
| end | |||
| end | |||