Class: Repofetch::Gitlab

Inherits:
Plugin
  • Object
show all
Extended by:
Util
Includes:
Util
Defined in:
lib/repofetch/gitlab.rb

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.

Parameters:

  • repo_identifier (String)

    The repository identifier (either the ID number or the namespaced repo name).



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_identifierObject (readonly)

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.

Raises:



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.

Returns:

  • (Boolean)


114
115
116
# File 'lib/repofetch/gitlab.rb', line 114

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)


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

#agentObject



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.headers['Authorization'] = "Bearer #{token}" unless token.nil?
  end
end

#asciiObject



45
46
47
# File 'lib/repofetch/gitlab.rb', line 45

def ascii
  ASCII
end

#createdObject



79
80
81
# File 'lib/repofetch/gitlab.rb', line 79

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

#forksObject



75
76
77
# File 'lib/repofetch/gitlab.rb', line 75

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

#headerObject



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

def header
  [repo_data['name_with_namespace'], 'GitLab']
end

#http_clone_urlObject



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_issuesObject



87
88
89
90
# File 'lib/repofetch/gitlab.rb', line 87

def open_issues
  # NOTE: It seems like the auth token must be set to get the open issues count.
  Repofetch::Stat.new('open issues', repo_data['open_issues_count'], emoji: '')
end

#primary_colorObject



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

def primary_color
  :red
end

#repo_dataObject



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_urlObject



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

#starsObject



71
72
73
# File 'lib/repofetch/gitlab.rb', line 71

def stars
  Repofetch::Stat.new('stars', repo_data['star_count'], emoji: '')
end

#statsObject



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]

  # NOTE: Stats that require authentication
  stats << open_issues unless token.nil?

  stats
end

#tokenObject



55
56
57
# File 'lib/repofetch/gitlab.rb', line 55

def token
  ENV.fetch('GITLAB_TOKEN', nil)
end

#updatedObject



83
84
85
# File 'lib/repofetch/gitlab.rb', line 83

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