Class: Danger::VSTS

Inherits:
CI
  • Object
show all
Defined in:
lib/danger/ci_source/vsts.rb

Overview

### CI Setup

You need to go to your project’s build definiton. Then add a “Command Line” Task with the “Tool” field set to “bundle” and the “Arguments” field set to “exec danger”.

### Token Setup

#### GitHub

You need to add the ‘DANGER_GITHUB_API_TOKEN` environment variable, to do this, go to your build definition’s variables tab.

Make sure that ‘DANGER_GITHUB_API_TOKEN` is not set to secret since vsts does not expose secret variables while building.

#### VSTS

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](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.

Make sure that ‘DANGER_VSTS_API_TOKEN` is not set to secret since vsts does not expose secret variables while building.

Instance Attribute Summary

Attributes inherited from CI

#pull_request_id, #repo_slug, #repo_url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CI

available_ci_sources, inherited, #supports?

Constructor Details

#initialize(env) ⇒ VSTS

Returns a new instance of VSTS.



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/danger/ci_source/vsts.rb', line 60

def initialize(env)
  case env["BUILD_REPOSITORY_PROVIDER"]
  when "GitHub"
    self.repo_slug = self.class.github_slug(env)
  when "TfsGit"
    self.repo_slug = self.class.vsts_slug(env)
  end

  repo_matches = env["BUILD_SOURCEBRANCH"].match(%r{refs\/pull\/([0-9]+)\/merge})
  self.pull_request_id = repo_matches[1] unless repo_matches.nil?
  self.repo_url = env["BUILD_REPOSITORY_URI"]
end

Class Method Details

.github_slug(env) ⇒ Object



30
31
32
# File 'lib/danger/ci_source/vsts.rb', line 30

def github_slug(env)
  env["BUILD_REPOSITORY_NAME"]
end

.validates_as_ci?(env) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/danger/ci_source/vsts.rb', line 42

def self.validates_as_ci?(env)
  has_all_variables = ["SYSTEM_TEAMFOUNDATIONCOLLECTIONURI", "BUILD_REPOSITORY_PROVIDER"].all? { |x| env[x] && !env[x].empty? }

  is_support_source_control = ["GitHub", "TfsGit"].include?(env["BUILD_REPOSITORY_PROVIDER"])

  has_all_variables && is_support_source_control
end

.validates_as_pr?(env) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/danger/ci_source/vsts.rb', line 50

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

  has_all_variables && env["BUILD_REASON"] == "PullRequest"
end

.vsts_slug(env) ⇒ Object



34
35
36
37
38
39
# File 'lib/danger/ci_source/vsts.rb', line 34

def vsts_slug(env)
  project_name = env["SYSTEM_TEAMPROJECT"]
  repo_name = env["BUILD_REPOSITORY_NAME"]

  "#{project_name}/#{repo_name}"
end

Instance Method Details

#supported_request_sourcesObject



56
57
58
# File 'lib/danger/ci_source/vsts.rb', line 56

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