Class: Danger::DangerfileBitbucketServerPlugin

Inherits:
Plugin
  • Object
show all
Defined in:
lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb

Overview

Handles interacting with Bitbucket Server inside a Dangerfile. Provides a few functions which wrap ‘pr_json` and also through a few standard functions to simplify your code.

Examples:

Warn when a PR is classed as work in progress


warn "PR is classed as Work in Progress" if bitbucket_server.pr_title.include? "[WIP]"

Declare a PR to be simple to avoid specific Danger rules


declared_trivial = (bitbucket_server.pr_title + bitbucket_server.pr_body).include?("#trivial")

Ensure that labels have been used on the PR


failure "Please add labels to this PR" if bitbucket_server.pr_labels.empty?

Ensure there is a summary for a PR


failure "Please provide a summary in the Pull Request description" if bitbucket_server.pr_body.length < 5

Only accept PRs to the develop branch


failure "Please re-submit this PR to develop, we may have already fixed your issue." if bitbucket_server.branch_for_base != "develop"

Highlight when a celebrity makes a pull request


message "Welcome, Danger." if bitbucket_server.pr_author == "dangermcshane"

Ensure that all PRs have an assignee


warn "This PR does not have any assignees yet." if bitbucket_server.pr_json[:reviewers].length == 0

Send a message with links to a collection of specific files


if git.modified_files.include? "config/*.js"
  config_files = git.modified_files.select { |path| path.include? "config/" }
  message "This PR changes #{ bitbucket_server.html_link(config_files) }"
end

Highlight with a clickable link if a Package.json is changed


warn "#{bitbucket_server.html_link("Package.json")} was edited." if git.modified_files.include? "Package.json"

See Also:

  • danger/danger

Bitbucket Server Misc collapse

PR Metadata collapse

PR Commit Metadata collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

all_plugins, clear_external_plugins, inherited, #method_missing

Constructor Details

#initialize(dangerfile) ⇒ DangerfileBitbucketServerPlugin

Returns a new instance of DangerfileBitbucketServerPlugin.



66
67
68
69
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 66

def initialize(dangerfile)
  super(dangerfile)
  @bs = dangerfile.env.request_source
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Danger::Plugin

Class Method Details

.instance_nameString

The instance name used in the Dangerfile

Returns:



62
63
64
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 62

def self.instance_name
  "bitbucket_server"
end

.new(dangerfile) ⇒ Object

So that this init can fail.



53
54
55
56
57
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 53

def self.new(dangerfile)
  return nil if dangerfile.env.request_source.class != Danger::RequestSources::BitbucketServer

  super
end

Instance Method Details

#base_commitString

The base commit to which the PR is going to be merged as a parent.

Returns:



132
133
134
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 132

def base_commit
  @bs.pr_json[:toRef][:latestCommit].to_s
end

#branch_for_baseString

The branch to which the PR is going to be merged into.

Returns:



108
109
110
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 108

def branch_for_base
  @bs.pr_json[:toRef][:displayId].to_s
end

#branch_for_headString

The branch to which the PR is going to be merged from.

Returns:



124
125
126
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 124

def branch_for_head
  @bs.pr_json[:fromRef][:displayId].to_s
end

#head_commitString

The head commit to which the PR is requesting to be merged from.

Returns:



140
141
142
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 140

def head_commit
  @bs.pr_json[:fromRef][:latestCommit].to_s
end
Note:

Atlassian [disabled inline HTML support](jira.atlassian.com/browse/BSERV-7147). This method method left for backward compatibility.

Returns a list of Markdown links for a file, or files in the head repository. It returns a string of multiple anchors if passed an array.

Parameters:

  • paths (String or Array<String>)

    A list of strings to convert to github anchors

  • full_path (Bool) (defaults to: true)

    Shows the full path as the link’s text, defaults to ‘true`.

Returns:



156
157
158
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 156

def html_link(paths, full_path: true)
  markdown_link(paths, full_path: full_path)
end

Returns a list of Markdown links for a file, or files in the head repository. It returns a string of multiple links if passed an array.

Parameters:

  • paths (String or Array<String>)

    A list of strings to convert to Markdown links

  • full_path (Bool) (defaults to: true)

    Shows the full path as the link’s text, defaults to ‘true`.

Returns:



170
171
172
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 170

def markdown_link(paths, full_path: true)
  create_link(paths, full_path) { |href, text| create_markdown_link(href, text) }
end

#pr_authorString

The username of the author of the Pull Request.

Returns:



100
101
102
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 100

def pr_author
  @bs.pr_json[:author][:user][:slug].to_s
end

#pr_descriptionString Also known as: pr_body

The body text of the Pull Request.

Returns:



91
92
93
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 91

def pr_description
  @bs.pr_json[:description].to_s
end

#pr_jsonHash

The hash that represents the PR’s JSON. For an example of what this looks like see the [Danger Fixture’d one](raw.githubusercontent.com/danger/danger/master/spec/fixtures/bitbucket_server_api/pr_response.json).

Returns:

  • (Hash)


75
76
77
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 75

def pr_json
  @bs.pr_json
end

A href that represents the current PR

Returns:



116
117
118
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 116

def pr_link
  @bs.pr_json[:links][:self].flat_map { |l| l[:href] }.first.to_s
end

#pr_titleString

The title of the Pull Request.

Returns:



83
84
85
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 83

def pr_title
  @bs.pr_json[:title].to_s
end

#update_pr_build_status(status, build_job_link, description) ⇒ String

Updates the PR with build status and build server job link.

Parameters:

  • status (String)

    SUCCESSFUL, FAILED and INPROGRESS

  • build_job_link (String)

    Build server job link

  • description (String)

    Build status description

Returns:



184
185
186
# File 'lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb', line 184

def update_pr_build_status(status, build_job_link, description)
  @bs.update_pr_build_status(status, build_job_link, description)
end