From 43e06174299dbc8942ff1e5d1c122a4ddb72282b Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 30 Dec 2021 09:47:12 +0800 Subject: [PATCH 01/10] fix: webhook field changed --- app/views/projects/webhooks/_detail.json.jbuilder | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From cfd94cb3b7cbfc0f58417dab37071ac1c04b6121 Mon Sep 17 00:00:00 2001 From: yystopf Date: Mon, 3 Jan 2022 20:33:11 +0800 Subject: [PATCH 02/10] fix: migrate repo username nil is excepted --- app/jobs/migrate_remote_repository_job.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From f339df699ea595b2a4b64e20424c91a2b3bd6b0f Mon Sep 17 00:00:00 2001 From: yystopf Date: Mon, 3 Jan 2022 22:32:09 +0800 Subject: [PATCH 03/10] add: user upload image --- app/controllers/users_controller.rb | 17 +++++++++++++++-- app/helpers/repositories_helper.rb | 2 +- app/libs/util.rb | 1 + config/routes.rb | 1 + 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 90e050707..8cb89febd 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,12 +1,13 @@ class UsersController < ApplicationController include ApplicationHelper include Ci::DbConnectable + include RepositoriesHelper before_action :load_user, only: [:show, :homepage_info, :sync_token, :sync_gitea_pwd, :projects, :watch_users, :fan_users, :hovercard] 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,7 +83,19 @@ 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) + + return render_error(-1, '头像格式不正确!') unless params[:image].present? && image_type?(File.extname(params[:image].original_filename.to_s)[1..-1]) + if Util.write_file(@image, avatar_path(@user)) && params[:image].present? + render_ok({message: '头像修改成功'}) + else + render_error(-1, '头像修改失败!') end 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/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/config/routes.rb b/config/routes.rb index d4e1ba1c5..be87e7e68 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -213,6 +213,7 @@ Rails.application.routes.draw do get :watch_users get :fan_users get :hovercard + put :update_image end collection do post :following From 499734ebf9fbfefc197bc6dde4439bc9b27f585c Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 4 Jan 2022 22:44:39 +0800 Subject: [PATCH 04/10] fix: upload user image allow base64 --- app/controllers/application_controller.rb | 10 ++++++++-- app/controllers/users_controller.rb | 14 ++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d7707391f..4e59a7e07 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -709,14 +709,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/users_controller.rb b/app/controllers/users_controller.rb index 8cb89febd..00ea926fd 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,7 +1,6 @@ class UsersController < ApplicationController include ApplicationHelper include Ci::DbConnectable - include RepositoriesHelper before_action :load_user, only: [:show, :homepage_info, :sync_token, :sync_gitea_pwd, :projects, :watch_users, :fan_users, :hovercard] before_action :check_user_exist, only: [:show, :homepage_info,:projects, :watch_users, :fan_users, :hovercard] @@ -90,13 +89,12 @@ class UsersController < ApplicationController 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) - - return render_error(-1, '头像格式不正确!') unless params[:image].present? && image_type?(File.extname(params[:image].original_filename.to_s)[1..-1]) - if Util.write_file(@image, avatar_path(@user)) && params[:image].present? - render_ok({message: '头像修改成功'}) - else - render_error(-1, '头像修改失败!') - end + + 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 From 7fde1e0e12f346e3c9a20379bf99e896008874d0 Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 6 Jan 2022 11:10:49 +0800 Subject: [PATCH 05/10] fix: webhook field changed --- app/views/projects/webhooks/edit.json.jbuilder | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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"] From 84f12adce8512ea37c75aabc25a1f3cd3ce6e5db Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 6 Jan 2022 14:29:31 +0800 Subject: [PATCH 06/10] fix: webhook tasks list error delay --- app/models/gitea/webhook_task.rb | 7 ++++++- app/views/projects/webhooks/tasks.json.jbuilder | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) 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/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 From a7665df0ea1728e7e359ad95da61de2532205469 Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 6 Jan 2022 15:04:59 +0800 Subject: [PATCH 07/10] fix: download file url use cgi escape --- app/controllers/repositories_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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("&") From 47d1f727ce15e591893aad2e58dd82ffe6d26aed Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 6 Jan 2022 17:38:44 +0800 Subject: [PATCH 08/10] fix: create team count limit --- .../organizations/teams_controller.rb | 33 +++++++++++++------ .../teams/_simple_detail.json.jbuilder | 3 ++ .../organizations/teams/index.json.jbuilder | 8 +++-- 3 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 app/views/organizations/teams/_simple_detail.json.jbuilder 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/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 From 7a1e6cfbad27e54bf38435f47662c4626780872f Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 7 Jan 2022 15:19:10 +0800 Subject: [PATCH 09/10] fix: issue edit and new branches only load name api --- app/helpers/tag_chosen_helper.rb | 5 +++-- .../repository/branches/list_name_service.rb | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 app/services/gitea/repository/branches/list_name_service.rb 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/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 From 2a09ced1ceec6b8bf067ed8844e5edccee5fb825 Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 7 Jan 2022 15:41:50 +0800 Subject: [PATCH 10/10] fix: branches use branch name api --- app/controllers/projects_controller.rb | 5 +-- app/views/projects/branches.json.jbuilder | 37 ++++++++++++----------- 2 files changed, 22 insertions(+), 20 deletions(-) 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/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