Class: Rdv::Issues

Inherits:
Object
  • Object
show all
Defined in:
lib/rdv/issues.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, repo) ⇒ Issues

Returns a new instance of Issues.



5
6
7
8
# File 'lib/rdv/issues.rb', line 5

def initialize client, repo
  self.client = client
  self.repo = repo
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/rdv/issues.rb', line 3

def client
  @client
end

#repoObject

Returns the value of attribute repo.



3
4
5
# File 'lib/rdv/issues.rb', line 3

def repo
  @repo
end

Class Method Details

.create_from(client, repo, themes) ⇒ Object



66
67
68
# File 'lib/rdv/issues.rb', line 66

def self.create_from client, repo, themes
  new(client, repo).create_issues(themes)
end

Instance Method Details

#create_issue(issue) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rdv/issues.rb', line 25

def create_issue issue
  # Serialize the issue to compare it to existing ones
  serialized = serialize_issue(issue.name, issue.tags)
  # If issue already exist, don't create it
  if (existing = existing_issues[serialized])
    puts ("Issue \"#{ issue.name }\" not created, already exists " +
      "as ##{ existing.number } / #{ existing.html_url }").red
    return
  end
  # Create issue in repo
  client.create_issue(repo, issue.name, issue.content, {
    labels: issue.tags, assignee: issue.user
  })
rescue Octokit::UnprocessableEntity => e
  data = e.response_body
  # Tell that issue couldn't be created
  puts "Couldn't create issue \"#{ issue.name }\", #{ data.message }.".red
  # Display each field with errors
  data.errors.each do |error|
    puts "- #{ error.code } #{ error.field } #{ error.value }".red
  end
  # Ensure nil is returned so compacting issues array will remove it
  # from created ones
  nil
end

#create_issues(themes) ⇒ Object

Creates issues from the current themes



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rdv/issues.rb', line 11

def create_issues themes
  issues = themes.reduce([]) do |issues, theme|
    main_label = theme.name
    issues + theme.list.map do |issue|
      # Add theme as the first issue's tag
      issue.tags.unshift(main_label)
      # Create issue
      create_issue(issue)
    end
  end

  issues.compact
end

#existing_issuesObject

Retrieves all existing issues for the current repo and stores them in a hash with the key being some kind of unique identifier



54
55
56
57
58
59
60
# File 'lib/rdv/issues.rb', line 54

def existing_issues
  @existing_issues ||=
    client.list_issues(repo).reduce({}) do |hash, issue|
      hash[serialize_issue(issue.title, issue.labels.map(&:name))] = issue
      hash
    end
end

#serialize_issue(title, labels) ⇒ Object



62
63
64
# File 'lib/rdv/issues.rb', line 62

def serialize_issue title, labels
  "#{ title }-#{ labels.sort.join(":") }"
end