Class: Repofetch::Github

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#ownerObject (readonly)

Returns the value of attribute owner.



21
22
23
# File 'lib/repofetch/github.rb', line 21

def owner
  @owner
end

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

Raises:



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.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('/')

  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.

Raises:



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

#asciiObject



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

def ascii
  ASCII
end

#headerObject



94
95
96
# File 'lib/repofetch/github.rb', line 94

def header
  ["#{owner}/#{repository}", 'GitHub']
end

#repo_idObject



32
33
34
# File 'lib/repofetch/github.rb', line 32

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

#statsObject



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