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



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

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

Instance Method Details

Add reference to Confluence page from current issue



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

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



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

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



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

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



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

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)


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

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.



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

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