From f0c1e9e31940d2efed2d201e900c52f9e6dc63a1 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Mon, 13 Feb 2023 11:27:44 +0800 Subject: [PATCH 01/19] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E5=90=8C=E6=AD=A5es=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/queries/projects/list_query.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/queries/projects/list_query.rb b/app/queries/projects/list_query.rb index 8a8fa2f36..6b0843540 100644 --- a/app/queries/projects/list_query.rb +++ b/app/queries/projects/list_query.rb @@ -51,10 +51,9 @@ class Projects::ListQuery < ApplicationQuery # items = items.where(id: @ids).by_name_or_identifier(params[:search]) items = items.where(id: @ids) else - items = items.by_name_or_identifier(params[:search]) + items = items.by_name_or_identifier(params[:search]).or(main_collection.where(user_id: Owner.like(params[:search]).pluck(:id))) end - items.or(main_collection.where(user_id: Owner.like(params[:search]).pluck(:id))) - + items end def by_project_type(items) From 774210206ef52a7f5b0d2e209389ada69904a928 Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 15 Feb 2023 11:30:33 +0800 Subject: [PATCH 02/19] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BB=A5.=E5=BC=80=E5=A4=B4=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81=E9=A2=84=E8=A7=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/repositories/_simple_entry.json.jbuilder | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/repositories/_simple_entry.json.jbuilder b/app/views/repositories/_simple_entry.json.jbuilder index 0b2a60b57..bbbd09d54 100644 --- a/app/views/repositories/_simple_entry.json.jbuilder +++ b/app/views/repositories/_simple_entry.json.jbuilder @@ -1,6 +1,6 @@ if @project.forge? file_name = entry['name'] - file_type = File.extname(file_name.to_s)[1..-1] + file_type = file_name.starts_with?('.') ? file_name[1..-1] : File.extname(file_name.to_s)[1..-1] direct_download = file_name.to_s.downcase != "Makefile".downcase && download_type(file_type) image_type = image_type?(file_type) json.name file_name From 1cd43de75bf2785e51ede69282ea0de78b35415b Mon Sep 17 00:00:00 2001 From: xxq250 Date: Wed, 15 Feb 2023 18:37:36 +0800 Subject: [PATCH 03/19] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Bot=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/application_controller.rb | 27 +++++++---- app/controllers/installations_controller.rb | 46 +++++++++++++++++++ app/models/bot.rb | 51 +++++++++++++++++++++ app/models/bot_install.rb | 20 ++++++++ app/models/user.rb | 2 +- app/views/installations/index.json.jbuilder | 7 +++ config/initializers/doorkeeper.rb | 2 +- config/routes.rb | 11 +++++ db/migrate/20230214000021_add_bot_info.rb | 9 ++++ test/jwt_test.rb | 42 +++++++++++++++++ 10 files changed, 206 insertions(+), 11 deletions(-) create mode 100644 app/controllers/installations_controller.rb create mode 100644 app/models/bot.rb create mode 100644 app/models/bot_install.rb create mode 100644 app/views/installations/index.json.jbuilder create mode 100644 db/migrate/20230214000021_add_bot_info.rb create mode 100644 test/jwt_test.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7490cb27f..8129df8f1 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,6 @@ require 'oauth2' +# require 'openssl' +# require 'jwt' class ApplicationController < ActionController::Base include CodeExample @@ -295,17 +297,24 @@ class ApplicationController < ActionController::Base # Find the current user #Rails.logger.info("current_laboratory is #{current_laboratory} domain is #{request.subdomain}") if request.headers["Authorization"].present? && request.headers["Authorization"].start_with?('Bearer') - tip_exception(401, "请登录后再操作!") unless valid_doorkeeper_token? - if @doorkeeper_token.present? - # client方法对接,需要一直带着用户标识uid - if @doorkeeper_token.resource_owner_id.blank? - tip_exception(-1, "缺少用户标识!") if params[:uid].nil? - User.current = User.find(params[:uid]) - else - User.current = User.find_by(id: @doorkeeper_token.resource_owner_id) + if !valid_doorkeeper_token? + header = request.authorization + pattern = /^Bearer /i + token = header.gsub(pattern, "") + User.current, message = Bot.decode_jwt_token(token) + tip_exception(401, message) if message.present? + else + if @doorkeeper_token.present? + # client方法对接,需要一直带着用户标识uid + if @doorkeeper_token.resource_owner_id.blank? + tip_exception(-1, "缺少用户标识!") if params[:uid].nil? + User.current = User.find(params[:uid]) + else + User.current = User.find_by(id: @doorkeeper_token.resource_owner_id) + end end end - else + else User.current = find_current_user uid_logger("user_setup: " + (User.current.logged? ? "#{User.current.try(:login)} (id=#{User.current.try(:id)})" : "anonymous")) diff --git a/app/controllers/installations_controller.rb b/app/controllers/installations_controller.rb new file mode 100644 index 000000000..c8928b51d --- /dev/null +++ b/app/controllers/installations_controller.rb @@ -0,0 +1,46 @@ +class InstallationsController < ApplicationController + include RegisterHelper + before_action :require_login + + def index + @install_bots = BotInstall.where(:installer_id => current_user.id) + end + + def register + @bot = Bot.find params[:id] + @bot.client_id = Doorkeeper::OAuth::Helpers::UniqueToken.generate if params[:client_id].blank? + @bot.client_secret = Doorkeeper::OAuth::Helpers::UniqueToken.generate if params[:client_secret].blank? + @bot.private_key = OpenSSL::PKey::RSA::generate(2048).to_s + @bot.owner_id = current_user.id + ActiveRecord::Base.transaction do + # 注册bot对应oauth应用 + Doorkeeper::Application.create!(name: @bot.name, uid: @bot.client_id, secret: @bot.client_secret, redirect_uri: "https://gitlink.org.cn") + # 注册bot对应用户 + result = autologin_register(User.generate_user_login('b'), nil, "b58333123413", 'bot', nickname: @bot.name) + tip_exception(-1, result[:message]) if result[:message].present? + @bot.uid = result[:user][:id] + @bot.save + render_ok + rescue Exception => e + tip_exception(-1, e.message) + end + end + + def access_tokens + @install_bot = BotInstall.find params[:id] + @bot = @install_bot.bot + @application = Doorkeeper::Application.find_by(uid: @bot.client_id, secret: @bot.client_secret) + if @application.present? + # 给bot生成token,因为bot是机器人操作 + @access_token = Doorkeeper::AccessToken.create!({ :application_id => @application.id, + :resource_owner_id => @bot.uid, + :scopes => "public write", + :expires_in => "604800", + :use_refresh_token => true + }) + end + render_ok(token: @access_token.token) + end + +end + diff --git a/app/models/bot.rb b/app/models/bot.rb new file mode 100644 index 000000000..2098ac1e2 --- /dev/null +++ b/app/models/bot.rb @@ -0,0 +1,51 @@ +# == Schema Information +# +# Table name: bot +# +# id :integer not null, primary key +# bot_name :string(255) +# bot_des :text(4294967295) +# webhook :string(255) +# is_public :integer +# logo :string(255) +# state :integer +# client_id :string(255) +# client_secret :string(255) +# web_url :string(255) +# category :string(255) +# install_num :integer default("0") +# update_time :datetime not null +# create_time :datetime not null +# private_key :text(65535) +# uid :integer +# owner_id :integer +# +# Indexes +# +# name (bot_name) UNIQUE +# + +class Bot < ApplicationRecord + self.table_name = "bot" + + has_many :install_bots + + def name + self.bot_name + end + + def self.decode_jwt_token(token) + decoded_token = JWT.decode token, nil, false + return [nil, "Token已过期"] if Time.now.to_i - 60 - decoded_token[0]["exp"].to_i > 0 + bot = Bot.find_by(id: decoded_token[0]["iss"]) + return [nil, "Token不存在"] if bot.blank? + rsa_private = OpenSSL::PKey::RSA.new(bot.private_key) + rsa_public = rsa_private.public_key + JWT.decode token, rsa_public, true, { algorithm: 'RS256' } + [User.find_by(id: bot.owner_id), ""] + rescue JWT::DecodeError + Rails.logger.error "jwt token decode error:#{token}" + [nil, "无效Token"] + end + +end diff --git a/app/models/bot_install.rb b/app/models/bot_install.rb new file mode 100644 index 000000000..d5283c131 --- /dev/null +++ b/app/models/bot_install.rb @@ -0,0 +1,20 @@ +# == Schema Information +# +# Table name: install_bot +# +# id :integer not null, primary key +# bot_id :integer not null +# installer_id :integer not null +# store_id :integer not null +# state :integer not null +# create_time :datetime not null +# update_time :datetime not null +# + +# frozen_string_literal: true + +class BotInstall < ApplicationRecord + self.table_name = "install_bot" + belongs_to :bot + +end diff --git a/app/models/user.rb b/app/models/user.rb index df35f3a2f..b43e289ed 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -114,7 +114,7 @@ class User < Owner # trustie: 来自Trustie平台 # forge: 平台本身注册的用户 # military: 军科的用户 - enumerize :platform, in: [:forge, :educoder, :trustie, :military, :github, :gitee, :qq, :wechat], default: :forge, scope: :shallow + enumerize :platform, in: [:forge, :educoder, :trustie, :military, :github, :gitee, :qq, :wechat, :bot], default: :forge, scope: :shallow belongs_to :laboratory, optional: true has_one :user_extension, dependent: :destroy diff --git a/app/views/installations/index.json.jbuilder b/app/views/installations/index.json.jbuilder new file mode 100644 index 000000000..b2f674f20 --- /dev/null +++ b/app/views/installations/index.json.jbuilder @@ -0,0 +1,7 @@ +json.status 0 +json.message "success" +json.data do + json.array! @install_bots do |install_bot| + json.extract! install_bot.bot, :id, :name + end +end \ No newline at end of file diff --git a/config/initializers/doorkeeper.rb b/config/initializers/doorkeeper.rb index 2a2355c6d..0cc9a02a4 100644 --- a/config/initializers/doorkeeper.rb +++ b/config/initializers/doorkeeper.rb @@ -18,7 +18,7 @@ Doorkeeper.configure do User.authenticate!(params[:username], params[:password]) end - access_token_generator '::Doorkeeper::JWT' + # access_token_generator '::Doorkeeper::JWT' admin_authenticator do user = current_user diff --git a/config/routes.rb b/config/routes.rb index dc538d93f..58c625841 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1047,6 +1047,17 @@ Rails.application.routes.draw do resources :commit_logs, :only => [:create] + scope '/app' do + post 'register', to: 'installations#register' + resources :installations do + get :repositories, on: :collection + member do + post :access_tokens + put :suspended + end + end + end + root 'main#index' diff --git a/db/migrate/20230214000021_add_bot_info.rb b/db/migrate/20230214000021_add_bot_info.rb new file mode 100644 index 000000000..5e8d11b52 --- /dev/null +++ b/db/migrate/20230214000021_add_bot_info.rb @@ -0,0 +1,9 @@ +class AddBotInfo < ActiveRecord::Migration[5.2] + def change + if table_exists? :bot + add_column :bot, :private_key, :text, comment: "私钥" + add_column :bot, :owner_id, :integer, comment: "所有者ID" + add_column :bot, :uid, :integer, comment: "Bot机器人用户ID" + end + end +end diff --git a/test/jwt_test.rb b/test/jwt_test.rb new file mode 100644 index 000000000..8d0faaa3f --- /dev/null +++ b/test/jwt_test.rb @@ -0,0 +1,42 @@ +require 'openssl' +require 'jwt' # https://rubygems.org/gems/jwt + +# Private key contents +private_pem = File.read("/Users/xxq/Documents/gitlink-webhook.2022-06-09.private-key.pem") + +private_key22="-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEApOebWmRV/ooNq5Ks04YnDU7pEezGShGvaiF0cIvn9jvmYHu0\nFialojvJV3VpB6xE6QBPXZ0Pi1lokZ9dMx8F5UWNx9WA7wf7xK3hAJLNml+GeewF\nou8vk/Ry7n6diLxETNVd7YzPvztn5qaMp/DXa+65i11H8a/XXqR7kCVnCevVlufh\nNr/Dp6dW31W8TInnDQasJFMZ8GY7f+tCwLXNc0M8p+TeDp9xmXHOrEB+S/mgbUOF\nXgRr6icbMmlT9bsAxYHrrDkcVxJhs0hq5vD3BaoK06gcZEnN7/HVNzgVSOYNsVNh\n9006cMgDSOwc9F8aulP7cr8k74INq1xswoGs9wIDAQABAoIBAHayc2NkF3YJXv+h\nqx7yUEfHBgKuAKiuBCqLfCnKuqPFx/So9h5/oPeeuzVlwL0SJePlIjuK4vZ128v9\n/vLeILtADmbJ6m2jvHh8hBmKkc3Ndplp50C5k/CWoufCYZhbk3oOlvZ3Rc4rb4VZ\nWqNDu3voMMv8z91KqeZo1LwUAA/l9mU++zLkRA6qOuWGBJFsM8YpshzxL5lzRUb3\n7y+YJDyUZztfzKwr6pqm1n9B2e6e+znCw1vMZXp2TbUrpvrrXSxlgdNuK68SkZX6\nTdZUD8y0viwaioRVf3vR+e/Bf7yZannGdvcmVGs0A7dq9QgHkakqNHiRkQgwviSq\nbjBo7dECgYEAzekeP5j/dAPkv9X4qnmZ4du/+ZgrQrJckDD/JuNIBmQT9m286l4P\nmb2TBcWkswVOZaS5Qy2bN/69rwIbcdvbaROGBCabn3ATK4fSzmUk31M2rRKYZqaU\nMi0W2g2YtSRg+bV6S7aFXa98j5+JlqJeDZQoRuvL68ooq5WzFWmfYnkCgYEAzQTi\n4USqz2z66BfU+v2rchzK8URxnv7EW/CG3XFRsG+1UXCyEIct2L7rzvC7r7+jjS4s\ngmV3Civ1sckGMwikLzxFtUZ1LUBakZp/mmipIzxcHOeBsRdHei8BFvMNqveg1JpO\ncY/Fp+wEkSNLhfkb/IXRw0iwFalBRnyo4BJbLu8CgYBrQ7E6OB169jxHotNzGv2K\npssO3rJKgFev1ZZVT7jJe4Dasrfi7zT5RcQ9EYSGrZD1aiYIVM2zEcUGUfayDXHy\n/vSlXOdc2ylhV9P9KLtYiyTEbBdwAf7ZVJu+465VTqol6t/WaTJ4Z15gAx/NlK+i\nKzgAGf2Uyy78k3NDCE67IQKBgFwM0pUUEKEbLDhi4uRiWsTcep4C/gTGHIGvJ85r\nH6NZNI7BS6GyH/qOFjAO1CYfpB4yWhed2Om/PQw61sa5HYZ7yEyQuvG7UC7JsHsy\nfKZuZmkv5IIPkq8gRZv5OuzFS/fI5GmGhNdVV+OWdkVLyK4Do1/L1guTt9QfCm+4\nrioPAoGBALGr8aUAbz/A611M/bLnk04UYfV+M34/hCf6/rKiBHdQoIHOriSC9Nv7\nyhE5axTdmIWMxfbyb3vHJ5MizZkD/Qj0VDuMkyS2+3TepI6tySQE3YQeWnCMJI9i\nuoCZ31GBui4+W5udbx8NOVsJfXUQn/OAoOn6WuMNPdgB45KXcktj\n-----END RSA PRIVATE KEY-----\n" +private_key33= "-----BEGIN RSA PRIVATE KEY-----\nMIIEogIBAAKCAQEAq/wUH8N+5fzj3hArKY7ChC591R/uKyeNM/BbsR2OGxO5F1CE\nozy6thtPult96Gm9oK3sMpLdYCze2YgozgteFO1Ft0o1GEJ1A4SinOKzeixLpFy0\n5N9t+iz7Xa7jC/1E3uy/s2WvSYCS9NnK2Uj3DQOH8BUWfkvyTtt91a2pplbPCV3T\nw1PykAcDWIFXVJJCMtYd2x+DukSWKHRsYbBCMtVZhEVuKmTE3FBTDVu9sN3b7uLL\n5RzHUg13QZZr9OvMNR3nUZl6yDxw+wD4anDrtpL9C+tFjhMyqsyYpYWwcJm65YiD\ny7Ps24IdcLB4iOxJE91fu+MnicvyBrtEjoBP/wIDAQABAoIBAAcKc9x1CW3q8300\n1j+GS6pTqO0fuIVlwh8dOPPATQAIx6wPrM5t/wrThWkQs8/e/Fdmp2POpWd5jsoD\nDACbcIeUyyTc0d2jYtz5AhtAIK7gv1wEO5efGgaC7ut/7GWiQb6KnLKAeDOfIuUJ\nQYexuAN9YIRQqLIU89+MltM3n9liZTMuPWRFJcitaDytXa10TCe5RUqHGZf49pi7\njCgk0x7jDYqbIzsqOu741P8My/gkAjKPkRnjaj3o6MrwHzIlc4t/6mKbaPnywywk\n6roYMqmytgueA9wxFcj74ekBQAaXsu4xRkbZXxjcBtIvTId5IHHK5Z/r3fgE9K3J\nOuzzJ1kCgYEA3uu/pUjJbKegOsgSdu3cO/NvRV2YsRD4NUgtiMCEakE4VQXRK5pf\nV8xqQeH/rLjZf5aP5xe8n25krh/c+m0ezOMyu5MmhoxaWCPWIezsaKJXOcvsOxIu\n2sJ9GRMXabyuDuSdL7ZGYMpLhRclXYLyPCz7wzN445IluTHuD3lJ2qUCgYEAxYFh\ncVD73yNZn9BN1DSGWpfPtLqOKIdG+xi/ypCSGpJG0QCJRFi7R1qJOxFtJNI8DRiD\nZapPEGLVd/KY5NzBGZBfNQt4DQH9qR4l43c6NNkisWA9rvXvCDqXKmBq7wfpnkYr\n4Ul2hXYmsPJjP8e0BfG54PaSu3BDBMJMtcgDktMCgYBvzyDdnwdgVyc3tHgGbMFk\n1HHAAfT/ArrrxpsIFz+TJ8lAY92JGDGwENhO2TLrCAAXTYY5657w/GbFKzgj5y1m\nqKIekOzm2WjLApZ5h6L/zEUhuRVwf2s+0AP82qWIpFlNIP9yGeNs0qpUQ8q6/13O\nLuXL/3on8nq3S8LSwgv3/QKBgDfM+g7d5ouAnU29uH9/54Wo5pIVMxzYO4Gt2GIO\nvnirYz6hfCbHOwJJ3gPGRKPmkfjROC59E6F5iv48mF3w0M28MGn4N47VRSmGzwWZ\nJeTQhDDBFCxeZ45Xn2Xln9Cw15xUDwmzi7zhSMUtdkUK0x3q0a1xfLtgWE775lhl\njjzpAoGAdUXFW1elfjXpfIYZf7vUV7MPquKL7qAcopd96XBszhOn7g+ibzem+wgt\n1UTSeOBESYANHeJk2MuWPSRXk/FlQETVIcPAEp0kxbwQE+7YEdrMVeDcIe5lPwGD\n+WuS3kg0MPgUrXZXn74gcwWmSIOyHfqXULqOxWE25uU2icdV+2w=\n-----END RSA PRIVATE KEY-----\n" +private_key = OpenSSL::PKey::RSA.new(private_key33) +# puts private_key.to_json + +# Generate the JWT +payload = { + # issued at time, 60 seconds in the past to allow for clock drift + iat: Time.now.to_i - 60, + # JWT expiration time (10 minute maximum) + exp: Time.now.to_i + (10 * 60), + # GitHub App's identifier + iss: "782" +} + +jwt = JWT.encode(payload, private_key, "RS256") +puts jwt +# puts OpenSSL::PKey::RSA.new(private_key33).public_key.to_s +# +rsa_private = OpenSSL::PKey::RSA.new(private_key33) +rsa_public = rsa_private.public_key + +# decoded_token = JWT.decode jwt, nil, false +begin + decoded_token = JWT.decode jwt, rsa_private, true, { algorithm: 'RS256' } +rescue JWT::DecodeError + puts "jwt is not mmmmmm" +end + +# +puts decoded_token[0] +puts decoded_token[0]["iss"] + +# serialized_private_key = OpenSSL::PKey::RSA::generate(2048).to_s + + From 6fe16c7c82476059688c445b940e8bfdd1adc9e2 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 09:19:37 +0800 Subject: [PATCH 04/19] =?UTF-8?q?=E8=B0=83=E6=95=B4Bot=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/installations_controller.rb | 2 +- config/routes.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/installations_controller.rb b/app/controllers/installations_controller.rb index c8928b51d..0ebc9a3eb 100644 --- a/app/controllers/installations_controller.rb +++ b/app/controllers/installations_controller.rb @@ -6,7 +6,7 @@ class InstallationsController < ApplicationController @install_bots = BotInstall.where(:installer_id => current_user.id) end - def register + def auth_active @bot = Bot.find params[:id] @bot.client_id = Doorkeeper::OAuth::Helpers::UniqueToken.generate if params[:client_id].blank? @bot.client_secret = Doorkeeper::OAuth::Helpers::UniqueToken.generate if params[:client_secret].blank? diff --git a/config/routes.rb b/config/routes.rb index 58c625841..d6e73aebb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1048,7 +1048,7 @@ Rails.application.routes.draw do resources :commit_logs, :only => [:create] scope '/app' do - post 'register', to: 'installations#register' + post 'auth_active', to: 'installations#auth_active' resources :installations do get :repositories, on: :collection member do From c0fcf3445a1e972f98fdcc18ac81851c5797444c Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 09:40:08 +0800 Subject: [PATCH 05/19] =?UTF-8?q?=E8=B0=83=E6=95=B4Bot=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/installations_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/installations_controller.rb b/app/controllers/installations_controller.rb index 0ebc9a3eb..5d6a26836 100644 --- a/app/controllers/installations_controller.rb +++ b/app/controllers/installations_controller.rb @@ -21,6 +21,7 @@ class InstallationsController < ApplicationController @bot.uid = result[:user][:id] @bot.save render_ok + end rescue Exception => e tip_exception(-1, e.message) end From 1d9df770b516d9804476c441b94d473bc17ac413 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 09:41:39 +0800 Subject: [PATCH 06/19] =?UTF-8?q?=E8=B0=83=E6=95=B4Bot=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/installations_controller.rb | 31 +++++++++++---------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/app/controllers/installations_controller.rb b/app/controllers/installations_controller.rb index 5d6a26836..40efa3c90 100644 --- a/app/controllers/installations_controller.rb +++ b/app/controllers/installations_controller.rb @@ -7,21 +7,22 @@ class InstallationsController < ApplicationController end def auth_active - @bot = Bot.find params[:id] - @bot.client_id = Doorkeeper::OAuth::Helpers::UniqueToken.generate if params[:client_id].blank? - @bot.client_secret = Doorkeeper::OAuth::Helpers::UniqueToken.generate if params[:client_secret].blank? - @bot.private_key = OpenSSL::PKey::RSA::generate(2048).to_s - @bot.owner_id = current_user.id - ActiveRecord::Base.transaction do - # 注册bot对应oauth应用 - Doorkeeper::Application.create!(name: @bot.name, uid: @bot.client_id, secret: @bot.client_secret, redirect_uri: "https://gitlink.org.cn") - # 注册bot对应用户 - result = autologin_register(User.generate_user_login('b'), nil, "b58333123413", 'bot', nickname: @bot.name) - tip_exception(-1, result[:message]) if result[:message].present? - @bot.uid = result[:user][:id] - @bot.save - render_ok - end + begin + @bot = Bot.find params[:id] + @bot.client_id = Doorkeeper::OAuth::Helpers::UniqueToken.generate if params[:client_id].blank? + @bot.client_secret = Doorkeeper::OAuth::Helpers::UniqueToken.generate if params[:client_secret].blank? + @bot.private_key = OpenSSL::PKey::RSA::generate(2048).to_s + @bot.owner_id = current_user.id + ActiveRecord::Base.transaction do + # 注册bot对应oauth应用 + Doorkeeper::Application.create!(name: @bot.name, uid: @bot.client_id, secret: @bot.client_secret, redirect_uri: "https://gitlink.org.cn") + # 注册bot对应用户 + result = autologin_register(User.generate_user_login('b'), nil, "b58333123413", 'bot', nickname: @bot.name) + tip_exception(-1, result[:message]) if result[:message].present? + @bot.uid = result[:user][:id] + @bot.save + render_ok + end rescue Exception => e tip_exception(-1, e.message) end From 3c51d69555d6a32a1484f4007e5c27043c708e62 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 09:57:25 +0800 Subject: [PATCH 07/19] =?UTF-8?q?=E8=B0=83=E6=95=B4Bot=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/installations_controller.rb | 29 +++++++++++++++++---- config/routes.rb | 4 ++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app/controllers/installations_controller.rb b/app/controllers/installations_controller.rb index 40efa3c90..6e66431be 100644 --- a/app/controllers/installations_controller.rb +++ b/app/controllers/installations_controller.rb @@ -6,6 +6,25 @@ class InstallationsController < ApplicationController @install_bots = BotInstall.where(:installer_id => current_user.id) end + def update_secret + ActiveRecord::Base.transaction do + bot = Bot.find params[:id] + application = Doorkeeper::Application.find_by(uid: bot.client_id, secret: bot.client_secret) + bot.client_secret = Doorkeeper::OAuth::Helpers::UniqueToken.generate + bot.save! + application.secret = bot.client_secret + application.save! + render_ok + end + end + + def update_private_key + bot = Bot.find params[:id] + bot.private_key = OpenSSL::PKey::RSA::generate(2048).to_s + bot.save! + render_ok + end + def auth_active begin @bot = Bot.find params[:id] @@ -35,11 +54,11 @@ class InstallationsController < ApplicationController if @application.present? # 给bot生成token,因为bot是机器人操作 @access_token = Doorkeeper::AccessToken.create!({ :application_id => @application.id, - :resource_owner_id => @bot.uid, - :scopes => "public write", - :expires_in => "604800", - :use_refresh_token => true - }) + :resource_owner_id => @bot.uid, + :scopes => "public write", + :expires_in => "604800", + :use_refresh_token => true + }) end render_ok(token: @access_token.token) end diff --git a/config/routes.rb b/config/routes.rb index d6e73aebb..3d6cc8f55 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1048,7 +1048,9 @@ Rails.application.routes.draw do resources :commit_logs, :only => [:create] scope '/app' do - post 'auth_active', to: 'installations#auth_active' + post ':id/auth_active', to: 'installations#auth_active' + post ':id/update_private_key', to: 'installations#update_private_key' + post ':id/update_secret', to: 'installations#update_secret' resources :installations do get :repositories, on: :collection member do From 3d0fa0d50cfa67f11f40e5d59edf5a8824d09cca Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 11:01:21 +0800 Subject: [PATCH 08/19] =?UTF-8?q?=E8=B0=83=E6=95=B4Bot=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/installations_controller.rb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/app/controllers/installations_controller.rb b/app/controllers/installations_controller.rb index 6e66431be..88c43cf69 100644 --- a/app/controllers/installations_controller.rb +++ b/app/controllers/installations_controller.rb @@ -51,15 +51,14 @@ class InstallationsController < ApplicationController @install_bot = BotInstall.find params[:id] @bot = @install_bot.bot @application = Doorkeeper::Application.find_by(uid: @bot.client_id, secret: @bot.client_secret) - if @application.present? - # 给bot生成token,因为bot是机器人操作 - @access_token = Doorkeeper::AccessToken.create!({ :application_id => @application.id, - :resource_owner_id => @bot.uid, - :scopes => "public write", - :expires_in => "604800", - :use_refresh_token => true - }) - end + tip_exception("该Bot未激活") if @application.blank? + # 给bot生成token,因为bot是机器人操作 + @access_token = Doorkeeper::AccessToken.create!({ :application_id => @application.id, + :resource_owner_id => @bot.uid, + :scopes => "public write", + :expires_in => "604800", + :use_refresh_token => true + }) render_ok(token: @access_token.token) end From 676d2ddce138ea3989c66143b8347cefe352677b Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 13:52:38 +0800 Subject: [PATCH 09/19] =?UTF-8?q?=E8=B0=83=E6=95=B4Bot=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/installations_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/installations_controller.rb b/app/controllers/installations_controller.rb index 88c43cf69..dd7253f2b 100644 --- a/app/controllers/installations_controller.rb +++ b/app/controllers/installations_controller.rb @@ -28,6 +28,7 @@ class InstallationsController < ApplicationController def auth_active begin @bot = Bot.find params[:id] + tip_exception("该Bot已激活") if Doorkeeper::Application.find_by(uid: @bot.client_id, secret: @bot.client_secret).present? @bot.client_id = Doorkeeper::OAuth::Helpers::UniqueToken.generate if params[:client_id].blank? @bot.client_secret = Doorkeeper::OAuth::Helpers::UniqueToken.generate if params[:client_secret].blank? @bot.private_key = OpenSSL::PKey::RSA::generate(2048).to_s From 5c3c47d13b3d42e989cb2e51a86379ae0d2b534d Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 14:37:16 +0800 Subject: [PATCH 10/19] =?UTF-8?q?=E8=B0=83=E6=95=B4Bot=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/installations_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/installations_controller.rb b/app/controllers/installations_controller.rb index dd7253f2b..20dd25e40 100644 --- a/app/controllers/installations_controller.rb +++ b/app/controllers/installations_controller.rb @@ -37,7 +37,7 @@ class InstallationsController < ApplicationController # 注册bot对应oauth应用 Doorkeeper::Application.create!(name: @bot.name, uid: @bot.client_id, secret: @bot.client_secret, redirect_uri: "https://gitlink.org.cn") # 注册bot对应用户 - result = autologin_register(User.generate_user_login('b'), nil, "b58333123413", 'bot', nickname: @bot.name) + result = autologin_register(User.generate_user_login('b'), nil, "#{SecureRandom.hex(6)}", 'bot', nickname: @bot.name) tip_exception(-1, result[:message]) if result[:message].present? @bot.uid = result[:user][:id] @bot.save From 7d3d95c406477b5b107db447ea6480bc13cc7dc5 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 15:37:01 +0800 Subject: [PATCH 11/19] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=B6=E5=90=91grimo?= =?UTF-8?q?irelab=E6=8E=A8=E9=80=81=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/issues_controller.rb | 2 + app/jobs/issue_webhook_job.rb | 181 +++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 app/jobs/issue_webhook_job.rb diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index ddb0facdf..efc7d3ea4 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -154,6 +154,8 @@ class IssuesController < ApplicationController Rails.logger.info "[ATME] maybe to at such users: #{@atme_receivers.pluck(:login)}" AtmeService.call(current_user, @atme_receivers, @issue) if @atme_receivers.size > 0 + # 新增时向grimoirelab推送事件 + IssueWebhookJob.set(wait: 5.seconds).perform_later(@issue.id) render json: {status: 0, message: "创建成功", id: @issue.id} else diff --git a/app/jobs/issue_webhook_job.rb b/app/jobs/issue_webhook_job.rb new file mode 100644 index 000000000..b85c7c1e0 --- /dev/null +++ b/app/jobs/issue_webhook_job.rb @@ -0,0 +1,181 @@ +class IssueWebhookJob < ApplicationJob + include ApplicationHelper + queue_as :message + + def perform(issue_id, user_id) + issue = Issue.find_by(id: issue_id) + return if issue.blank? + project = issue.project + user = issue.user + domain = "#{Rails.application.config_for(:configuration)['platform_url']}" + gitea_domain = GiteaService.gitea_config[:domain] + ssh_url = "git@#{gitea_domain.gsub("https://","")}:#{project.owner.login}/#{project.identifier}.git" + event_json = { + "action": "opened", + "number": 1, + "issue": { + "id": 888, + "url": "#{domain}/api/v1/#{project.owner.login}/#{project.identifier}/issues/#{issue.id}", + "html_url": "#{domain}/#{project.owner.login}/#{project.identifier}/issues/#{issue.id}", + "number": 1, + "user": { + "id": user.id, + "login": "#{user.login}", + "full_name": "", + "email": "#{user.mail}", + "avatar_url": "#{domain}/#{url_to_avatar(user)}", + "language": "", + "is_admin": false, + "last_login": "#{user.last_login_on}", + "created": "#{user.created_on}", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "website": "", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "#{user.login}" + }, + "original_author": "", + "original_author_id": 0, + "title": "#{issue.subject}", + "body": "#{issue.description}", + "ref": "", + "labels": [], + "milestone": null, + "assignee": null, + "assignees": null, + "state": "open", + "is_locked": false, + "comments": 0, + "created_at": "#{issue.created_on}", + "updated_at": "#{issue.updated_on}", + "closed_at": null, + "due_date": null, + "pull_request": null, + "repository": { + "id": 11307, + "name": "#{project.identifier}", + "owner": "#{project.owner.login}", + "full_name": "#{project.owner.login}/#{project.identifier}" + } + }, + "repository": { + "id": project.id, + "owner": { + "id": project.owner.id, + "login": "#{project.owner.login}", + "full_name": "", + "email": "#{project.owner.mail}", + "avatar_url": "#{domain}/#{url_to_avatar(project.owner)}", + "language": "", + "is_admin": false, + "last_login": "#{project.owner.last_login_on}", + "created": "#{project.owner.created_on}", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "website": "", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "#{project.owner.login}" + }, + "name": "#{project.identifier}", + "full_name": "#{project.owner.login}/#{project.identifier}", + "description": "", + "empty": false, + "private": false, + "fork": false, + "template": false, + "parent": null, + "mirror": false, + "size": 843, + "html_url": "#{domain}/#{project.owner.login}/#{project.identifier}", + "ssh_url": "#{ssh_url}", + "clone_url": "#{domain}/#{project.owner.login}/#{project.identifier}.git", + "original_url": "#{project.repository.mirror_url}", + "website": "", + "stars_count": 0, + "forks_count": 0, + "watchers_count": 1, + "open_issues_count": 1, + "open_pr_counter": 0, + "release_counter": 0, + "default_branch": "master", + "archived": false, + "created_at": "#{project.created_on}", + "updated_at": "#{project.updated_on}", + "permissions": { + "admin": true, + "push": true, + "pull": true + }, + "has_issues": true, + "internal_tracker": { + "enable_time_tracker": true, + "allow_only_contributors_to_track_time": true, + "enable_issue_dependencies": true + }, + "has_wiki": true, + "has_pull_requests": true, + "has_projects": false, + "ignore_whitespace_conflicts": false, + "allow_merge_commits": true, + "allow_rebase": true, + "allow_rebase_explicit": true, + "allow_squash_merge": true, + "default_merge_style": "merge", + "avatar_url": "", + "internal": false, + "mirror_interval": "" + }, + "sender": { + "id": user.id, + "login": "#{user.login}", + "full_name": "", + "email": "#{user.mail}", + "avatar_url": "#{domain}/#{url_to_avatar(user)}", + "language": "", + "is_admin": false, + "last_login": "#{user.last_login_on}", + "created": "#{user.created_on}", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "website": "", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "#{user.login}" + } + } + + url = URI("http://117.50.185.50:80") + http = Net::HTTP.new(url.host, url.port) + request = Net::HTTP::Post.new(url) + request["Content-Type"] = "application/json" + uuid = SecureRandom.uuid + request['X-GitHub-Delivery'] = uuid + request['X-Gitea-Delivery'] = uuid + request['X-Gogs-Delivery'] = uuid + request['X-GitHub-Event'] = 'issues' + request['X-Gitea-Event'] = 'issues' + request['X-Gogs-Event'] = 'issues' + request.body = JSON.dump(event_json) + + response = http.request(request) + Rails.logger.info "issue webhook event======#{response.read_body}" + end + +end \ No newline at end of file From 368be1849cc003dcf04991e34fc386db6978c47c Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 15:41:20 +0800 Subject: [PATCH 12/19] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=B6=E5=90=91grimo?= =?UTF-8?q?irelab=E6=8E=A8=E9=80=81=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/issues_controller.rb | 2 +- app/jobs/{issue_webhook_job.rb => issue_web_hook_job.rb} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename app/jobs/{issue_webhook_job.rb => issue_web_hook_job.rb} (99%) diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index efc7d3ea4..76aef3dc6 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -155,7 +155,7 @@ class IssuesController < ApplicationController Rails.logger.info "[ATME] maybe to at such users: #{@atme_receivers.pluck(:login)}" AtmeService.call(current_user, @atme_receivers, @issue) if @atme_receivers.size > 0 # 新增时向grimoirelab推送事件 - IssueWebhookJob.set(wait: 5.seconds).perform_later(@issue.id) + IssueWebHookJob.set(wait: 5.seconds).perform_later(@issue.id) render json: {status: 0, message: "创建成功", id: @issue.id} else diff --git a/app/jobs/issue_webhook_job.rb b/app/jobs/issue_web_hook_job.rb similarity index 99% rename from app/jobs/issue_webhook_job.rb rename to app/jobs/issue_web_hook_job.rb index b85c7c1e0..97501675f 100644 --- a/app/jobs/issue_webhook_job.rb +++ b/app/jobs/issue_web_hook_job.rb @@ -1,4 +1,4 @@ -class IssueWebhookJob < ApplicationJob +class IssueWebHookJob < ApplicationJob include ApplicationHelper queue_as :message From c872f5068547f21052a068211c1c057fca7d0327 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 15:42:15 +0800 Subject: [PATCH 13/19] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=B6=E5=90=91grimo?= =?UTF-8?q?irelab=E6=8E=A8=E9=80=81=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/issues_controller.rb | 2 +- app/jobs/{issue_web_hook_job.rb => issue_webhook_job.rb} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename app/jobs/{issue_web_hook_job.rb => issue_webhook_job.rb} (99%) diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 76aef3dc6..efc7d3ea4 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -155,7 +155,7 @@ class IssuesController < ApplicationController Rails.logger.info "[ATME] maybe to at such users: #{@atme_receivers.pluck(:login)}" AtmeService.call(current_user, @atme_receivers, @issue) if @atme_receivers.size > 0 # 新增时向grimoirelab推送事件 - IssueWebHookJob.set(wait: 5.seconds).perform_later(@issue.id) + IssueWebhookJob.set(wait: 5.seconds).perform_later(@issue.id) render json: {status: 0, message: "创建成功", id: @issue.id} else diff --git a/app/jobs/issue_web_hook_job.rb b/app/jobs/issue_webhook_job.rb similarity index 99% rename from app/jobs/issue_web_hook_job.rb rename to app/jobs/issue_webhook_job.rb index 97501675f..b85c7c1e0 100644 --- a/app/jobs/issue_web_hook_job.rb +++ b/app/jobs/issue_webhook_job.rb @@ -1,4 +1,4 @@ -class IssueWebHookJob < ApplicationJob +class IssueWebhookJob < ApplicationJob include ApplicationHelper queue_as :message From d578c5c8df8fd4fc7d3ee66824bf06647dcb3ea9 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 15:44:42 +0800 Subject: [PATCH 14/19] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=B6=E5=90=91grimo?= =?UTF-8?q?irelab=E6=8E=A8=E9=80=81=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/issue_webhook_job.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/jobs/issue_webhook_job.rb b/app/jobs/issue_webhook_job.rb index b85c7c1e0..375688663 100644 --- a/app/jobs/issue_webhook_job.rb +++ b/app/jobs/issue_webhook_job.rb @@ -2,7 +2,7 @@ class IssueWebhookJob < ApplicationJob include ApplicationHelper queue_as :message - def perform(issue_id, user_id) + def perform(issue_id) issue = Issue.find_by(id: issue_id) return if issue.blank? project = issue.project From f93e66523947ce253b3861a2814f698b1d037e72 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 15:46:15 +0800 Subject: [PATCH 15/19] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=B6=E5=90=91grimo?= =?UTF-8?q?irelab=E6=8E=A8=E9=80=81=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/issue_webhook_job.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/jobs/issue_webhook_job.rb b/app/jobs/issue_webhook_job.rb index 375688663..23a4f978a 100644 --- a/app/jobs/issue_webhook_job.rb +++ b/app/jobs/issue_webhook_job.rb @@ -46,17 +46,17 @@ class IssueWebhookJob < ApplicationJob "body": "#{issue.description}", "ref": "", "labels": [], - "milestone": null, - "assignee": null, - "assignees": null, + "milestone": nil, + "assignee": nil, + "assignees": nil, "state": "open", "is_locked": false, "comments": 0, "created_at": "#{issue.created_on}", "updated_at": "#{issue.updated_on}", - "closed_at": null, - "due_date": null, - "pull_request": null, + "closed_at": nil, + "due_date": nil, + "pull_request": nil, "repository": { "id": 11307, "name": "#{project.identifier}", @@ -95,7 +95,7 @@ class IssueWebhookJob < ApplicationJob "private": false, "fork": false, "template": false, - "parent": null, + "parent": nil, "mirror": false, "size": 843, "html_url": "#{domain}/#{project.owner.login}/#{project.identifier}", From 00bb1e82af0ee8b2701f062a6bb6dfa6715e1030 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 15:51:06 +0800 Subject: [PATCH 16/19] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=B6=E5=90=91grimo?= =?UTF-8?q?irelab=E6=8E=A8=E9=80=81=E4=BA=8B=E4=BB=B6,=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/issue_webhook_job.rb | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/app/jobs/issue_webhook_job.rb b/app/jobs/issue_webhook_job.rb index 23a4f978a..34dcdffaa 100644 --- a/app/jobs/issue_webhook_job.rb +++ b/app/jobs/issue_webhook_job.rb @@ -9,7 +9,7 @@ class IssueWebhookJob < ApplicationJob user = issue.user domain = "#{Rails.application.config_for(:configuration)['platform_url']}" gitea_domain = GiteaService.gitea_config[:domain] - ssh_url = "git@#{gitea_domain.gsub("https://","")}:#{project.owner.login}/#{project.identifier}.git" + ssh_url = "git@#{gitea_domain.gsub("https://", "")}:#{project.owner.login}/#{project.identifier}.git" event_json = { "action": "opened", "number": 1, @@ -161,21 +161,26 @@ class IssueWebhookJob < ApplicationJob } } - url = URI("http://117.50.185.50:80") - http = Net::HTTP.new(url.host, url.port) - request = Net::HTTP::Post.new(url) - request["Content-Type"] = "application/json" - uuid = SecureRandom.uuid - request['X-GitHub-Delivery'] = uuid - request['X-Gitea-Delivery'] = uuid - request['X-Gogs-Delivery'] = uuid - request['X-GitHub-Event'] = 'issues' - request['X-Gitea-Event'] = 'issues' - request['X-Gogs-Event'] = 'issues' - request.body = JSON.dump(event_json) + begin + url = URI("http://117.50.185.50:80") + http = Net::HTTP.new(url.host, url.port) + request = Net::HTTP::Post.new(url) + request["Content-Type"] = "application/json" + uuid = SecureRandom.uuid + request['X-GitHub-Delivery'] = uuid + request['X-Gitea-Delivery'] = uuid + request['X-Gogs-Delivery'] = uuid + request['X-GitHub-Event'] = 'issues' + request['X-Gitea-Event'] = 'issues' + request['X-Gogs-Event'] = 'issues' + request.body = JSON.dump(event_json) - response = http.request(request) - Rails.logger.info "issue webhook event======#{response.read_body}" + response = http.request(request) + Rails.logger.info "issue webhook event======#{response.read_body}" + rescue Exception => e + Rails.logger.info "issue webhook event error======#{e.message}" + puts "issue webhook event error======#{e.message}" + end end end \ No newline at end of file From 0725ef74de94b0041aafcbd567ab8b53ecaa8cd9 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 15:54:43 +0800 Subject: [PATCH 17/19] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=B6=E5=90=91grimo?= =?UTF-8?q?irelab=E6=8E=A8=E9=80=81=E4=BA=8B=E4=BB=B6,=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/issue_webhook_job.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/jobs/issue_webhook_job.rb b/app/jobs/issue_webhook_job.rb index 34dcdffaa..445a0b02c 100644 --- a/app/jobs/issue_webhook_job.rb +++ b/app/jobs/issue_webhook_job.rb @@ -176,10 +176,10 @@ class IssueWebhookJob < ApplicationJob request.body = JSON.dump(event_json) response = http.request(request) - Rails.logger.info "issue webhook event======#{response.read_body}" + Rails.logger.info "issue #{issue_id} webhook event======#{response.read_body}" rescue Exception => e - Rails.logger.info "issue webhook event error======#{e.message}" - puts "issue webhook event error======#{e.message}" + Rails.logger.info "issue #{issue_id} webhook event error======#{e.message}" + puts "issue: #{issue_id} webhook event error======#{e.message}" end end From e92b288f4167d9db00b89f097b0697621d921276 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 16:07:51 +0800 Subject: [PATCH 18/19] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=B6=E5=90=91grimo?= =?UTF-8?q?irelab=E6=8E=A8=E9=80=81=E4=BA=8B=E4=BB=B6,=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86,log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/issue_webhook_job.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/jobs/issue_webhook_job.rb b/app/jobs/issue_webhook_job.rb index 445a0b02c..abd8a2322 100644 --- a/app/jobs/issue_webhook_job.rb +++ b/app/jobs/issue_webhook_job.rb @@ -160,7 +160,7 @@ class IssueWebhookJob < ApplicationJob "username": "#{user.login}" } } - + puts "event_json:#{event_json.to_json}" begin url = URI("http://117.50.185.50:80") http = Net::HTTP.new(url.host, url.port) From 3427fa676850a74489c90319530b38e2b34743cc Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 16 Feb 2023 16:11:40 +0800 Subject: [PATCH 19/19] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=B6=E5=90=91grimo?= =?UTF-8?q?irelab=E6=8E=A8=E9=80=81=E4=BA=8B=E4=BB=B6,=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/issue_webhook_job.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/jobs/issue_webhook_job.rb b/app/jobs/issue_webhook_job.rb index abd8a2322..4027a55b2 100644 --- a/app/jobs/issue_webhook_job.rb +++ b/app/jobs/issue_webhook_job.rb @@ -14,7 +14,7 @@ class IssueWebhookJob < ApplicationJob "action": "opened", "number": 1, "issue": { - "id": 888, + "id": issue.id, "url": "#{domain}/api/v1/#{project.owner.login}/#{project.identifier}/issues/#{issue.id}", "html_url": "#{domain}/#{project.owner.login}/#{project.identifier}/issues/#{issue.id}", "number": 1, @@ -160,7 +160,7 @@ class IssueWebhookJob < ApplicationJob "username": "#{user.login}" } } - puts "event_json:#{event_json.to_json}" + # puts "event_json:#{event_json.to_json}" begin url = URI("http://117.50.185.50:80") http = Net::HTTP.new(url.host, url.port)