-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathissue_tracker.rb
More file actions
116 lines (99 loc) · 3.21 KB
/
issue_tracker.rb
File metadata and controls
116 lines (99 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# frozen_string_literal: true
require "active_support/core_ext/object/blank"
require "octokit"
module ErrbitGithubPlugin
class IssueTracker < ErrbitPlugin::IssueTracker
LABEL = "github"
NOTE = "Please configure your GitHub repository in the <strong>GITHUB " \
"REPO</strong> field above.<br> Instead of providing your " \
"username & password, you can link your GitHub account to your " \
"user profile, and allow Errbit to create issues using your " \
"OAuth token."
FIELDS = {
username: {
placeholder: "Your username on GitHub"
},
password: {
placeholder: "Password for your account"
}
}
def self.label
LABEL
end
def self.note
NOTE
end
def self.fields
FIELDS
end
def self.icons
@icons ||= {
create: [
"image/png", ErrbitGithubPlugin.read_static_file("github_create.png")
],
goto: [
"image/png", ErrbitGithubPlugin.read_static_file("github_goto.png")
],
inactive: [
"image/png", ErrbitGithubPlugin.read_static_file("github_inactive.png")
]
}
end
def configured?
errors.empty?
end
def url
"https://github.com/#{repo}/issues"
end
def errors
errors = []
if self.class.fields.detect { |f| options[f[0]].blank? }
errors << [:base, "You must specify your GitHub username and password"]
end
if repo.blank?
errors << [:base, "You must specify your GitHub repository url."]
end
errors
end
def repo
options[:github_repo]
end
def create_issue(title, body, user: {})
github_client = if user["github_login"] && user["github_oauth_token"]
Octokit::Client.new(
login: user["github_login"], access_token: user["github_oauth_token"]
)
else
Octokit::Client.new(
login: options["username"], password: options["password"]
)
end
issue = github_client.create_issue(repo, title, body)
issue.html_url
rescue Octokit::Unauthorized
raise ErrbitGithubPlugin::AuthenticationError, "Could not authenticate with GitHub. Please check your username and password."
end
# @param url [String]
# @param user [Hash]
# @return [String] The URL of the closed issue
def close_issue(url, user: {})
github_client = if user["github_login"] && user["github_oauth_token"]
Octokit::Client.new(
login: user["github_login"], access_token: user["github_oauth_token"]
)
else
Octokit::Client.new(
login: options["username"], password: options["password"]
)
end
# It would be better to get the number from issue.number when we create the issue,
# however, since we only have the url, get the number from it.
# ex: "https://github.com/octocat/Hello-World/issues/1347"
issue_number = url.split("/").last
issue = github_client.close_issue(repo, issue_number)
issue.html_url
rescue Octokit::Unauthorized
raise ErrbitGithubPlugin::AuthenticationError, "Could not authenticate with GitHub. Please check your username and password."
end
end
end