Module: ProblemChild::Helpers

Included in:
App
Defined in:
lib/problem_child/helpers.rb

Instance Method Summary collapse

Instance Method Details

#anonymous_submissions?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/problem_child/helpers.rb', line 8

def anonymous_submissions?
  !!(ENV["GITHUB_TOKEN"] && !ENV["GITHUB_TOKEN"].to_s.empty?)
end

#auth!Object



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/problem_child/helpers.rb', line 124

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"
    halt 401
  end
end

#base_shaObject

Head SHA of default branch, used for creating new branches



62
63
64
65
# File 'lib/problem_child/helpers.rb', line 62

def base_sha
  default_branch = client.repo(repo)[:default_branch]
  branches.find { |branch| branch[:name] == default_branch }[:commit][:sha]
end

#branch_exists?(branch) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/problem_child/helpers.rb', line 67

def branch_exists?(branch)
  branches.any? { |b| b.name == branch }
end

#branchesObject

Returns array of Octokit branch objects



57
58
59
# File 'lib/problem_child/helpers.rb', line 57

def branches
  client.branches(repo)
end

#cache_form_dataObject



117
118
119
120
121
122
# File 'lib/problem_child/helpers.rb', line 117

def cache_form_data
  uploads.each do |key, upload|
    session["file_#{key}"] = File.open(upload[:tempfile]).read
  end
  session[:form_data] = params.to_json
end

#clientObject



20
21
22
# File 'lib/problem_child/helpers.rb', line 20

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



86
87
88
# File 'lib/problem_child/helpers.rb', line 86

def create_branch(branch)
  client.create_ref repo, "heads/#{branch}", base_sha
end

#create_issueObject



51
52
53
54
# File 'lib/problem_child/helpers.rb', line 51

def create_issue
  issue = client.create_issue(repo, form_data["title"], issue_body, :labels => labels)
  issue["number"] if issue
end

#create_pull_requestObject

Create a pull request with the form contents



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/problem_child/helpers.rb', line 91

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"]}",
        session["file_#{key}"],
        :branch => branch
      )
      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_dataObject

abstraction to allow cached form data to be used in place of default params



37
38
39
# File 'lib/problem_child/helpers.rb', line 37

def form_data
  session["form_data"].nil? ? params : JSON.parse(session["form_data"])
end

#issue_bodyObject



32
33
34
# File 'lib/problem_child/helpers.rb', line 32

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_titleObject



28
29
30
# File 'lib/problem_child/helpers.rb', line 28

def issue_title
  form_data["title"]
end

#labelsObject



41
42
43
# File 'lib/problem_child/helpers.rb', line 41

def labels
  form_data["labels"].join(",") if form_data["labels"]
end

#patch_branchObject

Name of branch to submit pull request from Starts with patch-1 and keeps going until it finds one not taken



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/problem_child/helpers.rb', line 73

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) do
    num = num + 1
    branch = "#{branch_name}-#{num}"
  end
  branch
end

#render_template(template, locals = {}) ⇒ Object



24
25
26
# File 'lib/problem_child/helpers.rb', line 24

def render_template(template, locals={})
  halt erb template, :layout => :layout, :locals => locals.merge({ :template => template })
end

#repoObject



4
5
6
# File 'lib/problem_child/helpers.rb', line 4

def repo
  ENV["GITHUB_REPO"]
end

#repo_access?Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
# File 'lib/problem_child/helpers.rb', line 110

def repo_access?
  return true unless anonymous_submissions?
  !client.repository(repo)["private"]
rescue
  false
end

#tokenObject



12
13
14
15
16
17
18
# File 'lib/problem_child/helpers.rb', line 12

def token
  if anonymous_submissions?
    ENV["GITHUB_TOKEN"]
  elsif !github_user.nil?
    github_user.token
  end
end

#uploadsObject



45
46
47
48
49
# File 'lib/problem_child/helpers.rb', line 45

def uploads
  form_data.select do |key, value|
    value.is_a?(Hash) && ( value.has_key?("filename") || value.has_key?(:filename) )
  end
end