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



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

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



138
139
140
141
142
143
# File 'lib/lazylead/confluence.rb', line 138

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

Detect links mentioned in ticket comments



107
108
109
110
111
112
113
114
115
116
# File 'lib/lazylead/confluence.rb', line 107

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

#need_link?Boolean

Returns:

  • (Boolean)


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

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.



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/lazylead/confluence.rb', line 125

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