Class: Lazylead::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/lazylead/confluence.rb

Overview

The reference from Jira issue to Confluence system.

Instance Method Summary collapse

Constructor Details

#initialize(issue, sys, cnfl) ⇒ Link

:issue

The issue from external system

:sys

The jira ticketing system

:cnfl

The Confluence instances details (host, appId) which expected within the comments



89
90
91
92
93
# File 'lib/lazylead/confluence.rb', line 89

def initialize(issue, sys, cnfl)
  @issue = issue
  @sys = sys
  @confl = cnfl
end

Instance Method Details

Add reference to Confluence page from current issue



156
157
158
159
160
161
# File 'lib/lazylead/confluence.rb', line 156

def add_link
  @diff.each do |url|
    cnf = @confl.find { |c| url.start_with? c.url }
    @issue.remotelink.build.save cnf.make_link(url)
  end
end

Detect existing links from the ticket



144
145
146
147
148
149
# File 'lib/lazylead/confluence.rb', line 144

def existing_links(ticket, type = "Wiki Page")
  ticket.remotelink
        .all
        .select { |l| l.attrs["relationship"] == type }
        .map { |e| e.attrs["object"]["url"] }
end

Fetch ticket links from comments



96
97
98
99
100
101
102
103
# File 'lib/lazylead/confluence.rb', line 96

def fetch_links
  ticket = @sys.raw do |conn|
    conn.Issue.find(@issue.id, expand: "comments,changelog", fields: "")
  end
  @mentioned = mentioned_links(ticket)
  @existing = existing_links(ticket)
  @diff = @mentioned.reject { |url| @existing.include? url }
end
TODO:

#/DEV Refactor this method in order to make it more human-readable and remove the suppresion below

Detect links mentioned in ticket comments rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



111
112
113
114
115
116
117
118
119
120
# File 'lib/lazylead/confluence.rb', line 111

def mentioned_links(ticket)
  ticket.comments
        .map { |cmnt| cmnt.attrs["body"] }
        .select { |cmnt| @confl.any? { |c| cmnt.include? c.url } }
        .flat_map(&:split)
        .select { |cmnt| @confl.any? { |c| cmnt.start_with? c.url } }
        .map { |url| to_page_id(url) }
        .reject(&:blank?)
        .uniq
end

#need_link?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/lazylead/confluence.rb', line 151

def need_link?
  !@diff.empty?
end

#to_page_id(url) ⇒ Object

Convert confluence page url to the following format:

http://confluence.com/pages/viewpage.action?pageId=xxxxx

Sometimes links to confluence pages have the following formats:

http://confluence.com/pages/spaceKey=THESPACE&title=THETITLE
http://confluence.com/display/THESPACE/THETITLE

and can’t be linked though the “remote link” Jira functionality.



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/lazylead/confluence.rb', line 131

def to_page_id(url)
  return url if url.include? "pageId="
  if url.include? "&title="
    space = url[/spaceKey=(?<space>[A-Z0-9+.]+)/, 1]
    title = url[/&title=(?<title>[A-Za-z0-9+.]+)/, 1]
  else
    space, title = url.split("/").last(2)
  end
  @confl.find { |c| url.start_with? c.url }
        .fetch_page_id(space, title.tr("+", " "))
end