Module: ProblemChild::Helpers
- Included in:
- App
- Defined in:
- lib/problem_child/helpers.rb
Instance Method Summary collapse
- #anonymous_submissions? ⇒ Boolean
- #auth! ⇒ Object
-
#base_sha ⇒ Object
Head SHA of default branch, used for creating new branches.
- #branch_exists?(branch) ⇒ Boolean
-
#branches ⇒ Object
Returns array of Octokit branch objects.
- #client ⇒ Object
-
#create_branch(branch) ⇒ Object
Create a branch with the given name, based off of HEAD of the defautl branch.
- #create_issue ⇒ Object
-
#create_pull_request ⇒ Object
Create a pull request with the form contents.
-
#form_data ⇒ Object
abstraction to allow cached form data to be used in place of default params.
- #issue_body ⇒ Object
- #issue_title ⇒ Object
- #labels ⇒ Object
-
#patch_branch ⇒ Object
Name of branch to submit pull request from Starts with patch-1 and keeps going until it finds one not taken.
- #render_template(template, locals = {}) ⇒ Object
- #repo ⇒ Object
- #repo_access? ⇒ Boolean
- #token ⇒ Object
- #uploads ⇒ Object
Instance Method Details
#anonymous_submissions? ⇒ Boolean
7 8 9 |
# File 'lib/problem_child/helpers.rb', line 7 def anonymous_submissions? !ENV['GITHUB_TOKEN'].to_s.empty? end |
#auth! ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/problem_child/helpers.rb', line 116 def auth! if anonymous_submissions? true elsif ENV['GITHUB_TEAM_ID'] github_team_authenticate!(ENV['GITHUB_TEAM_ID']) elsif ENV['GITHUB_ORG_ID'] github_organization_authenticate!(ENV['GITHUB_ORG_ID']) else raise 'Must define GITHUB_TEAM_ID, GITHUB_ORG_ID, OR GITHUB_TOKEN' end end |
#base_sha ⇒ Object
Head SHA of default branch, used for creating new branches
61 62 63 64 |
# File 'lib/problem_child/helpers.rb', line 61 def base_sha default_branch = client.repo(repo)[:default_branch] branches.find { |branch| branch[:name] == default_branch }[:commit][:sha] end |
#branch_exists?(branch) ⇒ Boolean
66 67 68 |
# File 'lib/problem_child/helpers.rb', line 66 def branch_exists?(branch) branches.any? { |b| b.name == branch } end |
#branches ⇒ Object
Returns array of Octokit branch objects
56 57 58 |
# File 'lib/problem_child/helpers.rb', line 56 def branches client.branches(repo) end |
#client ⇒ Object
19 20 21 |
# File 'lib/problem_child/helpers.rb', line 19 def client @client ||= Octokit::Client.new access_token: token end |
#create_branch(branch) ⇒ Object
Create a branch with the given name, based off of HEAD of the defautl branch
85 86 87 |
# File 'lib/problem_child/helpers.rb', line 85 def create_branch(branch) client.create_ref repo, "heads/#{branch}", base_sha end |
#create_issue ⇒ Object
50 51 52 53 |
# File 'lib/problem_child/helpers.rb', line 50 def create_issue issue = client.create_issue(repo, form_data['title'], issue_body, labels: labels) issue['number'] if issue end |
#create_pull_request ⇒ Object
Create a pull request with the form contents
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/problem_child/helpers.rb', line 90 def create_pull_request unless uploads.empty? branch = patch_branch create_branch(branch) uploads.each do |key, upload| client.create_contents( repo, upload[:filename], "Create #{upload[:filename]}", branch: branch, file: upload[:tempfile] ) session["file_#{key}"] = nil end end pr = client.create_pull_request(repo, 'master', branch, form_data['title'], issue_body, labels: labels) pr['number'] if pr end |
#form_data ⇒ Object
abstraction to allow cached form data to be used in place of default params
36 37 38 |
# File 'lib/problem_child/helpers.rb', line 36 def form_data session['form_data'].nil? ? params : JSON.parse(session['form_data']) end |
#issue_body ⇒ Object
31 32 33 |
# File 'lib/problem_child/helpers.rb', line 31 def issue_body form_data.reject { |key, value| key == 'title' || value.empty? || key == 'labels' || value.is_a?(Hash) }.map { |key, value| "* **#{key.humanize}**: #{value}" }.join("\n") end |
#issue_title ⇒ Object
27 28 29 |
# File 'lib/problem_child/helpers.rb', line 27 def issue_title form_data['title'] end |
#labels ⇒ Object
40 41 42 |
# File 'lib/problem_child/helpers.rb', line 40 def labels form_data['labels'].join(',') if form_data['labels'] end |
#patch_branch ⇒ Object
Name of branch to submit pull request from Starts with patch-1 and keeps going until it finds one not taken
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/problem_child/helpers.rb', line 72 def patch_branch num = 1 branch_name = form_data['title'].parameterize return branch_name unless branch_exists?(branch_name) branch = "#{branch_name}-#{num}" while branch_exists?(branch) num += 1 branch = "#{branch_name}-#{num}" end branch end |
#render_template(template, locals = {}) ⇒ Object
23 24 25 |
# File 'lib/problem_child/helpers.rb', line 23 def render_template(template, locals = {}) halt erb template, layout: :layout, locals: locals.merge(template: template) end |
#repo ⇒ Object
3 4 5 |
# File 'lib/problem_child/helpers.rb', line 3 def repo ENV['GITHUB_REPO'] end |
#repo_access? ⇒ Boolean
109 110 111 112 113 114 |
# File 'lib/problem_child/helpers.rb', line 109 def repo_access? return true unless anonymous_submissions? !client.repository(repo)['private'] rescue false end |
#token ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/problem_child/helpers.rb', line 11 def token if anonymous_submissions? ENV['GITHUB_TOKEN'] elsif !github_user.nil? github_user.token end end |
#uploads ⇒ Object
44 45 46 47 48 |
# File 'lib/problem_child/helpers.rb', line 44 def uploads form_data.select do |_key, value| value.is_a?(Hash) && value.key?(:tempfile) && value[:tempfile].is_a?(Tempfile) end end |