module ProblemChild::Helpers
Public Instance Methods
anonymous_submissions?()
click to toggle source
# File lib/problem_child/helpers.rb, line 7 def anonymous_submissions? !ENV['GITHUB_TOKEN'].to_s.empty? end
auth!()
click to toggle source
# 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()
click to toggle source
Head SHA of default branch, used for creating new branches
# 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)
click to toggle source
# File lib/problem_child/helpers.rb, line 66 def branch_exists?(branch) branches.any? { |b| b.name == branch } end
branches()
click to toggle source
Returns array of Octokit branch objects
# File lib/problem_child/helpers.rb, line 56 def branches client.branches(repo) end
client()
click to toggle source
# File lib/problem_child/helpers.rb, line 19 def client @client ||= Octokit::Client.new access_token: token end
create_branch(branch)
click to toggle source
Create a branch with the given name, based off of HEAD of the defautl branch
# File lib/problem_child/helpers.rb, line 85 def create_branch(branch) client.create_ref repo, "heads/#{branch}", base_sha end
create_issue()
click to toggle source
# 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()
click to toggle source
Create a pull request with the form contents
# 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()
click to toggle source
abstraction to allow cached form data to be used in place of default params
# File lib/problem_child/helpers.rb, line 36 def form_data session['form_data'].nil? ? params : JSON.parse(session['form_data']) end
issue_body()
click to toggle source
# 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()
click to toggle source
# File lib/problem_child/helpers.rb, line 27 def issue_title form_data['title'] end
labels()
click to toggle source
# File lib/problem_child/helpers.rb, line 40 def labels form_data['labels'].join(',') if form_data['labels'] end
patch_branch()
click to toggle source
Name of branch to submit pull request from Starts with patch-1 and keeps going until it finds one not taken
# 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 = {})
click to toggle source
# 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()
click to toggle source
# File lib/problem_child/helpers.rb, line 3 def repo ENV['GITHUB_REPO'] end
repo_access?()
click to toggle source
# File lib/problem_child/helpers.rb, line 109 def repo_access? return true unless anonymous_submissions? !client.repository(repo)['private'] rescue false end
token()
click to toggle source
# 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()
click to toggle source
# 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