2013-09-24 18:56:25 -04:00
|
|
|
class PivotaltrackerService < Service
|
|
|
|
include HTTParty
|
|
|
|
|
2016-08-10 10:15:27 -04:00
|
|
|
API_ENDPOINT = 'https://www.pivotaltracker.com/services/v5/source_commits'
|
|
|
|
|
|
|
|
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
|
|
|
|
'Project Management Software (Source Commits Endpoint)'
|
|
|
|
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',
|
|
|
|
placeholder: 'Pivotal Tracker API token.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'text',
|
|
|
|
name: 'restrict_to_branch',
|
|
|
|
placeholder: 'Comma-separated list of branches which will be ' \
|
|
|
|
'automatically inspected. Leave blank to include all branches.'
|
|
|
|
}
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PivotaltrackerService.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
|