Class: Danger::DangerJiraSync

Inherits:
Plugin
  • Object
show all
Defined in:
lib/jira_sync/plugin.rb

Overview

Jira and GitHub should be friends, and Danger brings them closer together with jira_sync

Examples:

You must always configure jira_sync before it can access the Jira

REST API

      jira_sync.configure(
        jira_url: "https://myjirainstance.atlassian.net",
        jira_username: "[email protected]",
        jira_api_token: "ABC123",
      )

Automatically label Pull Requests with the associated Jira issue’s

component names and project key

      jira_sync.autolabel_pull_request(%w(DEV))

See Also:

  • roverdotcom/danger-jira_sync

Defined Under Namespace

Classes: NotConfiguredError

Instance Method Summary collapse

Instance Method Details

#autolabel_pull_request(issue_prefixes) ⇒ Array<String>?

Labels the Pull Request with Jira Project Keys and Component Names

Parameters:

  • issue_prefixes (Array<String>)

    An array of issue key prefixes; this is often the project key. These must be present in the title or body of the Pull Request

Returns:

  • (Array<String>, nil)

    The list of project & component labels that were applied or nil if no issue or labels were found

Raises:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jira_sync/plugin.rb', line 68

def autolabel_pull_request(issue_prefixes)
  raise NotConfiguredError unless @jira_client
  raise(ArgumentError, "issue_prefixes cannot be empty") if issue_prefixes.empty?

  issue_keys = extract_issue_keys_from_pull_request(issue_prefixes)
  return if issue_keys.empty?

  labels = fetch_labels_from_issues(issue_keys)
  return if labels.empty?

  create_missing_github_labels(labels)
  add_labels_to_issue(labels)

  labels
end

#configure(jira_url:, jira_username:, jira_api_token:) ⇒ JIRA::Client

Configures the Jira REST Client with your credentials

Parameters:

  • jira_url (String)

    The full url to your Jira instance, e.g., “myjirainstance.atlassian.net

  • jira_username (String)

    The username to use for accessing the Jira instance. Commonly, this is an email address.

  • jira_api_token (String)

    The API key to use to access the Jira instance. Generate one here: id.atlassian.com/manage/api-tokens

Returns:

  • (JIRA::Client)

    The underlying jira-ruby JIRA::Client instance



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jira_sync/plugin.rb', line 45

def configure(jira_url:, jira_username:, jira_api_token:)
  warn "danger-jira_sync plugin configuration is missing jira_url" if jira_url.blank?
  warn "danger-jira_sync plugin configuration is missing jira_username" if jira_username.blank?
  warn "danger-jira_sync plugin configuration is missing jira_api_token" if jira_api_token.blank?

  @jira_client = JIRA::Client.new(
    site: jira_url,
    username: jira_username,
    password: jira_api_token,
    context_path: "",
    auth_type: :basic
  )
end