2020-09-01 08:11:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Atlassian
|
|
|
|
module JiraConnect
|
|
|
|
class Client < Gitlab::HTTP
|
2020-11-02 22:08:56 -05:00
|
|
|
def self.generate_update_sequence_id
|
|
|
|
Gitlab::Metrics::System.monotonic_time.to_i
|
|
|
|
end
|
|
|
|
|
2020-09-01 08:11:01 -04:00
|
|
|
def initialize(base_uri, shared_secret)
|
|
|
|
@base_uri = base_uri
|
|
|
|
@shared_secret = shared_secret
|
|
|
|
end
|
|
|
|
|
2020-11-02 22:08:56 -05:00
|
|
|
def store_dev_info(project:, commits: nil, branches: nil, merge_requests: nil, update_sequence_id: nil)
|
2020-09-01 08:11:01 -04:00
|
|
|
dev_info_json = {
|
|
|
|
repositories: [
|
|
|
|
Serializers::RepositoryEntity.represent(
|
|
|
|
project,
|
|
|
|
commits: commits,
|
|
|
|
branches: branches,
|
2020-11-02 22:08:56 -05:00
|
|
|
merge_requests: merge_requests,
|
|
|
|
update_sequence_id: update_sequence_id
|
2020-09-01 08:11:01 -04:00
|
|
|
)
|
|
|
|
]
|
|
|
|
}.to_json
|
|
|
|
|
|
|
|
uri = URI.join(@base_uri, '/rest/devinfo/0.10/bulk')
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
'Authorization' => "JWT #{jwt_token('POST', uri)}",
|
|
|
|
'Content-Type' => 'application/json'
|
|
|
|
}
|
|
|
|
|
|
|
|
self.class.post(uri, headers: headers, body: dev_info_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def jwt_token(http_method, uri)
|
|
|
|
claims = Atlassian::Jwt.build_claims(
|
|
|
|
Atlassian::JiraConnect.app_key,
|
|
|
|
uri,
|
|
|
|
http_method,
|
|
|
|
@base_uri
|
|
|
|
)
|
|
|
|
|
|
|
|
Atlassian::Jwt.encode(claims, @shared_secret)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|