|
| 1 | +require 'octokit' |
| 2 | + |
| 3 | +module ErrbitGithubPlugin |
| 4 | + class IssueTracker < ErrbitPlugin::IssueTracker |
| 5 | + |
| 6 | + def label; 'github'; end |
| 7 | + def note |
| 8 | + 'Please configure your github repository in the <strong>GITHUB REPO</strong> field above.<br/>' << |
| 9 | + 'Instead of providing your username & password, you can link your Github account ' << |
| 10 | + 'to your user profile, and allow Errbit to create issues using your OAuth token.' |
| 11 | + end |
| 12 | + |
| 13 | + def fields |
| 14 | + { |
| 15 | + :username => { |
| 16 | + :placeholder => "Your username on GitHub" |
| 17 | + }, |
| 18 | + :password => { |
| 19 | + :placeholder => "Password for your account" |
| 20 | + } |
| 21 | + } |
| 22 | + end |
| 23 | + |
| 24 | + attr_accessor :oauth_token |
| 25 | + attr_reader :url |
| 26 | + |
| 27 | + def configured? |
| 28 | + project_id.present? |
| 29 | + end |
| 30 | + |
| 31 | + def comments_allowed?; false; end |
| 32 | + |
| 33 | + def project_id |
| 34 | + app.github_repo |
| 35 | + end |
| 36 | + |
| 37 | + def check_params |
| 38 | + if Fields.detect {|f| self[f[0]].blank? } |
| 39 | + errors.add :base, 'You must specify your GitHub username and password' |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + def create_issue(problem, reported_by = nil) |
| 44 | + # Login using OAuth token, if given. |
| 45 | + if oauth_token |
| 46 | + client = Octokit::Client.new(:login => username, :oauth_token => oauth_token) |
| 47 | + else |
| 48 | + client = Octokit::Client.new(:login => username, :password => password) |
| 49 | + end |
| 50 | + |
| 51 | + begin |
| 52 | + issue = client.create_issue( |
| 53 | + project_id, |
| 54 | + issue_title(problem), |
| 55 | + body_template.result(binding).unpack('C*').pack('U*') |
| 56 | + ) |
| 57 | + @url = issue.html_url |
| 58 | + problem.update_attributes( |
| 59 | + :issue_link => issue.html_url, |
| 60 | + :issue_type => Label |
| 61 | + ) |
| 62 | + |
| 63 | + rescue Octokit::Unauthorized |
| 64 | + raise ErrbitGithubPlugin::AuthenticationError, "Could not authenticate with GitHub. Please check your username and password." |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + def body_template |
| 69 | + @@body_template ||= ERB.new(File.read(Rails.root + "app/views/issue_trackers/github_issues_body.txt.erb").gsub(/^\s*/, '')) |
| 70 | + end |
| 71 | + end |
| 72 | +end |
0 commit comments