Class: Repofetch::Github
- Includes:
- ActionView::Helpers::NumberHelper
- Defined in:
- lib/repofetch/github.rb
Overview
Adds support for GitHub repositories.
Constant Summary collapse
- HTTP_REMOTE_REGEX =
%r{https?://github\.com/(?<owner>[\w.\-]+)/(?<repository>[\w.\-]+)}.freeze
- SSH_REMOTE_REGEX =
%r{git@github\.com:(?<owner>[\w.\-]+)/(?<repository>[\w.\-]+)}.freeze
- ASCII =
File.read(File.('github/ASCII', __dir__))
Instance Attribute Summary collapse
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#repository ⇒ Object
readonly
Returns the value of attribute repository.
Class Method Summary collapse
-
.from_args(args) ⇒ Object
Creates an instance from CLI args and configuration.
-
.from_git(git, args) ⇒ Object
Creates an instance from a
Git::Baseinstance. -
.matches_remote?(remote) ⇒ Boolean
Detects that the remote URL is for a GitHub repository.
-
.matches_repo?(git) ⇒ Boolean
Detects that the repository is a GitHub repository.
-
.remote_identifiers(remote) ⇒ Object
Gets the owner and repository from a GitHub remote URL.
-
.repo_identifiers(git) ⇒ Object
Gets the owner and repository from a GitHub local repository.
Instance Method Summary collapse
- #ascii ⇒ Object
- #header ⇒ Object
-
#initialize(owner, repository) ⇒ Github
constructor
Initializes the GitHub plugin.
- #repo_id ⇒ Object
- #stats ⇒ Object
Methods inherited from Plugin
register, replace_or_register, #separator, #stat_lines!, #theme, #theme_stats!, #to_s, #zipped_lines
Constructor Details
#initialize(owner, repository) ⇒ Github
Initializes the GitHub plugin.
20 21 22 23 24 25 26 |
# File 'lib/repofetch/github.rb', line 20 def initialize(owner, repository) super @owner = owner @repository = repository @client = Octokit::Client.new(access_token: ENV.fetch('GITHUB_TOKEN', nil)) end |
Instance Attribute Details
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
17 18 19 |
# File 'lib/repofetch/github.rb', line 17 def owner @owner end |
#repository ⇒ Object (readonly)
Returns the value of attribute repository.
17 18 19 |
# File 'lib/repofetch/github.rb', line 17 def repository @repository end |
Class Method Details
.from_args(args) ⇒ Object
Creates an instance from CLI args and configuration.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/repofetch/github.rb', line 82 def self.from_args(args) parser = OptionParser.new do |opts| opts. = 'Usage: <plugin activation> -- [options] OWNER/REPOSITORY' opts.separator '' opts.separator 'This plugin can use the GITHUB_TOKEN environment variable increase rate limits' end parser.parse(args) split = args[0]&.split('/') # TODO: Raise a better exception than ArgumentError raise ArgumentError, parser.to_s unless split&.length == 2 new(*split) end |
.from_git(git, args) ⇒ Object
Creates an instance from a Git::Base instance.
70 71 72 73 74 75 76 77 |
# File 'lib/repofetch/github.rb', line 70 def self.from_git(git, args) # TODO: Raise a better exception than ArgumentError raise ArgumentError, 'Explicitly activate this plugin to CLI arguments' unless args.empty? owner, repository = repo_identifiers(git) new(owner, repository) end |
.matches_remote?(remote) ⇒ Boolean
Detects that the remote URL is for a GitHub repository.
45 46 47 |
# File 'lib/repofetch/github.rb', line 45 def self.matches_remote?(remote) HTTP_REMOTE_REGEX.match?(remote) || SSH_REMOTE_REGEX.match?(remote) end |
.matches_repo?(git) ⇒ Boolean
Detects that the repository is a GitHub repository.
38 39 40 41 42 |
# File 'lib/repofetch/github.rb', line 38 def self.matches_repo?(git) default_remote = Repofetch.default_remote(git) url = default_remote&.url matches_remote?(url) end |
.remote_identifiers(remote) ⇒ Object
Gets the owner and repository from a GitHub remote URL.
Returns nil if there is no match.
59 60 61 62 63 64 65 |
# File 'lib/repofetch/github.rb', line 59 def self.remote_identifiers(remote) match = HTTP_REMOTE_REGEX.match(remote) match = SSH_REMOTE_REGEX.match(remote) if match.nil? raise "Remote #{remote.inspect} doesn't look like a GitHub remote" if match.nil? [match[:owner], match[:repository].delete_suffix('.git')] end |
.repo_identifiers(git) ⇒ Object
Gets the owner and repository from a GitHub local repository.
50 51 52 53 54 |
# File 'lib/repofetch/github.rb', line 50 def self.repo_identifiers(git) default_remote = Repofetch.default_remote(git) url = default_remote&.url remote_identifiers(url) end |
Instance Method Details
#ascii ⇒ Object
101 102 103 |
# File 'lib/repofetch/github.rb', line 101 def ascii ASCII end |
#header ⇒ Object
97 98 99 |
# File 'lib/repofetch/github.rb', line 97 def header "#{theme.format(:bold, "#{owner}/#{repository}")} @ #{theme.format(:bold, 'GitHub')}" end |
#repo_id ⇒ Object
28 29 30 |
# File 'lib/repofetch/github.rb', line 28 def repo_id "#{@owner}/#{@repository}" end |
#stats ⇒ Object
32 33 34 35 |
# File 'lib/repofetch/github.rb', line 32 def stats stats = [url, stargazers, subscribers, forks, created, updated, size, issues, pull_requests] stats.each { |stat| stat.style_label!(:bold) } end |