Class: Repofetch::BitbucketCloud
- Extended by:
- Util
- Defined in:
- lib/repofetch/bitbucketcloud.rb,
lib/repofetch/bitbucketcloud/stats.rb
Overview
Adds support for Bitbucket repositories.
Defined Under Namespace
Modules: Stats
Constant Summary collapse
- HTTP_REMOTE_REGEX =
%r{https?://bitbucket\.org/(?<owner>[\w._-]+)/(?<repo>[\w._-]+)}.freeze
- SSH_REMOTE_REGEX =
%r{git@bitbucket\.org:(?<owner>[\w._-]+)/(?<repo>[\w._-]+)}.freeze
- ASCII =
File.read(File.('bitbucketcloud/ASCII', __dir__))
Instance Attribute Summary collapse
-
#repo_identifier ⇒ Object
readonly
Returns the value of attribute repo_identifier.
Class Method Summary collapse
- .from_args(args) ⇒ Object
-
.from_git(git, args) ⇒ Object
Creates an instance from a
Git::Base
instance. -
.matches_remote?(remote) ⇒ Boolean
Detects that the remote URL is for a Bitbucket Cloud repository.
-
.matches_repo?(git) ⇒ Boolean
Detects that the repository is a Bitbucket repository.
-
.remote_identifiers(remote) ⇒ Object
Gets the owner and repository from a GitHub remote URL.
-
.repo_identifiers(git) ⇒ Object
Gets the owner and repository from a GitHub local repository.
Instance Method Summary collapse
- #agent ⇒ Object
- #ascii ⇒ Object
- #header ⇒ Object
-
#initialize(repo_identifier) ⇒ BitbucketCloud
constructor
A new instance of BitbucketCloud.
- #primary_color ⇒ Object
- #stats ⇒ Object
- #token ⇒ Object
Methods included from Util
clean_ansi, default_remote, default_remote_url, remove_format_params
Methods included from Stats
#clone_urls, #created, #forks, #http_clone_url, #issues, #pull_requests, #repo_data, #size, #ssh_clone_url, #updated, #watchers
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) ⇒ BitbucketCloud
Returns a new instance of BitbucketCloud.
25 26 27 28 29 |
# File 'lib/repofetch/bitbucketcloud.rb', line 25 def initialize(repo_identifier) super @repo_identifier = repo_identifier end |
Instance Attribute Details
#repo_identifier ⇒ Object (readonly)
Returns the value of attribute repo_identifier.
23 24 25 |
# File 'lib/repofetch/bitbucketcloud.rb', line 23 def repo_identifier @repo_identifier end |
Class Method Details
.from_args(args) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/repofetch/bitbucketcloud.rb', line 94 def self.from_args(args) parser = OptionParser.new do |opts| opts. = 'Usage: <plugin activation> -- [options] OWNER/PROJECT' opts.separator '' opts.separator 'This plugin can use the BITBUCKET_TOKEN environment variable' 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.
86 87 88 89 90 91 92 |
# File 'lib/repofetch/bitbucketcloud.rb', line 86 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 Bitbucket Cloud repository.
63 64 65 |
# File 'lib/repofetch/bitbucketcloud.rb', line 63 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 Bitbucket repository.
58 59 60 |
# File 'lib/repofetch/bitbucketcloud.rb', line 58 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.
75 76 77 78 79 80 81 |
# File 'lib/repofetch/bitbucketcloud.rb', line 75 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 Bitbucket Cloud remote" if match.nil? [match[:owner], match[:repo].delete_suffix('.git')] end |
.repo_identifiers(git) ⇒ Object
Gets the owner and repository from a GitHub local repository.
68 69 70 |
# File 'lib/repofetch/bitbucketcloud.rb', line 68 def self.repo_identifiers(git) remote_identifiers(default_remote_url(git)) end |
Instance Method Details
#agent ⇒ Object
47 48 49 50 51 |
# File 'lib/repofetch/bitbucketcloud.rb', line 47 def agent @agent ||= Sawyer::Agent.new('https://api.bitbucket.org/2.0') do |http| http.headers['Authorization'] = "Bearer #{token}" unless token.nil? end end |
#ascii ⇒ Object
43 44 45 |
# File 'lib/repofetch/bitbucketcloud.rb', line 43 def ascii ASCII end |
#header ⇒ Object
31 32 33 |
# File 'lib/repofetch/bitbucketcloud.rb', line 31 def header ["#{repo_data['owner']['display_name']}/#{repo_data['name']}", 'Bitbucket'] end |
#primary_color ⇒ Object
35 36 37 |
# File 'lib/repofetch/bitbucketcloud.rb', line 35 def primary_color :blue end |
#stats ⇒ Object
39 40 41 |
# File 'lib/repofetch/bitbucketcloud.rb', line 39 def stats [http_clone_url, ssh_clone_url, watchers, forks, created, updated, size, issues, pull_requests] end |
#token ⇒ Object
53 54 55 |
# File 'lib/repofetch/bitbucketcloud.rb', line 53 def token ENV.fetch('BITBUCKET_TOKEN', nil) end |