diff --git a/11.rb b/11.rb new file mode 100644 index 0000000000000000000000000000000000000000..6f9d20497fd9cd45d86452381b7c3d116e49bebc --- /dev/null +++ b/11.rb @@ -0,0 +1,109 @@ +# frozen_string_literal: true + +class SwaggerController < ApplicationController + include SwaggerHelper + + before_action :i18n_to_chinese + + layout 'swagger' + + def index + gon.doc_version = doc_version + + if api_v8? && cookies[:enterprise_access_token].blank? + return @auth_url = get_auth_url if params[:code].blank? + cookies[:enterprise_access_token] = get_access_token + redirect_to '/api/v8/swagger' + elsif api_v5? && cookies[:access_token].blank? + return @auth_url = get_auth_url if params[:code].blank? + cookies[:access_token] = get_access_token + redirect_to '/api/v5/swagger' + elsif api_v1? && cookies[:access_token].blank? + return @auth_url = get_auth_url if params[:code].blank? + cookies[:access_token] = get_access_token + redirect_to '/api/v1/swagger' + end + rescue => e + Gitee::AppLogger.info "#{self.class.name}##{__method__}: #{e}" + end + + def oauth + + end + + def doc_json + if api_v5? && params[:from] == 'file' + result = Rails.root.join('public/swagger/v5.json').read + result.sub!('"gitee.com"', "\"#{Settings.gitlab.host}\"") # host + result.sub!('https://gitee.com/oauth/authorize', "#{Settings.gitlab.host_with_protocol}/oauth/authorize") + result.sub!('https://gitee.com/oauth/token', "#{Settings.gitlab.host_with_protocol}/oauth/token") + return render json: result, layout: false + end + + if Rails.env.development? + result = Net::HTTP.get(URI.parse(swagger_doc_url)) + else + result = Rails.cache.fetch("API_DOC_#{get_host}_#{doc_version}", expires_in: 12.hour) do + Net::HTTP.get(URI.parse(swagger_doc_url)) + end + end + render json: result, layout: false + end + + def doc_version + if api_v8? + ApiEnterprise::DOC_VERSION + elsif api_v1? + ApiV1::DOC_VERSION + else + ApiVv5::DOC_VERSION + end + end + + def swagger_doc_url + if api_v8? + "#{get_host_url}/enterprises/swagger_doc.json" + elsif api_v1? + "#{get_host_url}/api/v1/swagger_doc.json" + else + "#{get_host_url}/api/v5/swagger_doc.json" + end + end + + private + + def get_access_token + body = RestClient.post("#{get_host_url}/oauth/token", system_application_oauth) + { + value: JSON.parse(body)['access_token'], + expires: 1.day.from_now + } + end + + def i18n_to_chinese + I18n.locale = 'zh-CN' + end + + def swagger_config + if api_v8? + Settings.enterprise_swagger.symbolize_keys + else + Settings.swagger.symbolize_keys + end + end + + def get_auth_url + hash = swagger_config.slice(:client_id, :redirect_uri) + hash[:redirect_uri] = hash[:redirect_uri].sub('v5', 'v1') if api_v1? + hash[:response_type] = 'code' + "/oauth/authorize?#{URI.encode_www_form(hash)}" + end + + def system_application_oauth + hash = swagger_config.slice(:client_id, :client_secret,:redirect_uri) + hash[:redirect_uri] = hash[:redirect_uri].sub('v5', 'v1') if api_v1? + hash[:grant_type] = 'authorization_code' + hash[:code] = params[:code] + hash + end +end