diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a15377efb..4dcf59d11 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -704,14 +704,20 @@ class ApplicationController < ActionController::Base Rails.application.config_for(:configuration)['platform_url'] || request.base_url end + def image_type?(str) + default_type = %w(png jpg gif tif psd svg bmp webp jpeg ico psd) + default_type.include?(str&.downcase) + end + def convert_image! @image = params[:image] @image = @image.nil? && params[:user].present? ? params[:user][:image] : @image return unless @image.present? max_size = EduSetting.get('upload_avatar_max_size') || 2 * 1024 * 1024 # 2M if @image.class == ActionDispatch::Http::UploadedFile - render_error('请上传文件') if @image.size.zero? - render_error('文件大小超过限制') if @image.size > max_size.to_i + return render_error('请上传文件') if @image.size.zero? + return render_error('文件大小超过限制') if @image.size > max_size.to_i + return render_error('头像格式不正确!') unless image_type?(File.extname(@image.original_filename.to_s)[1..-1]) else image = @image.to_s.strip return render_error('请上传正确的图片') if image.blank? diff --git a/app/controllers/organizations/teams_controller.rb b/app/controllers/organizations/teams_controller.rb index c599ac420..285f9960b 100644 --- a/app/controllers/organizations/teams_controller.rb +++ b/app/controllers/organizations/teams_controller.rb @@ -4,15 +4,24 @@ class Organizations::TeamsController < Organizations::BaseController before_action :check_user_can_edit_org, only: [:create, :update, :destroy] def index - #if @organization.is_owner?(current_user) || current_user.admin? - @teams = @organization.teams - #else - # @teams = @organization.teams.joins(:team_users).where(team_users: {user_id: current_user.id}) - #end - @is_admin = can_edit_org? - @teams = @teams.includes(:team_units, :team_users) - @teams = kaminari_paginate(@teams) + if params[:is_full].present? + if can_edit_org? + @teams = @organization.teams + else + @teams = [] + end + else + #if @organization.is_owner?(current_user) || current_user.admin? + @teams = @organization.teams + #else + # @teams = @organization.teams.joins(:team_users).where(team_users: {user_id: current_user.id}) + #end + @is_admin = can_edit_org? + @teams = @teams.includes(:team_units, :team_users) + + @teams = kaminari_paginate(@teams) + end end def search @@ -34,8 +43,12 @@ class Organizations::TeamsController < Organizations::BaseController def create ActiveRecord::Base.transaction do - Organizations::CreateTeamForm.new(team_params).validate! - @team = Organizations::Teams::CreateService.call(current_user, @organization, team_params) + if @organization.teams.count >= 50 + return render_forbidden("组织的团队数量已超过限制!") + else + Organizations::CreateTeamForm.new(team_params).validate! + @team = Organizations::Teams::CreateService.call(current_user, @organization, team_params) + end end rescue Exception => e uid_logger_error(e.message) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 77b818cad..571805324 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -82,8 +82,9 @@ class ProjectsController < ApplicationController def branches return @branches = [] unless @project.forge? - result = Gitea::Repository::Branches::ListService.call(@owner, @project.identifier) - @branches = result.is_a?(Hash) && result.key?(:status) ? [] : result + # result = Gitea::Repository::Branches::ListService.call(@owner, @project.identifier) + result = Gitea::Repository::Branches::ListNameService.call(@owner, @project.identifier) + @branches = result.is_a?(Hash) ? (result.key?(:status) ? [] : result["branch_name"]) : result end def branches_slice diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index c39ecbf5b..5ebc00b3f 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -252,7 +252,7 @@ class RepositoriesController < ApplicationController domain = Gitea.gitea_config[:domain] api_url = Gitea.gitea_config[:base_url] - url = "/repos/#{@owner.login}/#{@repository.identifier}/raw/#{URI.escape(params[:filepath])}?ref=#{CGI.escape(params[:ref])}" + url = "/repos/#{@owner.login}/#{@repository.identifier}/raw/#{CGI.escape(params[:filepath])}?ref=#{CGI.escape(params[:ref])}" file_path = [domain, api_url, url].join file_path = [file_path, "access_token=#{current_user&.gitea_token}"].join("&") diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index c20bb6ea1..5af634f18 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -6,7 +6,7 @@ class UsersController < ApplicationController before_action :check_user_exist, only: [:show, :homepage_info,:projects, :watch_users, :fan_users, :hovercard] before_action :require_login, only: %i[me list sync_user_info] before_action :connect_to_ci_db, only: [:get_user_info] - before_action :convert_image!, only: [:update] + before_action :convert_image!, only: [:update, :update_image] skip_before_action :check_sign, only: [:attachment_show] def connect_to_ci_db(options={}) @@ -82,10 +82,21 @@ class UsersController < ApplicationController Util.write_file(@image, avatar_path(@user)) if user_params[:image].present? @user.attributes = user_params.except(:image) unless @user.save - render_error(@user.errors.full_messages.join(", ")) + render_error(-1, @user.errors.full_messages.join(", ")) end end + def update_image + return render_not_found unless @user = User.find_by(login: params[:id]) || User.find_by_id(params[:id]) + return render_forbidden unless User.current.logged? && (current_user&.admin? || current_user.id == @user.id) + + Util.write_file(@image, avatar_path(@user)) + return render_ok({message: '头像修改成功'}) + rescue Exception => e + uid_logger_error(e.message) + render_error(-1, '头像修改失败!') + end + def me @user = current_user end diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index 6eca8f776..b357d40b3 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -15,7 +15,7 @@ module RepositoriesHelper end def image_type?(str) - default_type = %w(png jpg gif tif psd svg bmp webp jpeg) + default_type = %w(png jpg gif tif psd svg bmp webp jpeg ico psd) default_type.include?(str&.downcase) end diff --git a/app/helpers/tag_chosen_helper.rb b/app/helpers/tag_chosen_helper.rb index 4ded66e1a..dcf3c457c 100644 --- a/app/helpers/tag_chosen_helper.rb +++ b/app/helpers/tag_chosen_helper.rb @@ -29,8 +29,9 @@ module TagChosenHelper if project.educoder? return ['master'] else - branches = Gitea::Repository::Branches::ListService.call(project&.owner, project.identifier) - branches.collect{|i| i["name"] if i.is_a?(Hash)} + branches = Gitea::Repository::Branches::ListNameService.call(project&.owner, project.identifier) + return branches.collect{|i| i["name"] if i.is_a?(Hash)} if branches.is_a?(Array) + return branches["branch_name"] if branches.is_a?(Hash) end end diff --git a/app/jobs/migrate_remote_repository_job.rb b/app/jobs/migrate_remote_repository_job.rb index e54eaf2bc..488141910 100644 --- a/app/jobs/migrate_remote_repository_job.rb +++ b/app/jobs/migrate_remote_repository_job.rb @@ -6,7 +6,7 @@ class MigrateRemoteRepositoryJob < ApplicationJob return if repo.blank? puts "############ MigrateRemoteRepositoryJob starting ... ############" - + params.except!(:auth_password, :auth_username) if params[:auth_username].nil? gitea_repository = Gitea::Repository::MigrateService.new(token, params).call puts "#gitea_repository#{gitea_repository}" if gitea_repository[0]==201 diff --git a/app/libs/util.rb b/app/libs/util.rb index 1db9460f0..691ed7cab 100644 --- a/app/libs/util.rb +++ b/app/libs/util.rb @@ -29,6 +29,7 @@ module Util file.write(io) end end + true end def download_file(url, save_path) diff --git a/app/models/gitea/webhook_task.rb b/app/models/gitea/webhook_task.rb index d19a163aa..325352c69 100644 --- a/app/models/gitea/webhook_task.rb +++ b/app/models/gitea/webhook_task.rb @@ -1,7 +1,6 @@ class Gitea::WebhookTask < Gitea::Base serialize :payload_content, JSON serialize :request_content, JSON - serialize :response_content, JSON self.inheritance_column = nil @@ -10,4 +9,10 @@ class Gitea::WebhookTask < Gitea::Base belongs_to :webhook, class_name: "Gitea::Webhook", foreign_key: :hook_id enum type: {gogs: 1, slack: 2, gitea: 3, discord: 4, dingtalk: 5, telegram: 6, msteams: 7, feishu: 8, matrix: 9} + + def response_content_json + JSON.parse(response_content) + rescue + {} + end end \ No newline at end of file diff --git a/app/services/gitea/repository/branches/list_name_service.rb b/app/services/gitea/repository/branches/list_name_service.rb new file mode 100644 index 000000000..716ee464c --- /dev/null +++ b/app/services/gitea/repository/branches/list_name_service.rb @@ -0,0 +1,22 @@ +class Gitea::Repository::Branches::ListNameService < Gitea::ClientService + attr_reader :user, :repo + + def initialize(user, repo) + @user = user + @repo = repo + end + + def call + response = get(url, params) + render_200_response(response) + end + + private + def params + Hash.new.merge(token: user.gitea_token) + end + + def url + "/repos/#{user.login}/#{repo}/branch_name_set".freeze + end +end diff --git a/app/views/organizations/teams/_simple_detail.json.jbuilder b/app/views/organizations/teams/_simple_detail.json.jbuilder new file mode 100644 index 000000000..5e9a6246d --- /dev/null +++ b/app/views/organizations/teams/_simple_detail.json.jbuilder @@ -0,0 +1,3 @@ +json.id team.id +json.name team.name +json.nickname team.nickname.blank? ? team.name : team.nickname \ No newline at end of file diff --git a/app/views/organizations/teams/index.json.jbuilder b/app/views/organizations/teams/index.json.jbuilder index ad3479ec4..189047fc4 100644 --- a/app/views/organizations/teams/index.json.jbuilder +++ b/app/views/organizations/teams/index.json.jbuilder @@ -1,4 +1,8 @@ -json.total_count @teams.total_count +json.total_count params[:is_full].present? ? @teams.count : @teams.total_count json.teams @teams do |team| - json.partial! "detail", team: team, organization: @organization + if params[:is_full].present? + json.partial! "simple_detail", team: team, organization: @organization + else + json.partial! "detail", team: team, organization: @organization + end end diff --git a/app/views/projects/branches.json.jbuilder b/app/views/projects/branches.json.jbuilder index c7c2025b3..3bfc9f46b 100644 --- a/app/views/projects/branches.json.jbuilder +++ b/app/views/projects/branches.json.jbuilder @@ -1,21 +1,22 @@ json.array! @branches do |branch| - json.name branch['name'] - json.user_can_push branch['user_can_push'] - json.user_can_merge branch['user_can_merge'] - json.protected branch['protected'] + branch_name = branch.is_a?(Hash) ? branch['name'] : branch + json.name branch_name + # json.user_can_push branch['user_can_push'] + # json.user_can_merge branch['user_can_merge'] + # json.protected branch['protected'] json.http_url render_http_url(@project) - json.zip_url render_zip_url(@owner, @repository, branch['name']) - json.tar_url render_tar_url(@owner, @repository, branch['name']) - json.last_commit do - json.sha branch['commit']['id'] - json.message branch['commit']['message'] - json.timestamp render_unix_time(branch['commit']['timestamp']) - json.time_from_now time_from_now(branch['commit']['timestamp']) - json.author do - json.partial! 'repositories/commit_author', user: render_cache_commit_author(branch['commit']['author']), name: branch['commit']['author']['name'] - end - json.committer do - json.partial! 'repositories/commit_author', user: render_cache_commit_author(branch['commit']['committer']), name: branch['commit']['committer']['name'] - end - end + json.zip_url render_zip_url(@owner, @repository, branch_name) + json.tar_url render_tar_url(@owner, @repository, branch_name) + # json.last_commit do + # json.sha branch['commit']['id'] + # json.message branch['commit']['message'] + # json.timestamp render_unix_time(branch['commit']['timestamp']) + # json.time_from_now time_from_now(branch['commit']['timestamp']) + # json.author do + # json.partial! 'repositories/commit_author', user: render_cache_commit_author(branch['commit']['author']), name: branch['commit']['author']['name'] + # end + # json.committer do + # json.partial! 'repositories/commit_author', user: render_cache_commit_author(branch['commit']['committer']), name: branch['commit']['committer']['name'] + # end + # end end diff --git a/app/views/projects/webhooks/_detail.json.jbuilder b/app/views/projects/webhooks/_detail.json.jbuilder index 2497e5c64..41a91f4ad 100644 --- a/app/views/projects/webhooks/_detail.json.jbuilder +++ b/app/views/projects/webhooks/_detail.json.jbuilder @@ -1,4 +1,4 @@ json.(webhook, :id, :url, :http_method, :is_active) -json.type webhook.hook_task_type +json.type webhook.type json.last_status webhook.last_status json.create_time Time.at(webhook.created_unix).strftime("%Y-%m-%d %H:%M:%S") \ No newline at end of file diff --git a/app/views/projects/webhooks/edit.json.jbuilder b/app/views/projects/webhooks/edit.json.jbuilder index 2ee6d24e8..c54d10306 100644 --- a/app/views/projects/webhooks/edit.json.jbuilder +++ b/app/views/projects/webhooks/edit.json.jbuilder @@ -1,6 +1,6 @@ json.id @webhook.id json.(@webhook, :id, :http_method, :content_type, :url, :secret, :last_status, :is_active) -json.type @webhook.hook_task_type +json.type @webhook.type json.create_time Time.at(@webhook.created_unix).strftime("%Y-%m-%d %H:%M:%S") event = @webhook.events json.branch_filter event["branch_filter"] diff --git a/app/views/projects/webhooks/tasks.json.jbuilder b/app/views/projects/webhooks/tasks.json.jbuilder index b8aef99f5..82b2eae4a 100644 --- a/app/views/projects/webhooks/tasks.json.jbuilder +++ b/app/views/projects/webhooks/tasks.json.jbuilder @@ -1,5 +1,6 @@ json.total_count @tasks.total_count json.tasks @tasks.each do |task| - json.(task, :id, :type, :uuid, :is_succeed, :is_delivered, :payload_content, :request_content, :response_content) + json.(task, :id, :event_type, :type, :uuid, :is_succeed, :is_delivered, :payload_content, :request_content) + json.response_content task.response_content_json json.delivered_time Time.at(task.delivered*10**-9).strftime("%Y-%m-%d %H:%M:%S") end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 8f295ed00..36cd53919 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -225,6 +225,7 @@ Rails.application.routes.draw do get :watch_users get :fan_users get :hovercard + put :update_image end collection do post :following