2018-08-10 02:45:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-09-24 18:56:25 -04:00
|
|
|
class PivotaltrackerService < Service
|
2019-08-31 15:57:00 -04:00
|
|
|
API_ENDPOINT = 'https://www.pivotaltracker.com/services/v5/source_commits'
|
2016-08-10 10:15:27 -04:00
|
|
|
|
|
|
|
prop_accessor :token, :restrict_to_branch
|
2013-09-24 18:56:25 -04:00
|
|
|
validates :token, presence: true, if: :activated?
|
|
|
|
|
|
|
|
def title
|
|
|
|
'PivotalTracker'
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
2019-04-12 08:28:07 -04:00
|
|
|
s_('PivotalTrackerService|Project Management Software (Source Commits Endpoint)')
|
2013-09-24 18:56:25 -04:00
|
|
|
end
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.to_param
|
2013-09-24 18:56:25 -04:00
|
|
|
'pivotaltracker'
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
[
|
2016-08-10 10:15:27 -04:00
|
|
|
{
|
|
|
|
type: 'text',
|
|
|
|
name: 'token',
|
2019-04-12 08:28:07 -04:00
|
|
|
placeholder: s_('PivotalTrackerService|Pivotal Tracker API token.'),
|
2017-05-22 06:07:12 -04:00
|
|
|
required: true
|
2016-08-10 10:15:27 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'text',
|
|
|
|
name: 'restrict_to_branch',
|
2019-04-12 08:28:07 -04:00
|
|
|
placeholder: s_('PivotalTrackerService|Comma-separated list of branches which will be ' \
|
|
|
|
'automatically inspected. Leave blank to include all branches.')
|
2016-08-10 10:15:27 -04:00
|
|
|
}
|
2013-09-24 18:56:25 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.supported_events
|
2015-02-28 11:33:18 -05:00
|
|
|
%w(push)
|
|
|
|
end
|
|
|
|
|
2015-02-20 08:49:26 -05:00
|
|
|
def execute(data)
|
2015-02-28 11:33:18 -05:00
|
|
|
return unless supported_events.include?(data[:object_kind])
|
2016-08-10 10:15:27 -04:00
|
|
|
return unless allowed_branch?(data[:ref])
|
2015-02-19 00:02:57 -05:00
|
|
|
|
2015-02-20 08:49:26 -05:00
|
|
|
data[:commits].each do |commit|
|
2013-09-25 10:54:34 -04:00
|
|
|
message = {
|
|
|
|
'source_commit' => {
|
|
|
|
'commit_id' => commit[:id],
|
|
|
|
'author' => commit[:author][:name],
|
|
|
|
'url' => commit[:url],
|
|
|
|
'message' => commit[:message]
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 18:38:25 -04:00
|
|
|
Gitlab::HTTP.post(
|
2016-08-10 10:15:27 -04:00
|
|
|
API_ENDPOINT,
|
2013-09-25 10:54:34 -04:00
|
|
|
body: message.to_json,
|
|
|
|
headers: {
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'X-TrackerToken' => token
|
|
|
|
}
|
|
|
|
)
|
2013-09-24 18:56:25 -04:00
|
|
|
end
|
|
|
|
end
|
2016-08-10 10:15:27 -04:00
|
|
|
|
|
|
|
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
|
2013-09-24 18:56:25 -04:00
|
|
|
end
|