Class: Migrator::Converter::TracToGithub

Inherits:
Object
  • Object
show all
Defined in:
lib/tractive/migrator/converter/trac_to_github.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ TracToGithub

Returns a new instance of TracToGithub.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tractive/migrator/converter/trac_to_github.rb', line 8

def initialize(args)
  @comments_map = {}

  @trac_ticket_base_url = args[:cfg]["trac"]["ticketbaseurl"]
  @attachurl            = args[:opts][:attachurl] || args[:cfg].dig("ticket", "attachments", "url")
  @changeset_base_url   = args[:cfg]["trac"]["changeset_base_url"] || ""
  @singlepost           = args[:opts][:singlepost]
  @labels_cfg           = args[:cfg]["labels"].transform_values(&:to_h)
  @milestonesfromtrac   = args[:cfg]["milestones"]
  @users                = args[:cfg]["users"].to_h
  @trac_mails_cache     = {}
  @repo                 = args[:cfg]["github"]["repo"]
  @client               = GithubApi::Client.new(access_token: args[:cfg]["github"]["token"])
  @wiki_attachments_url = args[:cfg].dig("wiki", "attachments", "url")
  @revmap_file_path     = args[:opts][:revmapfile] || args[:cfg]["revmap_path"]
  @make_owners_label    = args[:opts]["make-owners-labels"] || args[:cfg]["make_owners_labels"]
  @attachment_options   = {
    url: @attachurl,
    hashed: args[:cfg].dig("ticket", "attachments", "hashed")
  }

  load_milestone_map
  create_labels_on_github(@labels_cfg["severity"].values)
  create_labels_on_github(@labels_cfg["priority"].values)
  create_labels_on_github(@labels_cfg["tracstate"].values)
  create_labels_on_github(@labels_cfg["component"].values)

  @uri_parser = URI::Parser.new
  @twf_to_markdown = Migrator::Converter::TwfToMarkdown.new(
    @trac_ticket_base_url,
    @attachment_options,
    @changeset_base_url,
    @wiki_attachments_url,
    @revmap_file_path,
    git_repo: @repo, home_page_name: args[:opts]["home-page-name"]
  )
end

Instance Attribute Details

#comments_mapObject (readonly)

Returns the value of attribute comments_map.



6
7
8
# File 'lib/tractive/migrator/converter/trac_to_github.rb', line 6

def comments_map
  @comments_map
end

Instance Method Details

#compose(ticket) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/tractive/migrator/converter/trac_to_github.rb', line 46

def compose(ticket)
  body = ""
  closed_time = nil

  # summary line:
  # body += %i[id component priority resolution].map do |cat|
  #   ticket[cat] and !ticket[cat].to_s.lstrip.empty? and
  #     "**#{cat}:** #{ticket[cat]}"
  # end.select { |x| x }.join(" | ")

  # Initial report
  # TODO: respect ticket[:changetime]
  body += "\n\n" unless @singlepost
  body += ticket_change(@singlepost, {
                          ticket: ticket[:id],
                          time: ticket[:time],
                          author: ticket[:reporter],
                          assigne: ticket[:owner],
                          field: :initial,
                          oldvalue: nil,
                          newvalue: ticket[:description]
                        })["body"]

  changes = if ticket.is_a? Hash
              []
            else
              ticket.all_changes
            end

  # replay all changes in chronological order:
  comments = changes.map { |x| ticket_change(@singlepost, x) }.select { |x| x }.to_a

  index = 0
  curr_index = 0
  changes.each do |change|
    kind = change[:field] || "attachment"

    next if %w[cc reporter version].include?(kind)

    if kind == "comment" && (change[:newvalue].nil? || change[:newvalue].lstrip.empty?)
      curr_index += 1
      next
    end

    if kind == "comment"
      @comments_map[curr_index] = index
      curr_index += 1
    end

    index += 1
  end

  if @singlepost
    body += comments.map { |x| x["body"] }.join("\n")
    comments = []
  end

  labels = Set[]
  changes.each do |x|
    del = @labels_cfg.fetch(x[:field], {})[x[:oldvalue]]
    # add = @labels_cfg.fetch(x[:field], {})[x[:newvalue]]
    @labels_cfg.fetch(x[:field], {})[x[:newvalue]]
    labels.delete(del) if del
    # labels.add(add) if add
    closed_time = x[:time] if x[:field] == "status" && x[:newvalue] == "closed"
  end

  # we separate labels from badges
  # labels: are changed frequently in the lifecycle of a ticket, therefore are transferred to github lables
  # badges: are basically fixed  and are transferred to a metadata table in the ticket

  badges = Set[]

  badges.add(@labels_cfg.fetch("type", {})[ticket[:type]])
  badges.add(@labels_cfg.fetch("resolution", {})[ticket[:resolution]])
  badges.add(@labels_cfg.fetch("version", {})[ticket[:version]])

  labels.add(@labels_cfg.fetch("severity", {})[ticket[:severity]])
  labels.add(@labels_cfg.fetch("priority", {})[ticket[:priority]])
  labels.add(@labels_cfg.fetch("tracstate", {})[ticket[:status]])
  labels.add(@labels_cfg.fetch("component", {})[ticket[:component]])

  labels.delete(nil)

  keywords = ticket[:keywords]&.split(",") || []
  keywords.each do |keyword|
    badges.add(@labels_cfg.fetch("keywords", {})[keyword.strip.gsub(" ", "_")])
  end

  # If the field is not set, it will be nil and generate an unprocessable json

  milestone = @milestonemap[ticket[:milestone]]

  # compute footer
  footer = "_Issue migrated from #{trac_ticket_link(ticket)} at #{Time.now}_"

  # compute badgetabe
  #

  github_assignee = map_assignee(ticket[:owner])

  unless github_assignee.nil? || github_assignee.empty?
    if @make_owners_label
      labels.add("name" => "owner:#{github_assignee}")
    else
      badges.add("owner:#{github_assignee}")
    end
  end

  badges     = badges.to_a.compact.sort
  badgetable = badges.map { |i| %(`#{i}`) }.join(" ")
  badgetable += begin
    "   |    by #{trac_mail(ticket[:reporter])}"
  rescue StandardError
    "deleted Ticket"
  end
  # badgetable += "   |   **->#{ticket[:owner]}**"  # note that from github to gitlab we loose the assigne

  # compose body
  body = [badgetable, body, footer].join("\n\n___\n")
  labels = labels.map { |label| label["name"] }

  issue = {
    "title" => ticket[:summary],
    "body" => body,
    "labels" => labels.to_a,
    "closed" => ticket[:status] == "closed",
    "created_at" => format_time(ticket[:time]),
    "milestone" => milestone
  }

  if @users.key?(ticket[:owner])
    owner = trac_mail(ticket[:owner])
    github_owner = extract_github_owner_username(owner)
    $logger.debug("..owner in trac: #{owner}")
    $logger.debug("..assignee in GitHub: #{github_owner}")
    issue["assignee"] = github_owner
  end

  ### as the assignee stuff is pretty fragile, we do not assign at all
  # issue['assignee'] = github_assignee if github_assignee

  if ticket[:changetime]
    # issue["updated_at"] = format_time(ticket[:changetime])
  end

  issue["closed_at"] = format_time(closed_time || ticket[:closed_at].to_i) if issue["closed"]

  {
    "issue" => issue,
    "comments" => comments
  }
end