Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge VSTS CI source into Azure Pipelines CI source; add local support for VSTS request source #1416

Merged
2 changes: 0 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ Lint/MissingSuper:
- 'lib/danger/ci_source/surf.rb'
- 'lib/danger/ci_source/teamcity.rb'
- 'lib/danger/ci_source/travis.rb'
- 'lib/danger/ci_source/vsts.rb'
- 'lib/danger/ci_source/xcode_cloud.rb'
- 'lib/danger/ci_source/xcode_server.rb'
- 'lib/danger/danger_core/standard_error.rb'
Expand Down Expand Up @@ -291,7 +290,6 @@ Style/FrozenStringLiteralComment:
- 'lib/danger/ci_source/surf.rb'
- 'lib/danger/ci_source/teamcity.rb'
- 'lib/danger/ci_source/travis.rb'
- 'lib/danger/ci_source/vsts.rb'
- 'lib/danger/ci_source/xcode_cloud.rb'
- 'lib/danger/ci_source/xcode_server.rb'
- 'lib/danger/clients/rubygems_client.rb'
Expand Down
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

## master
* Fix offenses for RuboCop that can be fixed automatically. [@ydah](https://github.com/ydah)

* Add `ref` option to `import_dangerfile` method as an alias of `branch` option. [@manicmaniac](https://github.com/manicmaniac) [#1394](https://github.com/danger/danger/issues/1394)
* Fixed incorrect help messages for `danger plugins lint` and `danger plugins readme`. [@manicmaniac](https://github.com/manicmaniac) [#1397](https://github.com/danger/danger/issues/1397)

* Merge AzurePipelines and VSTS CI sources, and add support for VSTS as request source for local PR execution [@sphanley][https://github.com/sphanley]
<!-- Your comment above here -->

## 9.1.0
Expand Down
23 changes: 19 additions & 4 deletions lib/danger/ci_source/azure_pipelines.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables
require "uri"
require "danger/request_sources/github/github"
require "danger/request_sources/vsts"

module Danger
# ### CI Setup
Expand All @@ -15,15 +16,28 @@ module Danger
#
# ### Token Setup
#
# Add the `DANGER_GITHUB_API_TOKEN` to your environment variables.
# #### GitHub
#
# You need to add the `DANGER_GITHUB_API_TOKEN` environment variable, to do this, go to your build definition's variables tab.
# #
# #### Azure Git
#
# You need to add the `DANGER_VSTS_API_TOKEN` and `DANGER_VSTS_HOST` environment variable, to do this,
# go to your build definition's variables tab. The `DANGER_VSTS_API_TOKEN` is your vsts personal access token.
# Instructions for creating a personal access token can be found [here](https://www.visualstudio.com/en-us/docs/setup-admin/team-services/use-personal-access-tokens-to-authenticate).
# For the `DANGER_VSTS_HOST` variable the suggested value is `$(System.TeamFoundationCollectionUri)$(System.TeamProject)`
# which will automatically get your vsts domain and your project name needed for the vsts api.
#
class AzurePipelines < CI


def self.validates_as_ci?(env)
has_all_variables = ["AGENT_ID", "BUILD_SOURCEBRANCH", "BUILD_REPOSITORY_URI", "BUILD_REASON", "BUILD_REPOSITORY_NAME"].all? { |x| env[x] && !env[x].empty? }

# AGENT_ID is being used by AppCenter as well, so checking here to make sure AppCenter CI doesn't get a false positive for AzurePipelines
# Anyone working with AzurePipelines could provide a better/truly unique env key to avoid checking for AppCenter
!Danger::Appcenter.validates_as_ci?(env) &&
env.key?("AGENT_ID") &&
env["BUILD_REPOSITORY_PROVIDER"] != "TfsGit"
has_all_variables
end

def self.validates_as_pr?(env)
Expand All @@ -35,7 +49,8 @@ def supported_request_sources
Danger::RequestSources::GitHub,
Danger::RequestSources::GitLab,
Danger::RequestSources::BitbucketServer,
Danger::RequestSources::BitbucketCloud
Danger::RequestSources::BitbucketCloud,
Danger::RequestSources::VSTS
]
end

Expand Down
7 changes: 6 additions & 1 deletion lib/danger/ci_source/local_git_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ def run_git(command)
end

def supported_request_sources
@supported_request_sources ||= [Danger::RequestSources::GitHub, Danger::RequestSources::BitbucketServer, Danger::RequestSources::BitbucketCloud]
@supported_request_sources ||= [
Danger::RequestSources::GitHub,
Danger::RequestSources::BitbucketServer,
Danger::RequestSources::BitbucketCloud,
Danger::RequestSources::VSTS
]
end

def initialize(env = {})
Expand Down
5 changes: 3 additions & 2 deletions lib/danger/ci_source/support/find_repo_info_from_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ module Danger
class FindRepoInfoFromURL
REGEXP = %r{
://[^/]+/
(?<slug>[^/]+(/[^/]+){1,2})
(/(pull|merge_requests|pull-requests)/)
(([^/]+/){1,2}_git/)?
(?<slug>[^/]+(/[^/]+){0,2})
(/(pull|pullrequest|merge_requests|pull-requests)/)
(?<id>\d+)
}x.freeze

Expand Down
12 changes: 12 additions & 0 deletions lib/danger/ci_source/support/pull_request_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def generate_remote_pull_request(remote_pull_request)
remote_pull_request.head.sha,
remote_pull_request.base.sha
)
when :vsts
RemotePullRequest.new(
remote_pull_request[:pullRequestId].to_s,
remote_pull_request[:lastMergeSourceCommit][:commitId],
remote_pull_request[:lastMergeTargetCommit][:commitId]
)
else
raise "SCM provider not supported: #{scm_provider}"
end
Expand Down Expand Up @@ -140,6 +146,10 @@ def client(env)
require "danger/request_sources/bitbucket_server_api"
project, slug = repo_slug.split("/")
RequestSources::BitbucketServerAPI.new(project, slug, specific_pull_request_id, env)

when :vsts
require "danger/request_sources/vsts_api"
RequestSources::VSTSAPI.new(repo_slug, specific_pull_request_id, env)

when :github
require "octokit"
Expand Down Expand Up @@ -170,6 +180,8 @@ def find_scm_provider(remote_url)
:bitbucket_cloud
elsif remote_url =~ %r{/pull-requests/}
:bitbucket_server
elsif remote_url =~ /\.visualstudio\.com/i || remote_url =~ /dev\.azure\.com/i
:vsts
else
:github
end
Expand Down
73 changes: 0 additions & 73 deletions lib/danger/ci_source/vsts.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/danger/commands/local_helpers/local_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def setup(verbose: false)

if gh.instance_of? Danger::RequestSources::BitbucketServer
cork.puts "Running your Dangerfile against this PR - #{gh.host}/projects/#{source.repo_slug.split('/').first}/repos/#{source.repo_slug.split('/').last}/pull-requests/#{source.pull_request_id}"
elsif gh.instance_of? Danger::RequestSources::VSTS
cork.puts "Running your Dangerfile against this PR - #{gh.client.pr_api_endpoint}"
else
cork.puts "Running your Dangerfile against this PR - https://#{gh.host}/#{source.repo_slug}/pull/#{source.pull_request_id}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/danger/danger_core/environment_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def get_repo_source(repo_url)
RequestSources::GitLab
elsif repo_url =~ /bitbucket\.(org|com)/i
RequestSources::BitbucketCloud
elsif repo_url =~ /dev\.azure\.com/i
elsif repo_url =~ /\.visualstudio\.com/i || repo_url =~ /dev\.azure\.com/i
RequestSources::VSTS
end
end
Expand Down
7 changes: 3 additions & 4 deletions lib/danger/request_sources/vsts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ def self.optional_env_vars
def initialize(ci_source, environment)
self.ci_source = ci_source

@is_vsts_git = environment["BUILD_REPOSITORY_PROVIDER"] == "TfsGit"
@is_vsts_ci = environment.key? "DANGER_VSTS_HOST"

project, slug = ci_source.repo_slug.split("/")
@api = VSTSAPI.new(project, slug, ci_source.pull_request_id, environment)
@api = VSTSAPI.new(ci_source.repo_slug, ci_source.pull_request_id, environment)
end

def validates_as_ci?
@is_vsts_git
@is_vsts_ci
end

def validates_as_api_source?
Expand Down
6 changes: 5 additions & 1 deletion lib/danger/request_sources/vsts_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module RequestSources
class VSTSAPI
attr_accessor :host, :pr_api_endpoint, :min_api_version_for_comments

def initialize(_project, slug, pull_request_id, environment)
def initialize(slug, pull_request_id, environment)
self.min_api_version_for_comments = "3.0"

user_name = ""
Expand Down Expand Up @@ -44,6 +44,10 @@ def credentials_given?
@token && !@token.empty?
end

def pull_request(*)
fetch_pr_json
end

def fetch_pr_json
uri = URI("#{pr_api_endpoint}?api-version=#{@api_version}")
fetch_json(uri)
Expand Down
10 changes: 5 additions & 5 deletions spec/lib/danger/ci_sources/azure_pipelines_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "danger/ci_source/vsts"
require "danger/ci_source/azure_pipelines"

RSpec.describe Danger::AzurePipelines do
let(:valid_env) do
Expand Down Expand Up @@ -28,9 +28,9 @@
expect(described_class.validates_as_ci?(valid_env)).to be true
end

it "doesn't validate when `BUILD_REPOSITORY_PROVIDER` is TfsGit" do
it "validates when `BUILD_REPOSITORY_PROVIDER` is TfsGit" do
valid_env["BUILD_REPOSITORY_PROVIDER"] = "TfsGit"
expect(described_class.validates_as_ci?(valid_env)).to be false
expect(described_class.validates_as_ci?(valid_env)).to be true
end

it "doesn't validate when require env variables are not set" do
Expand Down Expand Up @@ -75,8 +75,8 @@
end

describe "#supported_request_sources" do
it "doesn't supports VSTS" do
expect(source.supported_request_sources).not_to include(Danger::RequestSources::VSTS)
it "supports VSTS" do
expect(source.supported_request_sources).to include(Danger::RequestSources::VSTS)
end

it "supports GitHub" do
Expand Down
1 change: 0 additions & 1 deletion spec/lib/danger/ci_sources/ci_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"Danger::Surf",
"Danger::TeamCity",
"Danger::Travis",
"Danger::VSTS",
"Danger::XcodeCloud",
"Danger::XcodeServer"
]
Expand Down
35 changes: 35 additions & 0 deletions spec/lib/danger/ci_sources/support/find_repo_info_from_url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,39 @@
)
end
end

context "Azure Pipelines" do
it "legacy URL format works" do
result = described_class.new("https://example.visualstudio.com/Test/_git/test/pullrequest/1946").call

expect(result).to have_attributes(
slug: "test",
id: "1946"
)
end
it "new URL format works" do
result = described_class.new("https://dev.azure.com/example/Test/_git/test/pullrequest/1946").call

expect(result).to have_attributes(
slug: "test",
id: "1946"
)
end
it "legacy URL format works with http + trailing slash" do
result = described_class.new("http://example.visualstudio.com/Test/_git/test/pullrequest/1946/").call

expect(result).to have_attributes(
slug: "test",
id: "1946"
)
end
it "new URL format works with http + trailing slash" do
result = described_class.new("http://dev.azure.com/example/Test/_git/test/pullrequest/1946/").call

expect(result).to have_attributes(
slug: "test",
id: "1946"
)
end
end
end
Loading