33module ErrbitGithubPlugin
44 class IssueTracker < ErrbitPlugin ::IssueTracker
55
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
6+ LABEL = 'github'
7+
8+ NOTE = 'Please configure your github repository in the <strong>GITHUB ' <<
9+ 'REPO</strong> field above.<br/> Instead of providing your ' <<
10+ 'username & password, you can link your Github account to your ' <<
11+ 'user profile, and allow Errbit to create issues using your ' <<
12+ 'OAuth token.'
1213
13- def fields
14- {
15- :username => {
16- :placeholder => "Your username on GitHub"
17- } ,
18- :password => {
19- :placeholder => "Password for your account"
20- }
14+ FIELDS = {
15+ :username => {
16+ :placeholder => "Your username on GitHub"
17+ } ,
18+ :password => {
19+ :placeholder => "Password for your account"
2120 }
21+ }
22+
23+ def self . label
24+ LABEL
25+ end
26+
27+ def self . note
28+ NOTE
29+ end
30+
31+ def self . fields
32+ FIELDS
33+ end
34+
35+ def self . body_template
36+ @body_template ||= ERB . new ( File . read (
37+ File . join (
38+ ErrbitGithubPlugin . root , 'views' , 'github_issues_body.txt.erb'
39+ )
40+ ) )
2241 end
2342
2443 attr_accessor :oauth_token
@@ -34,39 +53,41 @@ def project_id
3453 app . github_repo
3554 end
3655
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'
56+ def errors
57+ errors = [ ]
58+ if self . class . fields . detect { |f | params [ f [ 0 ] ] . blank? }
59+ errors << [ :base , 'You must specify your GitHub username and password' ]
4060 end
61+ errors
4162 end
4263
43- def create_issue ( problem , reported_by = nil )
64+ def github_client
4465 # Login using OAuth token, if given.
4566 if oauth_token
46- client = Octokit ::Client . new ( :login => username , :oauth_token => oauth_token )
67+ Octokit ::Client . new (
68+ :login => params [ 'username' ] , :oauth_token => oauth_token )
4769 else
48- client = Octokit ::Client . new ( :login => username , :password => password )
70+ Octokit ::Client . new (
71+ :login => params [ 'username' ] , :password => params [ 'password' ] )
4972 end
73+ end
5074
75+ def create_issue ( problem , reported_by = nil )
5176 begin
52- issue = client . create_issue (
77+ issue = github_client . create_issue (
5378 project_id ,
54- issue_title ( problem ) ,
55- body_template . result ( binding ) . unpack ( 'C*' ) . pack ( 'U*' )
79+ "[ #{ problem . environment } ][ #{ problem . where } ] #{ problem . message . to_s . truncate ( 100 ) } " ,
80+ self . class . body_template . result ( binding ) . unpack ( 'C*' ) . pack ( 'U*' )
5681 )
5782 @url = issue . html_url
5883 problem . update_attributes (
5984 :issue_link => issue . html_url ,
60- :issue_type => Label
85+ :issue_type => self . class . label
6186 )
6287
6388 rescue Octokit ::Unauthorized
6489 raise ErrbitGithubPlugin ::AuthenticationError , "Could not authenticate with GitHub. Please check your username and password."
6590 end
6691 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
7192 end
7293end
0 commit comments