Class: Repofetch::Gitlab
Overview
Adds support for GitLab repositories.
Constant Summary
collapse
- HTTP_REMOTE_REGEX =
%r{https?://gitlab\.com/(?<path>[\w.-][\w.\-/]+)}.freeze
- SSH_REMOTE_REGEX =
%r{git@gitlab\.com:(?<path>[\w.-][\w.\-/]+)}.freeze
- ASCII =
File.read(File.expand_path('gitlab/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?, register, replace_or_register, #separator, #stat_lines, #theme, #to_s, #zipped_lines
Constructor Details
#initialize(repo_identifier) ⇒ Gitlab
Returns a new instance of Gitlab.
22
23
24
25
26
|
# File 'lib/repofetch/gitlab.rb', line 22
def initialize(repo_identifier)
super
@repo_identifier = CGI.escape(repo_identifier)
end
|
Instance Attribute Details
#repo_identifier ⇒ Object
Returns the value of attribute repo_identifier.
19
20
21
|
# File 'lib/repofetch/gitlab.rb', line 19
def repo_identifier
@repo_identifier
end
|
Class Method Details
.from_args(args) ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/repofetch/gitlab.rb', line 129
def self.from_args(args)
parser = OptionParser.new do |opts|
opts.banner = 'Usage: <plugin activation> -- [options] OWNER/PROJECT/SUBPROJECT'
opts.separator ''
opts.separator 'This plugin can use the GITLAB_TOKEN environment variable to fetch more data'
end
parser.parse(args)
raise Repofetch::PluginUsageError, parser.to_s unless args.length == 1
new(args[0])
end
|
.from_git(git, args) ⇒ Object
Creates an instance from a Git::Base
instance.
121
122
123
124
125
126
127
|
# File 'lib/repofetch/gitlab.rb', line 121
def self.from_git(git, args)
raise Repofetch::PluginUsageError, 'Explicitly activate this plugin to CLI arguments' unless args.empty?
path = repo_identifier(git)
new(path)
end
|
.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.
109
110
111
|
# File 'lib/repofetch/gitlab.rb', line 109
def self.matches_repo?(git)
matches_remote?(default_remote_url(git))
end
|
.remote_identifier(remote) ⇒ Object
Gets the path (owner/subproject/repo
) of the repository.
Returns nil if there is no match.
100
101
102
103
104
105
106
|
# File 'lib/repofetch/gitlab.rb', line 100
def self.remote_identifier(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 GitLab remote" if match.nil?
match[:path].delete_suffix('.git')
end
|
.repo_identifier(git) ⇒ Object
Gets the path (owner/subproject/repo
) of the repository.
93
94
95
|
# File 'lib/repofetch/gitlab.rb', line 93
def self.repo_identifier(git)
remote_identifier(default_remote_url(git))
end
|
Instance Method Details
#agent ⇒ Object
49
50
51
52
53
|
# File 'lib/repofetch/gitlab.rb', line 49
def agent
@agent ||= Sawyer::Agent.new('https://gitlab.com/api/v4', links_parser: Sawyer::LinkParsers::Simple.new) do |http|
http.['Authorization'] = "Bearer #{token}" unless token.nil?
end
end
|
#ascii ⇒ Object
45
46
47
|
# File 'lib/repofetch/gitlab.rb', line 45
def ascii
ASCII
end
|
#created ⇒ Object
79
80
81
|
# File 'lib/repofetch/gitlab.rb', line 79
def created
Repofetch::TimespanStat.new('created', repo_data['created_at'], emoji: '🐣')
end
|
#forks ⇒ Object
75
76
77
|
# File 'lib/repofetch/gitlab.rb', line 75
def forks
Repofetch::Stat.new('forks', repo_data['forks_count'], emoji: '🔱')
end
|
28
29
30
|
# File 'lib/repofetch/gitlab.rb', line 28
def
[repo_data['name_with_namespace'], 'GitLab']
end
|
#http_clone_url ⇒ Object
63
64
65
|
# File 'lib/repofetch/gitlab.rb', line 63
def http_clone_url
Repofetch::Stat.new('HTTP(S)', repo_data['http_url_to_repo'], emoji: '🌐')
end
|
#open_issues ⇒ Object
87
88
89
90
|
# File 'lib/repofetch/gitlab.rb', line 87
def open_issues
Repofetch::Stat.new('open issues', repo_data['open_issues_count'], emoji: '❗')
end
|
#primary_color ⇒ Object
32
33
34
|
# File 'lib/repofetch/gitlab.rb', line 32
def primary_color
:red
end
|
#repo_data ⇒ Object
59
60
61
|
# File 'lib/repofetch/gitlab.rb', line 59
def repo_data
@repo_data ||= agent.call(:get, "projects/#{@repo_identifier}").data
end
|
#ssh_clone_url ⇒ Object
67
68
69
|
# File 'lib/repofetch/gitlab.rb', line 67
def ssh_clone_url
Repofetch::Stat.new('SSH', repo_data['ssh_url_to_repo'], emoji: '🔑')
end
|
#stars ⇒ Object
71
72
73
|
# File 'lib/repofetch/gitlab.rb', line 71
def stars
Repofetch::Stat.new('stars', repo_data['star_count'], emoji: '⭐')
end
|
#stats ⇒ Object
36
37
38
39
40
41
42
43
|
# File 'lib/repofetch/gitlab.rb', line 36
def stats
stats = [http_clone_url, ssh_clone_url, stars, forks, created, updated]
stats << open_issues unless token.nil?
stats
end
|
#token ⇒ Object
55
56
57
|
# File 'lib/repofetch/gitlab.rb', line 55
def token
ENV.fetch('GITLAB_TOKEN', nil)
end
|
#updated ⇒ Object
83
84
85
|
# File 'lib/repofetch/gitlab.rb', line 83
def updated
Repofetch::TimespanStat.new('updated', repo_data['last_activity_at'], emoji: '📤')
end
|