Class: Repofetch::Github

Inherits:
Plugin
  • Object
show all
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.expand_path('github/ASCII', __dir__))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#ownerObject (readonly)

Returns the value of attribute owner.



17
18
19
# File 'lib/repofetch/github.rb', line 17

def owner
  @owner
end

#repositoryObject (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.

Raises:

  • (ArgumentError)

    if args couldn’t be parsed.



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.banner = '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.

Raises:

  • (ArgumentError)

    if this plugin was selected and arguments were passed.



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

#asciiObject



101
102
103
# File 'lib/repofetch/github.rb', line 101

def ascii
  ASCII
end

#headerObject



97
98
99
# File 'lib/repofetch/github.rb', line 97

def header
  "#{theme.format(:bold, "#{owner}/#{repository}")} @ #{theme.format(:bold, 'GitHub')}"
end

#repo_idObject



28
29
30
# File 'lib/repofetch/github.rb', line 28

def repo_id
  "#{@owner}/#{@repository}"
end

#statsObject



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