Class: ErrbitGithubPlugin::IssueTracker

Inherits:
ErrbitPlugin::IssueTracker
  • Object
show all
Defined in:
lib/errbit_github_plugin/issue_tracker.rb

Constant Summary collapse

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"
  }
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fieldsObject



33
34
35
# File 'lib/errbit_github_plugin/issue_tracker.rb', line 33

def self.fields
  FIELDS
end

.iconsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/errbit_github_plugin/issue_tracker.rb', line 37

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

.labelObject



25
26
27
# File 'lib/errbit_github_plugin/issue_tracker.rb', line 25

def self.label
  LABEL
end

.noteObject



29
30
31
# File 'lib/errbit_github_plugin/issue_tracker.rb', line 29

def self.note
  NOTE
end

Instance Method Details

#close_issue(url, user: {}) ⇒ String

Returns The URL of the closed issue.

Parameters:

  • url (String)
  • user (Hash) (defaults to: {})

Returns:

  • (String)

    The URL of the closed issue



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/errbit_github_plugin/issue_tracker.rb', line 96

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

#configured?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/errbit_github_plugin/issue_tracker.rb', line 51

def configured?
  errors.empty?
end

#create_issue(title, body, user: {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/errbit_github_plugin/issue_tracker.rb', line 77

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

#errorsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/errbit_github_plugin/issue_tracker.rb', line 59

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

#repoObject



73
74
75
# File 'lib/errbit_github_plugin/issue_tracker.rb', line 73

def repo
  options[:github_repo]
end

#urlObject



55
56
57
# File 'lib/errbit_github_plugin/issue_tracker.rb', line 55

def url
  "https://github.com/#{repo}/issues"
end