2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
class PivotaltrackerService < Service
|
2017-08-17 22:00:37 +05:30
|
|
|
API_ENDPOINT = 'https://www.pivotaltracker.com/services/v5/source_commits'.freeze
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
prop_accessor :token, :restrict_to_branch
|
2014-09-02 18:07:02 +05:30
|
|
|
validates :token, presence: true, if: :activated?
|
|
|
|
|
|
|
|
def title
|
|
|
|
'PivotalTracker'
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
'Project Management Software (Source Commits Endpoint)'
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.to_param
|
2014-09-02 18:07:02 +05:30
|
|
|
'pivotaltracker'
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
[
|
2016-09-13 17:45:13 +05:30
|
|
|
{
|
|
|
|
type: 'text',
|
|
|
|
name: 'token',
|
2017-09-10 17:25:29 +05:30
|
|
|
placeholder: 'Pivotal Tracker API token.',
|
|
|
|
required: true
|
2016-09-13 17:45:13 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'text',
|
|
|
|
name: 'restrict_to_branch',
|
|
|
|
placeholder: 'Comma-separated list of branches which will be ' \
|
|
|
|
'automatically inspected. Leave blank to include all branches.'
|
|
|
|
}
|
2014-09-02 18:07:02 +05:30
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.supported_events
|
2015-04-26 12:48:37 +05:30
|
|
|
%w(push)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(data)
|
|
|
|
return unless supported_events.include?(data[:object_kind])
|
2016-09-13 17:45:13 +05:30
|
|
|
return unless allowed_branch?(data[:ref])
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
data[:commits].each do |commit|
|
2014-09-02 18:07:02 +05:30
|
|
|
message = {
|
|
|
|
'source_commit' => {
|
|
|
|
'commit_id' => commit[:id],
|
|
|
|
'author' => commit[:author][:name],
|
|
|
|
'url' => commit[:url],
|
|
|
|
'message' => commit[:message]
|
|
|
|
}
|
|
|
|
}
|
2018-03-26 14:24:53 +05:30
|
|
|
Gitlab::HTTP.post(
|
2016-09-13 17:45:13 +05:30
|
|
|
API_ENDPOINT,
|
2014-09-02 18:07:02 +05:30
|
|
|
body: message.to_json,
|
|
|
|
headers: {
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'X-TrackerToken' => token
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def allowed_branch?(ref)
|
|
|
|
return true unless ref.present? && restrict_to_branch.present?
|
|
|
|
|
|
|
|
branch = Gitlab::Git.ref_name(ref)
|
|
|
|
allowed_branches = restrict_to_branch.split(',').map(&:strip)
|
|
|
|
|
|
|
|
branch.present? && allowed_branches.include?(branch)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|