add version_releases attachments

This commit is contained in:
sylor_huang@126.com 2020-05-20 14:43:14 +08:00
parent ef1023d0f5
commit f197f41839
6 changed files with 50 additions and 23 deletions

View File

@ -56,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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -4,7 +4,7 @@ json.user_permission @user_permission
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"])
version = @forge_releases.find_by(version_gid: re["id"])
if @user_permission && re["draft"]
json.version_id version.try(:id)
json.id re["id"]
@ -38,6 +38,10 @@ json.releases do
json.image_url user.present? ? url_to_avatar(user) : ""
end
end
json.attachments do
json.array! version.attachments do |attachment|
json.partial! "attachments/attachment_simple", locals: {attachment: attachment}
end
end
end
end