forked from Trustie/forgeplus
11111
This commit is contained in:
parent
41515575f1
commit
9cf0a04455
|
@ -0,0 +1,47 @@
|
|||
class Huawei::CodeArtsChecksController < ApplicationController
|
||||
before_action :load_repository
|
||||
before_action :signer
|
||||
def index
|
||||
task_id = "xxxxxx" # 创建任务返回的 task_id
|
||||
url = "https://codearts-check.#{region}.myhuaweicloud.com/v2/#{project_id}/task/#{task_id}"
|
||||
|
||||
headers = @signer.sign_request("GET", url)
|
||||
|
||||
response = RestClient.get(url, headers)
|
||||
|
||||
puts JSON.pretty_generate(JSON.parse(response.body))
|
||||
|
||||
end
|
||||
def create
|
||||
branch = params[:branch] || "master"
|
||||
project_id = @project.id
|
||||
region = "cn-north-4"
|
||||
url = "https://codearts-check.#{region}.myhuaweicloud.com/v2/#{project_id}/task"
|
||||
|
||||
|
||||
body = {
|
||||
"name":@project.name,
|
||||
"project_id":project_id,
|
||||
"git_url": @project.repository.url,
|
||||
"branch": branch
|
||||
}.to_json
|
||||
|
||||
headers = @signer.sign_request("POST", url, {}, body)
|
||||
|
||||
response = RestClient.post(url, body, headers)
|
||||
|
||||
puts JSON.pretty_generate(JSON.parse(response.body))
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
private
|
||||
|
||||
def signer
|
||||
ak = ENV['HUAWEI_CLOUD_AK']
|
||||
sk = ENV['HUAWEI_CLOUD_SK']
|
||||
@signer = Huawei::Signer.new(ak, sk)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,53 @@
|
|||
require 'openssl'
|
||||
require 'base64'
|
||||
require 'json'
|
||||
require 'uri'
|
||||
require 'time'
|
||||
|
||||
class Huawei::Signer
|
||||
def initialize(ak, sk)
|
||||
@ak = ak
|
||||
@sk = sk
|
||||
end
|
||||
|
||||
def sign_request(method, url, headers = {}, body = "")
|
||||
uri = URI.parse(url)
|
||||
host = uri.host
|
||||
path = uri.path.empty? ? "/" : uri.path
|
||||
query = uri.query ? "?" + uri.query : ""
|
||||
|
||||
# 时间戳
|
||||
x_sdk_date = Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
|
||||
|
||||
# Step 1: CanonicalRequest
|
||||
canonical_headers = "host:#{host}\n" \
|
||||
"x-sdk-date:#{x_sdk_date}\n"
|
||||
|
||||
signed_headers = "host;x-sdk-date"
|
||||
|
||||
hashed_payload = Digest::SHA256.hexdigest(body)
|
||||
|
||||
canonical_request = "#{method}\n#{path}\n#{query}\n#{canonical_headers}\n#{signed_headers}\n#{hashed_payload}"
|
||||
|
||||
# Step 2: StringToSign
|
||||
hashed_canonical = Digest::SHA256.hexdigest(canonical_request)
|
||||
string_to_sign = "SDK-HMAC-SHA256\n#{x_sdk_date}\n#{hashed_canonical}"
|
||||
|
||||
# Step 3: 计算签名
|
||||
signature = OpenSSL::HMAC.hexdigest("SHA256", @sk, string_to_sign)
|
||||
|
||||
# Step 4: 生成 Authorization
|
||||
authorization = "SDK-HMAC-SHA256 Access=#{@ak}, SignedHeaders=#{signed_headers}, Signature=#{signature}"
|
||||
|
||||
# 合并 headers
|
||||
signed_headers_hash = {
|
||||
"Authorization" => authorization,
|
||||
"X-Sdk-Date" => x_sdk_date,
|
||||
"Host" => host,
|
||||
"Content-Type" => "application/json"
|
||||
}
|
||||
|
||||
headers.merge!(signed_headers_hash)
|
||||
headers
|
||||
end
|
||||
end
|
|
@ -42,6 +42,11 @@ Rails.application.routes.draw do
|
|||
|
||||
scope '/api' do
|
||||
resources :topics, only: [:index]
|
||||
|
||||
namespace :huawei do
|
||||
resources :code_arts_checks,only: [:index, :create]
|
||||
end
|
||||
|
||||
namespace :ci do
|
||||
resources :languages, only: [:index, :show] do
|
||||
collection do
|
||||
|
|
Loading…
Reference in New Issue