File Coverage

blib/lib/WebService/BitbucketServer/Spec.pm
Criterion Covered Total %
statement 12 60 20.0
branch 0 20 0.0
condition 0 5 0.0
subroutine 4 9 44.4
pod 4 4 100.0
total 20 98 20.4


line stmt bran cond sub pod time code
1             package WebService::BitbucketServer::Spec;
2             # ABSTRACT: Databases for mapping Bitbucket Server REST APIs to code
3              
4 4     4   22 use warnings;
  4         10  
  4         111  
5 4     4   18 use strict;
  4         7  
  4         123  
6              
7             our $VERSION = '0.602'; # VERSION
8              
9 4     4   17 use Exporter qw(import);
  4         7  
  4         117  
10 4     4   20 use namespace::clean -except => [qw(import)];
  4         37  
  4         25  
11              
12             our @EXPORT_OK = qw(api_info documentation_url package_name sub_name);
13              
14              
15             our $DOCUMENTATION_URL = 'https://developer.atlassian.com/static/rest/bitbucket-server/latest/';
16              
17             our %API = (
18             'access-tokens/1.0' => {
19             id => 'access_tokens',
20             documentation_filename => 'bitbucket-access-tokens-rest',
21             package => 'AccessTokens::V1',
22             },
23             'api/1.0' => {
24             id => 'core',
25             documentation_filename => 'bitbucket-rest',
26             package => 'Core::V1',
27             },
28             'audit/1.0' => {
29             id => 'audit',
30             documentation_filename => 'bitbucket-audit-rest',
31             package => 'Audit::V1',
32             },
33             'branch-permissions/2.0' => {
34             id => 'ref_restriction',
35             documentation_filename => 'bitbucket-ref-restriction-rest',
36             package => 'RefRestriction::V2',
37             },
38             'branch-utils/1.0' => {
39             id => 'branch',
40             documentation_filename => 'bitbucket-branch-rest',
41             package => 'Branch::V1',
42             },
43             'build-status/1.0' => {
44             id => 'build',
45             documentation_filename => 'bitbucket-build-rest',
46             package => 'Build::V1',
47             },
48             'comment-likes/1.0' => {
49             id => 'comment_likes',
50             documentation_filename => 'bitbucket-comment-likes-rest',
51             package => 'CommentLikes::V1',
52             },
53             'default-reviewers/1.0' => {
54             id => 'default_reviewers',
55             documentation_filename => 'bitbucket-default-reviewers-rest',
56             package => 'DefaultReviewers::V1',
57             },
58             'git/1.0' => {
59             id => 'git',
60             documentation_filename => 'bitbucket-git-rest',
61             package => 'Git::V1',
62             },
63             'gpg/1.0' => {
64             id => 'gpg',
65             documentation_filename => 'bitbucket-gpg-rest',
66             package => 'GPG::V1',
67             },
68             'jira/1.0' => {
69             id => 'jira',
70             documentation_filename => 'bitbucket-jira-rest',
71             package => 'JIRA::V1',
72             },
73             'ssh/1.0' => {
74             id => 'ssh',
75             documentation_filename => 'bitbucket-ssh-rest',
76             package => 'SSH::V1',
77             },
78             'mirroring/1.0' => {
79             id => 'mirroring_upstream',
80             documentation_filename => 'bitbucket-mirroring-upstream-rest',
81             package => 'MirroringUpstream::V1',
82             },
83             'sync/1.0' => {
84             id => 'repository_ref_sync',
85             documentation_filename => 'bitbucket-repository-ref-sync-rest',
86             package => 'RepositoryRefSync::V1',
87             },
88             );
89             $API{'keys/1.0'} = $API{'ssh/1.0'};
90              
91              
92             sub api_info {
93 0     0 1   my $endpoint = shift;
94              
95 0 0         $endpoint = $endpoint->[0] if ref($endpoint) eq 'ARRAY';
96              
97 0 0         my $namespace = ref($endpoint) eq 'HASH' ? _endpoint_namespace($endpoint) : $endpoint;
98              
99 0           return $API{$namespace};
100             }
101              
102             sub _endpoint_namespace {
103 0     0     my $endpoint = shift;
104              
105 0           my $path = $endpoint->{path};
106              
107 0           my ($namespace) = $path =~ m!^([^/]+/[^/]+)!;
108 0           return $namespace;
109             }
110              
111              
112             sub documentation_url {
113 0     0 1   my $endpoint = shift;
114 0   0       my $type = shift || 'html';
115 0           my $version = shift;
116              
117 0 0         my $namespace = ref($endpoint) eq 'HASH' ? _endpoint_namespace($endpoint) : $endpoint;
118 0           my $api_info = api_info($namespace);
119 0   0       my $filename = $api_info && $api_info->{documentation_filename} || $namespace;
120              
121 0           my $url = "${DOCUMENTATION_URL}${filename}.${type}";
122              
123 0 0         $url =~ s/latest/$version/g if $version;
124              
125 0           return $url
126             }
127              
128              
129             sub package_name {
130 0     0 1   my $endpoint = shift;
131              
132 0 0         $endpoint = $endpoint->[0] if ref($endpoint) eq 'ARRAY';
133              
134 0           my $api_info = api_info($endpoint);
135 0 0         return $api_info->{package} if $api_info;
136              
137 0           my $path = $endpoint->{path};
138              
139 0           my ($name, $version) = $path =~ m!^([^/]+)/([^/]+)!;
140              
141 0           $name = ucfirst(lc($name));
142 0           $name =~ s/[^A-Za-z0-9]/_/g;
143 0           $name =~ s/_(.)/uc($1)/eg;
  0            
144              
145 0           $version =~ s/[^A-Za-z0-9]/_/g;
146 0           $version =~ s/_0$//;
147              
148 0           return "${name}::V${version}";
149             }
150              
151              
152             sub sub_name {
153 0     0 1   my $endpoint = shift;
154              
155 0           my $api_info = api_info($endpoint);
156 0           my $key = join(' ', @{$endpoint}{qw(path method)});
  0            
157 0           my $sub_name = $endpoint->{id};
158              
159 0 0         if ($api_info) {
160 0           our %SUBMAP;
161              
162 0 0         if (!defined $SUBMAP{$api_info->{id}}) {
163 0           require File::ShareDir;
164 0           my $filepath = eval { File::ShareDir::module_file(__PACKAGE__, "submap_$api_info->{id}.pl") };
  0            
165 0 0         if ($filepath) {
166 0           my $subs = do $filepath;
167 0           $SUBMAP{$api_info->{id}} = $subs;
168             }
169             }
170              
171 0 0         $sub_name = $SUBMAP{$api_info->{id}}{$key} if defined $SUBMAP{$api_info->{id}}{$key};
172             }
173              
174             # make it look perly
175 0           $sub_name =~ s/([A-Z])/'_'.lc($1)/eg;
  0            
176              
177 0           return $sub_name;
178             }
179              
180             1;
181              
182             __END__