Class: Repofetch::BitbucketCloud

Inherits:
Plugin
  • Object
show all
Extended by:
Util
Includes:
ActionView::Helpers::NumberHelper, Stats, 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.expand_path('bitbucketcloud/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 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_identifierObject (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.banner = '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.

Raises:



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

#agentObject



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

#asciiObject



43
44
45
# File 'lib/repofetch/bitbucketcloud.rb', line 43

def ascii
  ASCII
end

#headerObject



31
32
33
# File 'lib/repofetch/bitbucketcloud.rb', line 31

def header
  ["#{repo_data['owner']['display_name']}/#{repo_data['name']}", 'Bitbucket']
end

#primary_colorObject



35
36
37
# File 'lib/repofetch/bitbucketcloud.rb', line 35

def primary_color
  :blue
end

#statsObject



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

#tokenObject



53
54
55
# File 'lib/repofetch/bitbucketcloud.rb', line 53

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