Merge branch 'pivotaltracker-restrict-branch' into 'master'

Ability to restrict branches for pivotal tracker integration

## What does this MR do?
It allows to specify branches which you want to track for pivotal tracker integration.

## Why was this MR needed?
Typical use case: send commits to PivotalTracker and finish the story only when the feature branch is merged into master.

See merge request !5752
This commit is contained in:
Jeroen van Baarsen 2016-08-15 10:12:39 +00:00
commit edfb9c7867
4 changed files with 101 additions and 6 deletions

View file

@ -2,6 +2,7 @@ Please view this file on the master branch, on stable branches it's out of date.
v 8.11.0 (unreleased)
- Remove the http_parser.rb dependency by removing the tinder gem. !5758 (tbalthazar)
- Ability to specify branches for Pivotal Tracker integration (Egor Lynko)
- Fix don't pass a local variable called `i` to a partial. !20510 (herminiotorres)
- Fix rename `add_users_into_project` and `projects_ids`. !20512 (herminiotorres)
- Fix the title of the toggle dropdown button. !5515 (herminiotorres)

View file

@ -1,7 +1,9 @@
class PivotaltrackerService < Service
include HTTParty
prop_accessor :token
API_ENDPOINT = 'https://www.pivotaltracker.com/services/v5/source_commits'
prop_accessor :token, :restrict_to_branch
validates :token, presence: true, if: :activated?
def title
@ -18,7 +20,17 @@ class PivotaltrackerService < Service
def fields
[
{ type: 'text', name: 'token', placeholder: '' }
{
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.'
}
]
end
@ -28,8 +40,8 @@ class PivotaltrackerService < Service
def execute(data)
return unless supported_events.include?(data[:object_kind])
return unless allowed_branch?(data[:ref])
url = 'https://www.pivotaltracker.com/services/v5/source_commits'
data[:commits].each do |commit|
message = {
'source_commit' => {
@ -40,7 +52,7 @@ class PivotaltrackerService < Service
}
}
PivotaltrackerService.post(
url,
API_ENDPOINT,
body: message.to_json,
headers: {
'Content-Type' => 'application/json',
@ -49,4 +61,15 @@ class PivotaltrackerService < Service
)
end
end
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
end

View file

@ -355,7 +355,7 @@ PUT /projects/:id/services/gemnasium
Parameters:
- `api_key` (**required**) - Your personal API KEY on gemnasium.com
- `api_key` (**required**) - Your personal API KEY on gemnasium.com
- `token` (**required**) - The project's slug on gemnasium.com
### Delete Gemnasium service
@ -503,6 +503,7 @@ PUT /projects/:id/services/pivotaltracker
Parameters:
- `token` (**required**)
- `restrict_to_branch` (optional) - Comma-separated list of branches which will be automatically inspected. Leave blank to include all branches.
### Delete PivotalTracker service
@ -661,4 +662,3 @@ Get JetBrains TeamCity CI service settings for a project.
```
GET /projects/:id/services/teamcity
```

View file

@ -39,4 +39,75 @@ describe PivotaltrackerService, models: true do
it { is_expected.not_to validate_presence_of(:token) }
end
end
describe 'Execute' do
let(:service) do
PivotaltrackerService.new.tap do |service|
service.token = 'secret_api_token'
end
end
let(:url) { PivotaltrackerService::API_ENDPOINT }
def push_data(branch: 'master')
{
object_kind: 'push',
ref: "refs/heads/#{branch}",
commits: [
{
id: '21c12ea',
author: {
name: 'Some User'
},
url: 'https://example.com/commit',
message: 'commit message',
}
]
}
end
before do
WebMock.stub_request(:post, url)
end
it 'should post correct message' do
service.execute(push_data)
expect(WebMock).to have_requested(:post, url).with(
body: {
'source_commit' => {
'commit_id' => '21c12ea',
'author' => 'Some User',
'url' => 'https://example.com/commit',
'message' => 'commit message'
}
},
headers: {
'Content-Type' => 'application/json',
'X-TrackerToken' => 'secret_api_token'
}
).once
end
context 'when allowed branches is specified' do
let(:service) do
super().tap do |service|
service.restrict_to_branch = 'master,v10'
end
end
it 'should post message if branch is in the list' do
service.execute(push_data(branch: 'master'))
service.execute(push_data(branch: 'v10'))
expect(WebMock).to have_requested(:post, url).twice
end
it 'should not post message if branch is not in the list' do
service.execute(push_data(branch: 'mas'))
service.execute(push_data(branch: 'v11'))
expect(WebMock).not_to have_requested(:post, url)
end
end
end
end