Class: Repofetch::Github
- Extended by:
- Util
- Includes:
- ActionView::Helpers::NumberHelper, Util
- 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::Base
instance. -
.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 included from Util
clean_ansi, default_remote, default_remote_url, remove_format_params
Methods inherited from Plugin
#formatted_header, from_path, #header_joiner, matches_path?, #primary_color, register, replace_or_register, #separator, #stat_lines, #theme, #to_s, #zipped_lines
Constructor Details
#initialize(owner, repository) ⇒ Github
Initializes the GitHub plugin.
24 25 26 27 28 29 30 |
# File 'lib/repofetch/github.rb', line 24 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.
21 22 23 |
# File 'lib/repofetch/github.rb', line 21 def owner @owner end |
#repository ⇒ Object (readonly)
Returns the value of attribute repository.
21 22 23 |
# File 'lib/repofetch/github.rb', line 21 def repository @repository end |
Class Method Details
.from_args(args) ⇒ Object
Creates an instance from CLI args and configuration.
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/repofetch/github.rb', line 80 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('/') raise Repofetch::PluginUsageError, parser.to_s unless split&.length == 2 new(*split) end |
.from_git(git, args) ⇒ Object
Creates an instance from a Git::Base
instance.
69 70 71 72 73 74 75 |
# File 'lib/repofetch/github.rb', line 69 def self.from_git(git, args) raise Repofetch::PluginUsageError, '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.
46 47 48 |
# File 'lib/repofetch/github.rb', line 46 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.
41 42 43 |
# File 'lib/repofetch/github.rb', line 41 def self.matches_repo?(git) matches_remote?(default_remote_url(git)) end |
.remote_identifiers(remote) ⇒ Object
Gets the owner and repository from a GitHub remote URL.
Returns nil if there is no match.
58 59 60 61 62 63 64 |
# File 'lib/repofetch/github.rb', line 58 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.
51 52 53 |
# File 'lib/repofetch/github.rb', line 51 def self.repo_identifiers(git) remote_identifiers(default_remote_url(git)) end |
Instance Method Details
#ascii ⇒ Object
98 99 100 |
# File 'lib/repofetch/github.rb', line 98 def ascii ASCII end |
#header ⇒ Object
94 95 96 |
# File 'lib/repofetch/github.rb', line 94 def header ["#{owner}/#{repository}", 'GitHub'] end |
#repo_id ⇒ Object
32 33 34 |
# File 'lib/repofetch/github.rb', line 32 def repo_id "#{@owner}/#{@repository}" end |
#stats ⇒ Object
36 37 38 |
# File 'lib/repofetch/github.rb', line 36 def stats [http_clone_url, ssh_clone_url, stargazers, subscribers, forks, created, updated, size, issues, pull_requests] end |