Skip to content

Commit

Permalink
Query GitLab stats
Browse files Browse the repository at this point in the history
  • Loading branch information
spenserblack authored Dec 30, 2022
1 parent 5c4d331 commit 14bb9f3
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions lib/repofetch/gitlab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'cgi'
require 'repofetch'
require 'sawyer'

class Repofetch
# Adds support for GitLab repositories.
Expand All @@ -16,13 +17,16 @@ def initialize(repo_identifier)
end

def header
# NOTE: Use `path_with_namespace`
"#{@repo_identifier} @ GitLab"
"#{repo_data['name_with_namespace']} @ GitLab"
end

def stats
# NOTE: Use `name`
[Repofetch::Stat.new('Hello', 'World')].each { |stat| %i[bold red].each { |style| stat.style_label!(style) } }
stats = [url, stars, forks, created, updated]

# NOTE: Stats that require authentication
stats.concat([open_issues]) unless token.nil?

stats.each { |stat| %i[bold red].each { |style| stat.style_label!(style) } }
end

def ascii
Expand All @@ -32,6 +36,46 @@ def ascii
ASCII
end

def agent
@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

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

def repo_data
@repo_data ||= agent.call(:get, "projects/#{@repo_identifier}").data
end

def url
Repofetch::Stat.new('URL', repo_data['http_url_to_repo'], emoji: '🌐')
end

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

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

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

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

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

def self.from_git(*)
false
end
Expand Down

0 comments on commit 14bb9f3

Please sign in to comment.