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

#createdObject (protected)



129
130
131
# File 'lib/repofetch/github.rb', line 129

def created
  Repofetch::TimespanStat.new('created', repo_stats['created_at'], emoji: '🐣')
end

#forksObject (protected)



125
126
127
# File 'lib/repofetch/github.rb', line 125

def forks
  Repofetch::Stat.new('forks', repo_stats['forks_count'], emoji: '🔱')
end

#headerObject



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

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

#http_clone_urlObject (protected)



109
110
111
# File 'lib/repofetch/github.rb', line 109

def http_clone_url
  Repofetch::Stat.new('HTTP(S)', repo_stats['clone_url'], emoji: '🌐')
end

#issuesObject (protected)



143
144
145
146
# File 'lib/repofetch/github.rb', line 143

def issues
  @issue_search = @client.search_issues("repo:#{repo_id} is:issue", per_page: 1, page: 0) if @issue_search.nil?
  Repofetch::Stat.new('issues', @issue_search['total_count'], emoji: '')
end

#pull_requestsObject (protected)



148
149
150
151
# File 'lib/repofetch/github.rb', line 148

def pull_requests
  @pr_search = @client.search_issues("repo:#{repo_id} is:pr", per_page: 1, page: 0) if @pr_search.nil?
  Repofetch::Stat.new('pull requests', @pr_search['total_count'], emoji: '🔀')
end

#repo_idObject



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

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

#repo_statsObject (protected)



104
105
106
107
# File 'lib/repofetch/github.rb', line 104

def repo_stats
  @repo_stats = @client.repository(repo_id) if @repo_stats.nil?
  @repo_stats
end

#sizeObject (protected)



137
138
139
140
141
# File 'lib/repofetch/github.rb', line 137

def size
  byte_size = number_to_human_size((repo_stats['size'] || 0) * 1024, precision: 2, significant: false,
                                                                     strip_insignificant_zeros: false)
  Repofetch::Stat.new('size', byte_size, emoji: '💽')
end

#ssh_clone_urlObject (protected)



113
114
115
# File 'lib/repofetch/github.rb', line 113

def ssh_clone_url
  Repofetch::Stat.new('SSH', repo_stats['ssh_url'], emoji: '🔑')
end

#stargazersObject (protected)



117
118
119
# File 'lib/repofetch/github.rb', line 117

def stargazers
  Repofetch::Stat.new('stargazers', repo_stats['stargazers_count'], emoji: '')
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

#subscribersObject (protected)



121
122
123
# File 'lib/repofetch/github.rb', line 121

def subscribers
  Repofetch::Stat.new('subscribers', repo_stats['subscribers_count'], emoji: '👀')
end

#updatedObject (protected)



133
134
135
# File 'lib/repofetch/github.rb', line 133

def updated
  Repofetch::TimespanStat.new('updated', repo_stats['updated_at'], emoji: '📤')
end