From 14bb9f3f6fc5de8eb9d387fe489364f23b2bfcc6 Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Fri, 30 Dec 2022 16:23:50 +0000 Subject: [PATCH] Query GitLab stats --- lib/repofetch/gitlab.rb | 52 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/repofetch/gitlab.rb b/lib/repofetch/gitlab.rb index 1af3df70..01094714 100644 --- a/lib/repofetch/gitlab.rb +++ b/lib/repofetch/gitlab.rb @@ -2,6 +2,7 @@ require 'cgi' require 'repofetch' +require 'sawyer' class Repofetch # Adds support for GitLab repositories. @@ -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 @@ -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