Class: Danger::TeamCity

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

Overview

CI Setup

You need to go to your project settings. Then depending on the type of your build settings, you may need to add a new build step for Danger. You want to be able to run the command bundle exec danger, so the "Simple Command Runner" should be all you need to do that.

Token + Environment Setup

GitHub

As this is self-hosted, you will need to add the DANGER_GITHUB_API_TOKEN to your build user's ENV. The alternative is to pass in the token as a prefix to the command DANGER_GITHUB_API_TOKEN="123" bundle exec danger.

However, you will need to find a way to add the environment vars: GITHUB_REPO_SLUG, GITHUB_PULL_REQUEST_ID and GITHUB_REPO_URL. These are not added by default. You can manually add GITHUB_REPO_SLUG and GITHUB_REPO_URL as build parameters or by exporting them inside your Simple Command Runner.

As for GITHUB_PULL_REQUEST_ID, TeamCity provides the %teamcity.build.branch% variable which is in the format PR_NUMBER/merge. You can slice the Pull Request ID out by doing the following:

branch="%teamcity.build.branch%"
export GITHUB_PULL_REQUEST_ID=(${branch//\// })

Or if you are using the pull request feature you can set an environment parameter called GITHUB_PULL_REQUEST_ID to the value of: %teamcity.pullRequest.number

GitLab

As this is self-hosted, you will need to add the DANGER_GITLAB_API_TOKEN to your build user's ENV. The alternative is to pass in the token as a prefix to the command DANGER_GITLAB_API_TOKEN="123" bundle exec danger.

However, you will need to find a way to add the environment vars: GITLAB_REPO_SLUG, GITLAB_PULL_REQUEST_ID and GITLAB_REPO_URL. These are not added by default. You could do this via the GitLab API potentially.

We would love some advice on improving this setup.

BitBucket Cloud

You will need to add the following environment variables as build parameters or by exporting them inside your Simple Command Runner.

  • BITBUCKET_REPO_SLUG

  • BITBUCKET_REPO_URL

  • DANGER_BITBUCKETCLOUD_USERNAME

  • DANGER_BITBUCKETCLOUD_PASSWORD

or

  • DANGER_BITBUCKETCLOUD_OAUTH_KEY
  • DANGER_BITBUCKETCLOUD_OAUTH_SECRET

You will also need to set the BITBUCKET_BRANCH_NAME environment variable. TeamCity provides %teamcity.build.branch%, which you can use at the top of your Simple Command Runner:

export BITBUCKET_BRANCH_NAME="%teamcity.build.branch%"

Optionally, you can set BITBUCKET_PULL_REQUEST_ID to skip the API call that resolves the pull request ID from the branch name. If you are using the TeamCity Pull Requests build feature:

export BITBUCKET_PULL_REQUEST_ID="%teamcity.pullRequest.number%"

BitBucket Server

You will need to add the following environment variables as build parameters or by exporting them inside your Simple Command Runner.

  • DANGER_BITBUCKETSERVER_USERNAME
  • DANGER_BITBUCKETSERVER_PASSWORD
  • DANGER_BITBUCKETSERVER_HOST
  • BITBUCKETSERVER_REPO_SLUG
  • BITBUCKETSERVER_PULL_REQUEST_ID
  • BITBUCKETSERVER_REPO_URL

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) ⇒ TeamCity

Returns a new instance of TeamCity.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/danger/ci_source/teamcity.rb', line 121

def initialize(env)
  # NB: Unfortunately TeamCity doesn't provide these variables
  # automatically so you have to add these variables manually to your
  # project or build configuration
  if self.class.validates_as_github_pr?(env)
    extract_github_variables!(env)
  elsif self.class.validates_as_gitlab_pr?(env)
    extract_gitlab_variables!(env)
  elsif self.class.validates_as_bitbucket_cloud_pr?(env)
    extract_bitbucket_variables!(env)
  elsif self.class.validates_as_bitbucket_server_pr?(env)
    extract_bitbucket_server_variables!(env)
  end
end

Class Method Details

.validates_as_bitbucket_cloud_pr?(env) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/danger/ci_source/teamcity.rb', line 100

def validates_as_bitbucket_cloud_pr?(env)
  ["BITBUCKET_REPO_SLUG", "BITBUCKET_BRANCH_NAME", "BITBUCKET_REPO_URL"].all? { |x| env[x] && !env[x].empty? }
end

.validates_as_bitbucket_server_pr?(env) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/danger/ci_source/teamcity.rb', line 104

def validates_as_bitbucket_server_pr?(env)
  ["BITBUCKETSERVER_REPO_SLUG", "BITBUCKETSERVER_PULL_REQUEST_ID", "BITBUCKETSERVER_REPO_URL"].all? { |x| env[x] && !env[x].empty? }
end

.validates_as_ci?(env) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/danger/ci_source/teamcity.rb', line 109

def self.validates_as_ci?(env)
  env.key? "TEAMCITY_VERSION"
end

.validates_as_github_pr?(env) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/danger/ci_source/teamcity.rb', line 92

def validates_as_github_pr?(env)
  ["GITHUB_PULL_REQUEST_ID", "GITHUB_REPO_URL"].all? { |x| env[x] && !env[x].empty? }
end

.validates_as_gitlab_pr?(env) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/danger/ci_source/teamcity.rb', line 96

def validates_as_gitlab_pr?(env)
  ["GITLAB_REPO_SLUG", "GITLAB_PULL_REQUEST_ID", "GITLAB_REPO_URL"].all? { |x| env[x] && !env[x].empty? }
end

.validates_as_pr?(env) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/danger/ci_source/teamcity.rb', line 113

def self.validates_as_pr?(env)
  validates_as_github_pr?(env) || validates_as_gitlab_pr?(env) || validates_as_bitbucket_cloud_pr?(env) || validates_as_bitbucket_server_pr?(env)
end

Instance Method Details

#supported_request_sourcesObject



117
118
119
# File 'lib/danger/ci_source/teamcity.rb', line 117

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