Class: Octopolo::GitHub::IssueCreator

Inherits:
Object
  • Object
show all
Includes:
ConfigWrapper
Defined in:
lib/octopolo/github/issue_creator.rb

Direct Known Subclasses

PullRequestCreator

Constant Summary collapse

MissingAttribute =
Class.new(StandardError)
NotYetCreated =
Class.new(StandardError)
CannotCreate =
Class.new(StandardError)

Instance Attribute Summary collapse

Attributes included from ConfigWrapper

#config

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_name, options) ⇒ IssueCreator

Public: Create a issue for the given repo with the given options

repo_name - Full name (“account/repo”) of the repo in question options - Hash of issue information

title: Title of the issue


20
21
22
23
# File 'lib/octopolo/github/issue_creator.rb', line 20

def initialize repo_name, options
  self.repo_name = repo_name
  self.options = options
end

Instance Attribute Details

#dataObject

Public: The created resource’s details



53
54
55
# File 'lib/octopolo/github/issue_creator.rb', line 53

def data
  @data
end

#numberObject

Public: The created issue’s number



12
13
14
# File 'lib/octopolo/github/issue_creator.rb', line 12

def number
  @number
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/octopolo/github/issue_creator.rb', line 10

def options
  @options
end

#repo_nameObject

for instantiating the issue creator



9
10
11
# File 'lib/octopolo/github/issue_creator.rb', line 9

def repo_name
  @repo_name
end

Class Method Details

.perform(repo_name, options) ⇒ Object

Public: Create a issue for the given repo with the given options

repo_name - Full name (“account/repo”) of the repo in question options - Hash of issue information

title: Title of the issue

Returns the IssueCreator instance



32
33
34
35
36
# File 'lib/octopolo/github/issue_creator.rb', line 32

def self.perform repo_name, options
  new(repo_name, options).tap do |creator|
    creator.perform
  end
end

Instance Method Details

#bodyObject

Public: The body (primary copy) of the issue

Returns a String



108
109
110
111
112
# File 'lib/octopolo/github/issue_creator.rb', line 108

def body
  output = Renderer.render renderer_template, body_locals
  output = edit_body(output) if options[:editor]
  output
end

#body_edit_temp_nameObject

Public: Temporary file for body editing

Returns Name of temporary file



100
101
102
# File 'lib/octopolo/github/issue_creator.rb', line 100

def body_edit_temp_name
  'octopolo_issue'
end

#body_localsObject

Public: The local variables to pass into the template



134
135
136
137
138
139
140
# File 'lib/octopolo/github/issue_creator.rb', line 134

def body_locals
  {
    pivotal_ids: pivotal_ids,
    jira_ids: jira_ids,
    jira_url: jira_url,
  }
end

#edit_body(body) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/octopolo/github/issue_creator.rb', line 114

def edit_body(body)
  return body unless ENV['EDITOR']

  # Open the file, write the contents, and close it
  tempfile = Tempfile.new([body_edit_temp_name, '.md'])
  tempfile.write(body)
  tempfile.close

  # Allow the user to edit the file
  system "#{ENV['EDITOR']} #{tempfile.path}"

  # Reopen the file, read the contents, and delete it
  tempfile.open
  output = tempfile.read
  tempfile.unlink

  output
end

#jira_idsObject

Public: Jira Issue IDs associated with the issue

Returns an Array of Strings



79
80
81
# File 'lib/octopolo/github/issue_creator.rb', line 79

def jira_ids
  options[:jira_ids] || []
end

#jira_urlObject

Public: Jira Url associated with the issue

Returns Jira Url



86
87
88
# File 'lib/octopolo/github/issue_creator.rb', line 86

def jira_url
  config.jira_url
end

#performObject

Public: Create the issue

Returns an array with the first element being the issue’s number, the second being a Mash of the response from GitHub’s API



42
43
44
45
46
47
48
49
50
# File 'lib/octopolo/github/issue_creator.rb', line 42

def perform
  # labels option cannot be null due to https://github.com/octokit/octokit.rb/pull/538
  result = GitHub.create_issue(repo_name, title, body, labels: [])
  # capture the information
  self.number = result.number
  self.data = result
rescue => e
  raise CannotCreate, e.message
end

#pivotal_idsObject

Public: The Pivotal Tracker story IDs associated with the issue

Returns an Array of Strings



72
73
74
# File 'lib/octopolo/github/issue_creator.rb', line 72

def pivotal_ids
  options[:pivotal_ids] || []
end

#renderer_templateObject

Public: Rendering template for body property

Returns Name of template file



93
94
95
# File 'lib/octopolo/github/issue_creator.rb', line 93

def renderer_template
  Renderer::ISSUE_BODY
end

#titleObject

Public: Title of the issue

Returns a String with the title



65
66
67
# File 'lib/octopolo/github/issue_creator.rb', line 65

def title
  options[:title] || raise(MissingAttribute)
end