blib/lib/GitHub/Crud.pm

filter: Statementstmt 9.2% 62 / 669 Branchbran 1.3% 4 / 302 Conditioncond 0.0% 0 / 98 MC/DCmcdc 0.0% 0 / 61 Subroutinesub 21.0% 16 / 76 Podpod 71.1% 37 / 52 totaltotal 9.4% 119 / 1258 CC 195 SCAR 44.1
CC
195
Cov
6%
new
342.0
write
275.8
specialFileData
110.0
CRAP 82.0
↑ Summary Build »
1 #!/usr/bin/perl -I/home/phil/perl/cpan/DataTableText/lib
2 #-------------------------------------------------------------------------------
3 # Create, Read, Update, Delete files, commits, issues, and web hooks on GitHub.
4 # Per: https://developer.github.com/v3/
5 # Philip R Brenan at gmail dot com, Appa Apps Ltd, 2017-2020
6 #-------------------------------------------------------------------------------
7 #podDocumentation
8 package GitHub::Crud;
9 1 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 our $VERSION = 20210519; # Upload and reindex on 21/5/8
11 1 ▶use warnings FATAL => qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Table::Text qw(:all !fileList);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Digest::SHA1 qw(sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 #use Date::Manip;
18 1 ▶use Scalar::Util qw(blessed reftype looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Encode qw(encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use utf8; # To allow utf8 constants for testing
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 0 ▶sub url { "https://api.github.com/repos" } # Github repository api url
Subroutine url: not called CC=1 SCAR=6.9
Pod undocumented
24 0 ▶sub api { "https://api.github.com/" } # Github api url
Subroutine api: not called CC=1 SCAR=6.9
Pod undocumented
25 0 ▶sub accessFolder { q(/etc/GitHubCrudPersonalAccessToken) }; # Personal access tokens are stored in a file in this folder with the name of the userid of the L<GitHub> repository
Subroutine accessFolder: not called CC=1 SCAR=6.9
Pod undocumented
26  
27 1 my %shas; # L<SHA> digests already seen - used to optimize write and delete
28  
29 sub GitHub::Crud::Response::new($$) #P Execute a request against L<GitHub> and decode the response
30 0 ▶ {my ($gitHub, $request) = @_; # Github, request string
Subroutine new: not called CC=18 SCAR=58.3
31  
32 0 my $R = bless {command=>$request}, "GitHub::Crud::Response"; # Construct the response
33  
34 0 my $r = xxx $request, qr(HTTP);
35  
36 0 $r =~ s/\r//gs; # Internet line ends
37 0 my ($http, @r) = split /\n/, $r;
38 0 ▶ while(@r > 2 and $http =~ "HTTP" and $http =~ /100/) # Continue messages
Branch: 1 branch
BranchTrueFalseTotal
if (@r > 2 and $http =~ /HTTP/u and $http =~ /100/u) 0 0 0
Condition0%0/6
@r > 2 and $http =~ /HTTP/u and $http =~ /100/u
!ll&&!rl&&r
000
@r > 2 and $http =~ /HTTP/u
!ll&&!rl&&r
000
MC/DC0%0/3
@r > 2 and $http =~ /HTTP/u and $http =~ /100/u
@r > 2 $http =~ /HTTP/u $http =~ /100/u
Truth table0%0/4
A and B and C
A= @r > 2B= $http =~ /HTTP/uC= $http =~ /100/u
ABCresult
0XX0
10X0
1100
1111
39 0 {shift @r; $http = shift @r;
40 }
41  
42 0 ▶ if ($http and $http =~ "HTTP" and $http =~ /200|201|404|409|422/)
Branch: 1 branch
BranchTrueFalseTotal
if ($http and $http =~ /HTTP/u and $http =~ /200|201|404|409|422/u) { } 0 0 0
Condition0%0/6
$http and $http =~ /HTTP/u and $http =~ /200|201|404|409|422/u
!ll&&!rl&&r
000
$http and $http =~ /HTTP/u
!ll&&!rl&&r
000
MC/DC0%0/3
$http and $http =~ /HTTP/u and $http =~ /200|201|404|409|422/u
$http $http =~ /HTTP/u $http =~ /200|201|404|409|422/u
Truth table0%0/4
A and B and C
A= $httpB= $http =~ /HTTP/uC= $http =~ /200|201|404|409|422/u
ABCresult
0XX0
10X0
1100
1111
43 0 {my $ps = 0; # Parse the response
44 0 my @data;
45 my %can;
46  
47 0 for(@r)
48 0 ▶ {if ($ps == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($ps == 0) { } 0 0 0
49 0 ▶ {if (length == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 0) { } 0 0 0
50 0 {$ps = 1;
51 }
52 else
53 0 {my ($name, $content) = split /\s*:\s*/, $_, 2; # Parse each header
54 0 $name =~ s/-/_/gs; # Translate - in names to _
55 0 ▶ if ($R->can($name))
Branch: 1 branch
BranchTrueFalseTotal
if ($R->can($name)) { } 0 0 0
56 0 {$R->$name = $content;
57 }
58 0 else {$can{$name}++} # Update list of new methods required
59 }
60 }
61 else
62 0 {push @data, $_;
63 }
64 }
65  
66 0 ▶ if (keys %can) # List of new methods required
Branch: 1 branch
BranchTrueFalseTotal
if (keys %can) 0 0 0
67 0 {lll "Add the following fields to package GitHub::Crud::Response";
68 0 say STDERR " $_=> undef," for(sort keys %can);
69 }
70  
71 0 ▶ if (@data) # Save any data
Branch: 1 branch
BranchTrueFalseTotal
if (@data) 0 0 0
72 0 {my $j = join ' ', @data;
73 0 my $p = $R->data = bless decodeJson($j), "GitHub::Crud::Response::Data";
74 0 ▶ if (ref($p) =~ m/hash/is and my $c = $p->content)
Branch: 1 branch
BranchTrueFalseTotal
if (ref($p) =~ /hash/isu and my $c = $p->content) 0 0 0
Condition0%0/3
ref($p) =~ /hash/isu and my $c = $p->content
!ll&&!rl&&r
000
MC/DC0%0/2
ref($p) =~ /hash/isu and my $c = $p->content
ref($p) =~ /hash/isu my $c = $p->content
Truth table0%0/3
A and B
A= ref($p) =~ /hash/isuB= my $c = $p->content
ABresult
0X0
100
111
75 0 {$R->content = decodeBase64($c); # Decode the data
76 }
77 }
78  
79 0 ▶ ($R->status) = split / /, $R->Status || $R->status || 200; # Save response status - github returns status == 0 when running as an action so we make it 200
Condition0%0/5
$R->Status || $R->status || 200
l!l
00
$R->Status or $R->status
l!l&&r!l&&!r
000
MC/DC0%0/2
$R->Status || $R->status || 200
$R->Status $R->status
Truth table0%0/3
A or B
A= $R->StatusB= $R->status
ABresult
1X1
011
000
80  
81 0 return $gitHub->response = $R; # Return successful response
82 }
83 else
84 0 {confess "Unexpected response from GitHub:\n$r\n$request\n"; # Confess to failure
85 }
86 }
87  
88 1 genHash(q(GitHub::Crud::Response), # Attributes describing a response from L<GitHub>.
89 Accept_Ranges => undef,
90 access_control_allow_origin => undef,
91 Access_Control_Allow_Origin => undef,
92 access_control_expose_headers => undef,
93 Access_Control_Expose_Headers => undef,
94 cache_control => undef,
95 Cache_Control => undef,
96 Connection => undef,
97 content_length => undef,
98 Content_Length => undef,
99 content_security_policy => undef,
100 Content_Security_Policy => undef,
101 content_type => undef,
102 Content_Type => undef,
103 content => undef, # The actual content of the file from L<GitHub>.
104 data => undef, # The data received from L<GitHub>, normally in L<json> format.
105 date => undef,
106 Date => undef,
107 etag => undef,
108 ETag => undef,
109 Expires => undef,
110 last_modified => undef,
111 Last_Modified => undef,
112 Link => undef,
113 Location => undef,
114 referrer_policy => undef,
115 Referrer_Policy => undef,
116 server => undef,
117 Server => undef,
118 Source_Age => undef,
119 Status => undef,
120 status => undef, # Our version of Status.
121 strict_transport_security => undef,
122 Strict_Transport_Security => undef,
123 vary => undef,
124 Vary => undef,
125 Via => undef,
126 x_accepted_oauth_scopes => undef,
127 X_Accepted_OAuth_Scopes => undef,
128 X_Cache_Hits => undef,
129 X_Cache => undef,
130 x_content_type_options => undef,
131 X_Content_Type_Options => undef,
132 X_Content_Type => undef,
133 X_Fastly_Request_ID => undef,
134 x_frame_options => undef,
135 X_Frame_Options => undef,
136 X_Geo_Block_List => undef,
137 x_github_media_type => undef,
138 X_GitHub_Media_Type => undef,
139 x_github_request_id => undef,
140 X_GitHub_Request_Id => undef,
141 x_oauth_scopes => undef,
142 X_OAuth_Scopes => undef,
143 x_ratelimit_limit => undef,
144 X_RateLimit_Limit => undef,
145 x_ratelimit_remaining => undef,
146 X_RateLimit_Remaining => undef,
147 x_ratelimit_reset => undef,
148 X_RateLimit_Reset => undef,
149 x_ratelimit_used => undef,
150 X_RateLimit_Used => undef,
151 X_Runtime_rack => undef,
152 X_Served_By => undef,
153 X_Timer => undef,
154 x_xss_protection => undef,
155 X_XSS_Protection => undef,
156 x_ratelimit_resource=> undef,
157 );
158  
159 1 genHash(q(GitHub::Crud::Response::Data), # Response from a request made to L<GitHub>.
160 command => undef,
161 content => undef,
162 documentation_url => undef,
163 download_url => undef,
164 encoding => undef,
165 git => undef,
166 git_url => undef,
167 html => undef,
168 html_url => undef,
169 _links => undef,
170 message => undef,
171 name => undef,
172 path => undef,
173 self => undef,
174 sha => undef,
175 size => undef,
176 type => undef,
177 url => undef,
178 );
179  
180 sub getSha($) #P Compute L<sha> for data after encoding any unicode characters as utf8.
181 0 ▶ {my ($data) = @_; # String possibly containing non ascii code points
Subroutine getSha: not called CC=2 SCAR=17.9
Pod undocumented
182  
183 0 my $length = length($data);
184 0 my $blob = 'blob' . " $length\0" . $data;
185 0 utf8::encode($blob);
186 0 my $r = eval{sha1_hex($blob)};
187 0 ▶ confess $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
188 0 $r
189 }
190  
191 1 if (0) # Test L<sha>
192 {my $sha = getSha("<h1>Hello World</h1>\n");
193 my $Sha = "f3e333e80d224c631f2ff51b9b9f7189ad349c15";
194 unless($sha eq $Sha)
195 {confess "Wrong SHA: $sha".
196 "Should be: $Sha";
197 }
198 confess "getSha success";
199 }
200  
201 sub shaKey($;$) #P Add a L<SHA> key to a L<url>
202 0 ▶ {my ($gitHub, $fileData) = @_; # Github, optional fileData to specify the file to use if it is not gitFile
Subroutine shaKey: not called CC=2 SCAR=17.9
Pod undocumented
203 0 ▶ filePath($gitHub->repository,
Branch: 1 branch
BranchTrueFalseTotal
$fileData ? : 0 0 0
204 $fileData ? ($fileData->path, $fileData->name) : $gitHub->gitFile)
205 }
206  
207 sub saveSha($$) #P Save the L<sha> of a file
208 0 ▶ {my ($gitHub, $fileData) = @_; # Github, file details returned by list or exists
Subroutine saveSha: not called CC=1 SCAR=6.9
Pod undocumented
209 0 $shas{$gitHub->shaKey($fileData)} = $fileData->sha;
210 }
211  
212 sub copySha($) #P Save the L<sha> of a file just read to a file just about to be written
213 0 ▶ {my ($gitHub) = @_; # Github
Subroutine copySha: not called CC=1 SCAR=6.9
Pod undocumented
214 0 $shas{$gitHub->shaKey} = $gitHub->response->data->sha;
215 }
216  
217 sub getExistingSha($) #P Get the L<sha> of a file that already exists
218 0 ▶ {my ($gitHub) = @_; # Github
Subroutine getExistingSha: not called CC=3 SCAR=24.8
Pod undocumented
219 0 my $s = $shas{$gitHub->shaKey}; # Get the L<sha> from the cache
220 0 ▶ return $s if defined $s; # A special L<sha> of 0 means the file was deleted
Branch: 1 branch
BranchTrueFalseTotal
if defined $s 0 0 0
221 0 my $r = $gitHub->exists; # Get the L<sha> of the file via exists if the file exists
222 0 ▶ return $r->sha if $r; # L<sha> of existing file
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
223 undef # Undef if no such file
224 0 }
225  
226 sub deleteSha($) #P Delete a L<sha> that is no longer valid
227 0 ▶ {my ($gitHub) = @_; # Github
Subroutine deleteSha: not called CC=1 SCAR=6.9
Pod undocumented
228 0 $shas{$gitHub->shaKey} = undef # Mark the L<sha> as deleted
229 }
230  
231 sub qm($) #P Quotemeta extended to include undef
232 0 ▶ {my ($s) = @_; # String to quote
Subroutine qm: not called CC=2 SCAR=17.9
Pod undocumented
233 0 ▶ return '' unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 0 0
234 0 $s =~ s((\'|\"|\\)) (\\$1)gs;
235 0 $s =~ s(\s) (%20)gsr; # Url encode blanks
236 }
237  
238 sub patKey($) #P Create an authorization header by locating an appropriate personal access token
239 0 ▶ {my ($gitHub) = @_; # GitHub
Subroutine patKey: not called CC=3 SCAR=24.8
Pod undocumented
240  
241 0 ▶ $gitHub->loadPersonalAccessToken unless $gitHub->personalAccessToken; # Load a personal access token if none has been supplied
Branch: 1 branch
BranchTrueFalseTotal
unless $gitHub->personalAccessToken 0 0 0
242  
243 0 ▶ if (my $pat = $gitHub->personalAccessToken) # User supplied personal access token explicitly
Branch: 1 branch
BranchTrueFalseTotal
if (my $pat = $gitHub->personalAccessToken) 0 0 0
244 0 {return "-H \"Authorization: token $pat\""
245 }
246  
247 0 confess "Personal access token required with scope \"public_repo\"". # We must have a personal access token to do anything useful!
248 " as generated on page:\nhttps://github.com/settings/tokens";
249 }
250  
251 sub refOrBranch($$) #P Add a ref or branch keyword
252 0 ▶ {my ($gitHub, $ref) = @_; # Github, whether to use ref rather than branch
Subroutine refOrBranch: not called CC=5 SCAR=34.0
Pod undocumented
253 0 my $b = $gitHub->branch;
254 0 ▶ return "?ref=$b" if $ref and $b;
Branch: 1 branch
BranchTrueFalseTotal
if $ref and $b 0 0 0
Condition0%0/3
$ref and $b
!ll&&!rl&&r
000
MC/DC0%0/2
$ref and $b
$ref $b
Truth table0%0/3
A and B
A= $refB= $b
ABresult
0X0
100
111
255 0 ▶ return "?branch=$b" if !$ref and $b;
Branch: 1 branch
BranchTrueFalseTotal
if not $ref and $b 0 0 0
Condition0%0/3
not $ref and $b
!ll&&!rl&&r
000
MC/DC0%0/2
not $ref and $b
not $ref $b
Truth table0%0/3
not(A) and B
A= not $refB= $b
ABresult
0X0
100
111
256 0 ''
257 }
258  
259 sub gitHub(%) #P Create a test L<GitHub> object
260 0 ▶ {my (%options) = @_; # Options
Subroutine gitHub: not called CC=1 SCAR=6.9
Pod undocumented
261 0 GitHub::Crud::new
262 (userid => q(philiprbrenan),
263 repository => q(aaa),
264 confessOnFailure => 1,
265 @_);
266 }
267  
268 #D1 Constructor # Create a L<github> object with the specified attributes describing the interface with L<github>.
269  
270 sub new(@) # Create a new L<GitHub> object with attributes as described at: L<GitHub::Crud Definition>.
271 0 ▶ {my (%attributes) = @_; # Attribute values
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
272  
273 0 my $curl = qx(curl -V); # Check Curl
274 0 ▶ if ($curl =~ /command not found/)
Branch: 1 branch
BranchTrueFalseTotal
if ($curl =~ /command not found/u) 0 0 0
275 0 {confess "Command curl not found"
276 }
277  
278 0 my $g = genHash(__PACKAGE__, # Attributes describing the interface with L<github>.
279 body => undef, #I The body of an issue.
280 branch => undef, #I Branch name (you should create this branch first) or omit it for the default branch which is usually 'master'.
281 confessOnFailure => undef, #I Confess to any failures
282 failed => undef, # Defined if the last request to L<GitHub> failed else B<undef>.
283 fileList => undef, # Reference to an array of files produced by L<list|/list>.
284 gitFile => undef, #I File name on L<GitHub> - this name can contain '/'. This is the file to be read from, written to, copied from, checked for existence or deleted.
285 gitFolder => undef, #I Folder name on L<GitHub> - this name can contain '/'.
286 message => undef, #I Optional commit message
287 nonRecursive => undef, #I Fetch only one level of files with L<list>.
288 personalAccessToken => undef, #I A personal access token with scope "public_repo" as generated on page: https://github.com/settings/tokens.
289 personalAccessTokenFolder => accessFolder, #I The folder into which to save personal access tokens. Set to q(/etc/GitHubCrudPersonalAccessToken) by default.
290 private => undef, #I Whether the repository being created should be private or not.
291 readData => undef, # Data produced by L<read|/read>.
292 repository => undef, #I The name of the repository to be worked on minus the userid - you should create this repository first manually.
293 response => undef, # A reference to L<GitHub>'s response to the latest request.
294 secret => undef, #I The secret for a web hook - this is created by the creator of the web hook and remembered by L<GitHub>,
295 title => undef, #I The title of an issue.
296 webHookUrl => undef, #I The url for a web hook.
297 userid => undef, #I Userid on L<GitHub> of the repository to be worked on.
298 );
299  
300 0 $g->$_ = $attributes{$_} for sort keys %attributes;
301  
302 0 $g
303 }
304  
305 #D1 Files # File actions on the contents of L<GitHub> repositories.
306  
307 sub list($) # List all the files contained in a L<GitHub> repository or all the files below a specified folder in the repository.\mRequired attributes: L<userid|/userid>, L<repository|/repository>.\mOptional attributes: L<gitFolder|/gitFolder>, L<refOrBranch|/refOrBranch>, L<nonRecursive|/nonRecursive>, L<patKey|/patKey>.\mUse the L<gitFolder|/gitFolder> parameter to specify the folder to start the list from, by default, the listing will start at the root folder of your repository.\mUse the L<nonRecursive|/nonRecursive> option if you require only the files in the start folder as otherwise all the folders in the start folder will be listed as well which might take some time.\mIf the list operation is successful, L<failed|/failed> is set to false and L<fileList|/fileList> is set to refer to an array of the file names found.\mIf the list operation fails then L<failed|/failed> is set to true and L<fileList|/fileList> is set to refer to an empty array.\mReturns the list of file names found or empty list if no files were found.
308 0 ▶ {my ($gitHub) = @_; # GitHub
Subroutine list: not called CC=9 SCAR=45.0
Pod documented
309 my $r = sub # Get contents
310 0 ▶ {my $user = qm $gitHub->userid; $user or confess "userid required";
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
311 0 ▶ my $repo = qm $gitHub->repository; $repo or confess "repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repo or confess("repository required") 0 0 0
312 0 ▶ my $path = qm $gitHub->gitFolder || '';
Condition0%0/2
&qm(scalar $gitHub->gitFolder) || ''
l!l
00
MC/DC0%0/1
&qm(scalar $gitHub->gitFolder) || ''
&qm(scalar $gitHub->gitFolder)
Truth table0%0/2
A
A= &qm(scalar $gitHub->gitFolder)
Aresult
11
00
313 0 my $bran = qm $gitHub->refOrBranch(1);
314 0 my $pat = $gitHub->patKey(0);
315 0 my $url = url;
316 0 my $s = filePath
317 ("curl -si $pat $url", $user, $repo, qq(contents), $path.$bran);
318 0 GitHub::Crud::Response::new($gitHub, $s);
319 0 }->();
320  
321 0 my $failed = $gitHub->failed = $r->status != 200; # Check response code
322 0 ▶ $failed and $gitHub->confessOnFailure and confess dump($gitHub); # Confess to any failure if so requested
Branch: 1 branch
BranchTrueFalseTotal
$failed and $gitHub->confessOnFailure and confess(GitHub::Crud::dump($gitHub)) 0 0 0
Condition0%0/3
$failed and $gitHub->confessOnFailure
!ll&&!rl&&r
000
MC/DC0%0/2
$failed and $gitHub->confessOnFailure
$failed $gitHub->confessOnFailure
Truth table0%0/3
A and B
A= $failedB= $gitHub->confessOnFailure
ABresult
0X0
100
111
323  
324 0 $gitHub->fileList = [];
325 0 ▶ if (!$failed and reftype($r->data) =~ m(array)i) # Array of file details
Branch: 1 branch
BranchTrueFalseTotal
if (not $failed and builtin::reftype($r->data) =~ /array/iu) 0 0 0
Condition0%0/3
not $failed and builtin::reftype($r->data) =~ /array/iu
!ll&&!rl&&r
000
MC/DC0%0/2
not $failed and builtin::reftype($r->data) =~ /array/iu
not $failed builtin::reftype($r->data) =~ /array/iu
Truth table0%0/3
not(A) and B
A= not $failedB= builtin::reftype($r->data) =~ /array/iu
ABresult
0X0
100
111
326 0 {for(@{$r->data}) # Objectify and save L<sha> digests from file descriptions retrieved by this call
327 0 {bless $_, "GitHub::Crud::Response::Data";
328 0 saveSha($gitHub, $_);
329 }
330  
331 0 ▶ my $path = $gitHub->gitFolder || '';
Condition0%0/2
$gitHub->gitFolder || ''
l!l
00
MC/DC0%0/1
$gitHub->gitFolder || ''
$gitHub->gitFolder
Truth table0%0/2
A
A= $gitHub->gitFolder
Aresult
11
00
332 0 my @d = map{filePath $path, $_->name} grep {$_->type eq "dir"} @{$r->data};# Folders
333 0 my @f = map{filePath $path, $_->name} grep {$_->type eq "file"} @{$r->data};# Files
334  
335 0 ▶ unless($gitHub->nonRecursive) # Get the contents of sub folders unless otherwise specified
Branch: 1 branch
BranchTrueFalseTotal
unless ($gitHub->nonRecursive) 0 0 0
336 0 {for my $d(@d)
337 0 {my $p = $gitHub->gitFolder = $d;
338 0 push @f, $gitHub->list;
339 }
340 }
341 0 $gitHub->gitFolder = $path; # Restore path supplied by the user
342 0 $gitHub->fileList = [@f]; # List of files not directories
343 }
344 0 @{$gitHub->fileList}
345 }
346  
347 sub specialFileData($) # Do not encode or decode data with a known file signature
348 0 ▶ {my ($d) = @_; # String to check
Subroutine specialFileData: not called CC=10 SCAR=47.0
Pod documented
349 0 my $h = '';
350 0 ▶ if ($d and length($d) > 8) # Read file magic number
Branch: 1 branch
BranchTrueFalseTotal
if ($d and length $d > 8) 0 0 0
Condition0%0/3
$d and length $d > 8
!ll&&!rl&&r
000
MC/DC0%0/2
$d and length $d > 8
$d length $d > 8
Truth table0%0/3
A and B
A= $dB= length $d > 8
ABresult
0X0
100
111
351 0 {for my $e(0..7)
352 0 {$h .= sprintf("%x", ord(substr($d, $e, 1)));
353 }
354 0 ▶ return 1 if $h =~ m(\A504b)i; # PK Zip
Branch: 1 branch
BranchTrueFalseTotal
if $h =~ /\A504b/iu 0 0 0
355 0 ▶ return 1 if $h =~ m(\Ad0cf11e0)i; # OLE files
Branch: 1 branch
BranchTrueFalseTotal
if $h =~ /\Ad0cf11e0/iu 0 0 0
356 0 ▶ return 1 if $h =~ m(\Affd8ff)i; # Jpg
Branch: 1 branch
BranchTrueFalseTotal
if $h =~ /\Affd8ff/iu 0 0 0
357 0 ▶ return 1 if $h =~ m(\A89504e470d0a1a0a)i; # Png
Branch: 1 branch
BranchTrueFalseTotal
if $h =~ /\A89504e470d0a1a0a/iu 0 0 0
358 0 ▶ return 1 if $h =~ m(\A4D546864)i; # Midi
Branch: 1 branch
BranchTrueFalseTotal
if $h =~ /\A4D546864/iu 0 0 0
359 0 ▶ return 1 if $h =~ m(\A49443340)i; # Mp3
Branch: 1 branch
BranchTrueFalseTotal
if $h =~ /\A49443340/iu 0 0 0
360 }
361 0 # Not a special file
362 0 }
363  
364 sub read($;$) # Read data from a file on L<GitHub>.\mRequired attributes: L<userid|/userid>, L<repository|/repository>.\mOptional attributes: L<gitFile|/gitFile> = the file to read, L<refOrBranch|/refOrBranch>, L<patKey|/patKey>.\mIf the read operation is successful, L<failed|/failed> is set to false and L<readData|/readData> is set to the data read from the file.\mIf the read operation fails then L<failed|/failed> is set to true and L<readData|/readData> is set to B<undef>.\mReturns the data read or B<undef> if no file was found.
365 0 ▶ {my ($gitHub, $File) = @_; # GitHub, file o read if not specified in gitFile
Subroutine read: not called CC=9 SCAR=45.0
Pod documented
366  
367 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
368 0 ▶ my $repo = qm $gitHub->repository; $repo or confess "repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repo or confess("repository required") 0 0 0
369 0 ▶ my $file = qm($File//$gitHub->gitFile); $file or confess "gitFile required";
Branch: 1 branch
BranchTrueFalseTotal
$file or confess("gitFile required") 0 0 0
Condition0%0/3
$File // $gitHub->gitFile
l!l&&r!l&&!r
000
MC/DC0%0/2
$File // $gitHub->gitFile
$File $gitHub->gitFile
Truth table0%0/3
A // B
A= $FileB= $gitHub->gitFile
ABresult
1X1
011
000
370 0 my $bran = qm $gitHub->refOrBranch(1);
371 0 my $pat = $gitHub->patKey(0);
372  
373 0 my $url = url;
374 0 my $s = filePath(qq(curl -si $pat $url),
375 $user, $repo, qq(contents), $file.$bran);
376 0 my $r = GitHub::Crud::Response::new($gitHub, $s); # Get response from GitHub
377 0 my $failed = $gitHub->failed = $r->status != 200; # Check response code
378 0 ▶ $failed and $gitHub->confessOnFailure and confess dump($gitHub); # Confess to any failure if so requested
Branch: 1 branch
BranchTrueFalseTotal
$failed and $gitHub->confessOnFailure and confess(GitHub::Crud::dump($gitHub)) 0 0 0
Condition0%0/3
$failed and $gitHub->confessOnFailure
!ll&&!rl&&r
000
MC/DC0%0/2
$failed and $gitHub->confessOnFailure
$failed $gitHub->confessOnFailure
Truth table0%0/3
A and B
A= $failedB= $gitHub->confessOnFailure
ABresult
0X0
100
111
379  
380 0 ▶ if ($failed) # Decode data unless read failed
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) { } 0 0 0
381 0 {$gitHub->readData = undef;
382 }
383 else # Decode data
384 0 {my $d = decodeBase64($r->data->content);
385 0 ▶ $gitHub->readData = specialFileData($d) ? $d : decode "UTF8", $d; # Convert to utf unless a known file format
Branch: 1 branch
BranchTrueFalseTotal
&specialFileData($d) ? : 0 0 0
386 }
387  
388 0 $gitHub->readData
389 }
390  
391 sub write($$;$) # Write utf8 data into a L<GitHub> file.\mRequired attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>. Either specify the target file on:<github> using the L<gitFile|/gitFile> attribute or supply it as the third parameter. Returns B<true> on success else L<undef>.
392 0 ▶ {my ($gitHub, $data, $File) = @_; # GitHub object, data to be written, optionally the name of the file on github
Subroutine write: not called CC=17 SCAR=56.2
Pod documented
393  
394 0 ▶ unless($data) # No data supplied so delete the file
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
395 0 ▶ {if ($File)
Branch: 1 branch
BranchTrueFalseTotal
if ($File) { } 0 0 0
396 0 {my $file = $gitHub->file;
397 0 $gitHub->file = $File;
398 0 $gitHub->delete;
399 0 $gitHub->file = $file;
400 }
401 else
402 0 {$gitHub->delete;
403 }
404 0 return 'empty'; # Success
405 }
406  
407 0 my $pat = $gitHub->patKey(1);
408 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
409 0 ▶ my $repo = qm $gitHub->repository; $repo or confess "repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repo or confess("repository required") 0 0 0
410 0 ▶ my $file = qm($File//$gitHub->gitFile); $file or confess "gitFile required";
Branch: 1 branch
BranchTrueFalseTotal
$file or confess("gitFile required") 0 0 0
Condition0%0/3
$File // $gitHub->gitFile
l!l&&r!l&&!r
000
MC/DC0%0/2
$File // $gitHub->gitFile
$File $gitHub->gitFile
Truth table0%0/3
A // B
A= $FileB= $gitHub->gitFile
ABresult
1X1
011
000
411 0 ▶ my $bran = qm $gitHub->refOrBranch(0) || '?';
Condition0%0/2
&qm(scalar $gitHub->refOrBranch(0)) || '?'
l!l
00
MC/DC0%0/1
&qm(scalar $gitHub->refOrBranch(0)) || '?'
&qm(scalar $gitHub->refOrBranch(0))
Truth table0%0/2
A
A= &qm(scalar $gitHub->refOrBranch(0))
Aresult
11
00
412 0 my $mess = qm $gitHub->message;
413  
414 0 ▶ if (!specialFileData($data)) # Send the data as utf8 unless it is a special file
Branch: 1 branch
BranchTrueFalseTotal
unless (&specialFileData($data)) 0 0 0
415 1 ▶ {use Encode 'encode';
Subroutine BEGIN: called CC=1 SCAR=0.0
416 0 $data = encode('UTF-8', $data);
417 }
418  
419 0 my $url = url;
420 0 my $save = $gitHub->gitFile; # Save any existing file name as we might need to update it to get the sha if the target file was supplied as a parameter to this sub
421 0 ▶ $gitHub->gitFile = $File if $File; # Set target file name so we can get its sha
Branch: 1 branch
BranchTrueFalseTotal
if $File 0 0 0
422 0 ▶ my $s = $gitHub->getExistingSha || getSha($data); # Get the L<sha> of the file if the file exists
Condition0%0/3
$gitHub->getExistingSha || &getSha($data)
l!l&&r!l&&!r
000
MC/DC0%0/2
$gitHub->getExistingSha || &getSha($data)
$gitHub->getExistingSha &getSha($data)
Truth table0%0/3
A || B
A= $gitHub->getExistingShaB= &getSha($data)
ABresult
1X1
011
000
423 0 $gitHub->gitFile = $save; # Restore file name
424 0 ▶ my $sha = $s ? ', "sha": "'. $s .'"' : ''; # L<sha> of existing file or blank string if no existing file
Branch: 1 branch
BranchTrueFalseTotal
$s ? : 0 0 0
425  
426 # if ($s and my $S = getSha($data)) # L<sha> of new data
427 # {if ($s eq $S) # Duplicate if the L<sha>s match
428 # {$gitHub->failed = undef;
429 # return 1;
430 # }
431 # }
432  
433 0 my $denc = encodeBase64($data) =~ s/\n//gsr;
434  
435 my $branch = sub # It seems we must put the branch in the json file though the documentation seems to imply it can go in the url or the json
436 0 ▶ {my $b = $gitHub->branch;
Subroutine __ANON__: not called CC=2 SCAR=17.9
437 0 ▶ return qq(, "branch" : "$b") if $b;
Branch: 1 branch
BranchTrueFalseTotal
if $b 0 0 0
438 0 q()
439 0 }->();
440  
441 0 my $j = qq({"message": "$mess", "content": "$denc" $sha $branch});
442 0 my $t = writeFile(undef, $j); # Write encoded content to temporary file
443 0 my $d = qq(-d @).$t;
444 0 my $u = filePath($url, $user, $repo, qw(contents), $file.$bran);
445 0 my $c = qq(curl -si -X PUT $pat $u $d); # Curl command
446 0 my $r = GitHub::Crud::Response::new($gitHub, $c); # Execute command to create response
447 0 unlink $t; # Cleanup
448  
449 0 my $status = $r->status; # Check response code
450 0 ▶ my $success = $status == 200 ? 'updated' : $status == 201 ? 'created' : undef;# Updated, created
Branch: 2 branches
BranchTrueFalseTotal
$status == 200 ? : 0 0 0
$status == 201 ? : 0 0 0
451 0 ▶ $gitHub->failed = $success ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
452 0 ▶ !$success and $gitHub->confessOnFailure and confess dump($gitHub); # Confess to any failure if so requested
Branch: 1 branch
BranchTrueFalseTotal
not $success and $gitHub->confessOnFailure and confess(GitHub::Crud::dump($gitHub)) 0 0 0
Condition0%0/3
not $success and $gitHub->confessOnFailure
!ll&&!rl&&r
000
MC/DC0%0/2
not $success and $gitHub->confessOnFailure
not $success $gitHub->confessOnFailure
Truth table0%0/3
not(A) and B
A= not $successB= $gitHub->confessOnFailure
ABresult
0X0
100
111
453  
454 0 $success # Return true on success
455 }
456  
457 sub readBlob($$) # Read a L<blob> from L<GitHub>.\mRequired attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>. Returns the content of the L<blob> identified by the specified L<sha>.
458 0 ▶ {my ($gitHub, $sha) = @_; # GitHub object, data to be written
Subroutine readBlob: not called CC=8 SCAR=42.8
Pod documented
459 0 ▶ defined($sha) or confess "sha required";
Branch: 1 branch
BranchTrueFalseTotal
defined $sha or confess("sha required") 0 0 0
460  
461 0 my $pat = $gitHub->patKey(1);
462 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
463 0 ▶ my $repo = qm $gitHub->repository; $repo or confess "repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repo or confess("repository required") 0 0 0
464 0 my $url = url;
465  
466 0 my $u = filePath($url, $user, $repo, qw(git blobs), $sha); # Url
467 0 my $c = qq(curl -si $pat $u); # Curl command
468 0 my $r = GitHub::Crud::Response::new($gitHub, $c); # Execute command to create response
469  
470 0 my $status = $r->status; # Check response code
471 0 my $success = $status == 200;
472 0 ▶ $gitHub->failed = $success ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
473 0 ▶ !$success and $gitHub->confessOnFailure and confess dump($gitHub); # Confess to any failure if so requested
Branch: 1 branch
BranchTrueFalseTotal
not $success and $gitHub->confessOnFailure and confess(GitHub::Crud::dump($gitHub)) 0 0 0
Condition0%0/3
not $success and $gitHub->confessOnFailure
!ll&&!rl&&r
000
MC/DC0%0/2
not $success and $gitHub->confessOnFailure
not $success $gitHub->confessOnFailure
Truth table0%0/3
not(A) and B
A= not $successB= $gitHub->confessOnFailure
ABresult
0X0
100
111
474  
475 0 ▶ $success ? decodeBase64($gitHub->response->data->content) : undef # Return content on success else undef
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
476 }
477  
478 sub writeBlob($$) # Write data into a L<GitHub> as a L<blob> that can be referenced by future commits.\mRequired attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>. Returns the L<sha> of the created L<blob> or L<undef> in a failure occurred.
479 0 ▶ {my ($gitHub, $data) = @_; # GitHub object, data to be written
Subroutine writeBlob: not called CC=10 SCAR=47.0
Pod documented
480 0 ▶ defined($data) or confess "binary data required";
Branch: 1 branch
BranchTrueFalseTotal
defined $data or confess("binary data required") 0 0 0
481  
482 0 my $pat = $gitHub->patKey(1);
483 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
484 0 ▶ my $repo = qm $gitHub->repository; $repo or confess "repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repo or confess("repository required") 0 0 0
485 0 my $url = url;
486  
487 0 my $denc = encodeBase64($data) =~ s/\n//gsr;
488 0 my $t = writeTempFile(qq({"content": "$denc", "encoding" : "base64"})); # Write encoded content to temporary file
489 0 my $d = qq(-d @).$t;
490 0 my $u = filePath($url, $user, $repo, qw(git blobs));
491 0 my $c = qq(curl -si -X POST $pat $u $d); # Curl command
492 0 my $r = GitHub::Crud::Response::new($gitHub, $c); # Execute command to create response
493 0 unlink $t; # Cleanup
494  
495 0 my $status = $r->status; # Check response code
496 0 ▶ my $success = $status == 200 ? 'updated' : $status == 201 ? 'created' : undef;# Updated, created
Branch: 2 branches
BranchTrueFalseTotal
$status == 200 ? : 0 0 0
$status == 201 ? : 0 0 0
497 0 ▶ $gitHub->failed = $success ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
498 0 ▶ !$success and $gitHub->confessOnFailure and confess dump($gitHub); # Confess to any failure if so requested
Branch: 1 branch
BranchTrueFalseTotal
not $success and $gitHub->confessOnFailure and confess(GitHub::Crud::dump($gitHub)) 0 0 0
Condition0%0/3
not $success and $gitHub->confessOnFailure
!ll&&!rl&&r
000
MC/DC0%0/2
not $success and $gitHub->confessOnFailure
not $success $gitHub->confessOnFailure
Truth table0%0/3
not(A) and B
A= not $successB= $gitHub->confessOnFailure
ABresult
0X0
100
111
499  
500 0 ▶ $success ? $gitHub->response->data->sha : undef # Return sha of blob on success
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
501 }
502  
503 sub copy($$) # Copy a source file from one location to another target location in your L<GitHub> repository, overwriting the target file if it already exists.\mRequired attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>, L<gitFile|/gitFile> = the file to be copied.\mOptional attributes: L<refOrBranch|/refOrBranch>.\mIf the write operation is successful, L<failed|/failed> is set to false otherwise it is set to true.\mReturns B<updated> if the write updated the file, B<created> if the write created the file else B<undef> if the write failed.
504 0 ▶ {my ($gitHub, $target) = @_; # GitHub object, the name of the file to be created
Subroutine copy: not called CC=3 SCAR=24.8
Pod documented
505 0 ▶ defined($target) or confess "Specify the name of the file to be copied to";
Branch: 1 branch
BranchTrueFalseTotal
defined $target or confess("Specify the name of the file to be copied to") 0 0 0
506 0 my $r = $gitHub->read; # Read the content of the source file
507 0 ▶ if (defined $r)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r) 0 0 0
508 0 {my $file = $gitHub->gitFile; # Save current source file
509 0 my $sha = $gitHub->response->data->sha; # L<sha> of last file read
510 0 $gitHub->gitFile = $target; # Set target file as current file
511 0 my $R = $gitHub->write($r); # Write content to target file
512 0 $gitHub->copySha; # Copy the L<sha> from the file just read
513 0 $gitHub->gitFile = $file; # Restore source file
514 0 return $R; # Return response from write
515 }
516 undef # Failed
517 0 }
518  
519 sub exists($) # Test whether a file exists on L<GitHub> or not and returns an object including the B<sha> and B<size> fields if it does else L<undef>.\mRequired attributes: L<userid|/userid>, L<repository|/repository>, L<gitFile|/gitFile> file to test.\mOptional attributes: L<refOrBranch|/refOrBranch>, L<patKey|/patKey>.
520 0 ▶ {my ($gitHub) = @_; # GitHub object
Subroutine exists: not called CC=6 SCAR=37.4
Pod documented
521 0 my @file = split /\//, $gitHub->gitFile;
522 0 ▶ confess "gitFile required to name the file to be checked" unless @file;
Branch: 1 branch
BranchTrueFalseTotal
unless @file 0 0 0
523 0 pop @file;
524 0 my $folder = $gitHub->gitFolder;
525 0 my $nonRecursive = $gitHub->nonRecursive;
526 0 $gitHub->gitFolder = filePath(@file);
527 0 $gitHub->nonRecursive = 1;
528 0 my $r = $gitHub->list; # Get a file listing
529 0 $gitHub->gitFolder = $folder;
530 0 $gitHub->nonRecursive = $nonRecursive;
531  
532 0 ▶ if (!$gitHub->failed and reftype($gitHub->response->data) =~ m(array)i) # Look for requested file in file listing
Branch: 1 branch
BranchTrueFalseTotal
if (not $gitHub->failed and builtin::reftype($gitHub->response->data) =~ /array/iu) 0 0 0
Condition0%0/3
not $gitHub->failed and builtin::reftype($gitHub->response->data) =~ /array/iu
!ll&&!rl&&r
000
MC/DC0%0/2
not $gitHub->failed and builtin::reftype($gitHub->response->data) =~ /array/iu
not $gitHub->failed builtin::reftype($gitHub->response->data) =~ /array/iu
Truth table0%0/3
not(A) and B
A= not $gitHub->failedB= builtin::reftype($gitHub->response->data) =~ /array/iu
ABresult
0X0
100
111
533 0 {for(@{$gitHub->response->data})
534 0 ▶ {return $_ if $_->path eq $gitHub->gitFile;
Branch: 1 branch
BranchTrueFalseTotal
if $_->path eq $gitHub->gitFile 0 0 0
535 }
536 }
537 undef
538 0 }
539  
540 sub rename($$) # Rename a source file on L<GitHub> if the target file name is not already in use.\mRequired attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>, L<gitFile|/gitFile> = the file to be renamed.\mOptional attributes: L<refOrBranch|/refOrBranch>.\mReturns the new name of the file B<renamed> if the rename was successful else B<undef> if the rename failed.
541 0 ▶ {my ($gitHub, $target) = @_; # GitHub object, the new name of the file
Subroutine rename: not called CC=4 SCAR=30.0
Pod documented
542 0 my $file = $gitHub->gitFile;
543 0 $gitHub->gitFile = $target;
544 0 ▶ return undef if $gitHub->exists;
Branch: 1 branch
BranchTrueFalseTotal
if $gitHub->exists 0 0 0
545 0 $gitHub->gitFile = $file;
546 0 $gitHub->copy($target);
547 0 $gitHub->gitFile = $target;
548 0 ▶ if ($gitHub->exists)
Branch: 1 branch
BranchTrueFalseTotal
if ($gitHub->exists) 0 0 0
549 0 {$gitHub->gitFile = $file;
550 0 ▶ return $target if $gitHub->delete;
Branch: 1 branch
BranchTrueFalseTotal
if $gitHub->delete 0 0 0
551 0 confess "Failed to delete source file $file";
552 }
553 undef
554 0 }
555  
556 sub delete($) # Delete a file from L<GitHub>.\mRequired attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>, L<gitFile|/gitFile> = the file to be deleted.\mOptional attributes: L<refOrBranch|/refOrBranch>.\mIf the delete operation is successful, L<failed|/failed> is set to false otherwise it is set to true.\mReturns true if the delete was successful else false.
557 0 ▶ {my ($gitHub) = @_; # GitHub object
Subroutine delete: not called CC=10 SCAR=47.0
Pod documented
558  
559 0 my $pat = $gitHub->patKey(1);
560 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
561 0 ▶ my $repo = qm $gitHub->repository; $repo or confess "repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repo or confess("repository required") 0 0 0
562 0 ▶ my $file = qm $gitHub->gitFile; $file or confess "file to delete required";
Branch: 1 branch
BranchTrueFalseTotal
$file or confess("file to delete required") 0 0 0
563 0 my $bran = qm $gitHub->refOrBranch(0);
564 0 my $url = url;
565  
566 0 my $s = $gitHub->getExistingSha; # L<sha> of existing file or undef
567 0 ▶ return 2 unless $s; # File already deleted
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 0 0
568 0 my $sha = ' -d \'{"message": "", "sha": "'. $s .'"}\'';
569 0 my $u = filePath($url, $user, $repo, qw(contents), $file.$bran.$sha);
570 0 my $d = "curl -si -X DELETE $pat $u";
571 0 my $r = GitHub::Crud::Response::new($gitHub, $d);
572 0 my $success = $r->status == 200; # Check response code
573 0 ▶ $gitHub->deleteSha if $success; # The L<sha> is no longer valid
Branch: 1 branch
BranchTrueFalseTotal
if $success 0 0 0
574 0 ▶ $gitHub->failed = $success ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
575 0 ▶ !$success and $gitHub->confessOnFailure and confess dump($gitHub); # Confess to any failure if so requested
Branch: 1 branch
BranchTrueFalseTotal
not $success and $gitHub->confessOnFailure and confess(GitHub::Crud::dump($gitHub)) 0 0 0
Condition0%0/3
not $success and $gitHub->confessOnFailure
!ll&&!rl&&r
000
MC/DC0%0/2
not $success and $gitHub->confessOnFailure
not $success $gitHub->confessOnFailure
Truth table0%0/3
not(A) and B
A= not $successB= $gitHub->confessOnFailure
ABresult
0X0
100
111
576 0 ▶ $success ? 1 : undef # Return true on success
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
577 }
578  
579 #D1 Repositories # Perform actions on L<github> repositories.
580  
581 sub getRepository($) # Get the overall details of a repository
582 0 ▶ {my ($gitHub) = @_; # GitHub object
Subroutine getRepository: not called CC=5 SCAR=34.0
Pod documented
583  
584 0 my $pat = $gitHub->patKey(1);
585 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
586 0 ▶ my $repo = qm $gitHub->repository; $repo or confess "repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repo or confess("repository required") 0 0 0
587 0 my $url = url;
588  
589 0 my $c = qq(curl -si $pat $url/$user/$repo);
590 0 my $r = GitHub::Crud::Response::new($gitHub, $c);
591 0 my $success = $r->status == 200; # Check response code
592 0 ▶ !$success and $gitHub->confessOnFailure and confess dump([$gitHub, $c]); # Confess to any failure if so requested
Branch: 1 branch
BranchTrueFalseTotal
not $success and $gitHub->confessOnFailure and confess(GitHub::Crud::dump([$gitHub, $c])) 0 0 0
Condition0%0/3
not $success and $gitHub->confessOnFailure
!ll&&!rl&&r
000
MC/DC0%0/2
not $success and $gitHub->confessOnFailure
not $success $gitHub->confessOnFailure
Truth table0%0/3
not(A) and B
A= not $successB= $gitHub->confessOnFailure
ABresult
0X0
100
111
593  
594 0 $r
595 }
596  
597 sub listCommits($) # List all the commits in a L<GitHub> repository.\mRequired attributes: L<userid|/userid>, L<repository|/repository>.
598 0 ▶ {my ($gitHub) = @_; # GitHub object
Subroutine listCommits: not called CC=5 SCAR=34.0
Pod documented
599  
600 0 my $pat = $gitHub->patKey(1);
601 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
602 0 ▶ my $repo = qm $gitHub->repository; $repo or confess "repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repo or confess("repository required") 0 0 0
603 0 my $url = url;
604  
605 0 my $c = qq(curl -si $pat $url/$user/$repo/branches);
606  
607 0 my $r = GitHub::Crud::Response::new($gitHub, $c);
608 0 my $success = $r->status == 200; # Check response code
609 0 ▶ !$success and $gitHub->confessOnFailure and confess dump($gitHub); # Confess to any failure if so requested
Branch: 1 branch
BranchTrueFalseTotal
not $success and $gitHub->confessOnFailure and confess(GitHub::Crud::dump($gitHub)) 0 0 0
Condition0%0/3
not $success and $gitHub->confessOnFailure
!ll&&!rl&&r
000
MC/DC0%0/2
not $success and $gitHub->confessOnFailure
not $success $gitHub->confessOnFailure
Truth table0%0/3
not(A) and B
A= not $successB= $gitHub->confessOnFailure
ABresult
0X0
100
111
610  
611 0 $r
612 }
613  
614 sub listCommitShas($) # Create {commit name => sha} from the results of L<listCommits>.
615 0 ▶ {my ($commits) = @_; # Commits from L<listCommits>
Subroutine listCommitShas: not called CC=2 SCAR=17.9
Pod documented
616  
617 0 ▶ return undef unless my $data = $commits->data; # Commits array
Branch: 1 branch
BranchTrueFalseTotal
unless my $data = $commits->data 0 0 0
618 0 {map {$$_{name} => $$_{commit}{sha}} @$data} # Commits hash
619 }
620  
621 sub writeCommit($$@) # Write all the files in a B<$folder> (or just the the named files) into a L<GitHub> repository in parallel as a commit on the specified branch.\mRequired attributes: L<userid|/userid>, L<repository|/repository>, L<refOrBranch|/refOrBranch>.
622 0 ▶ {my ($gitHub, $folder, @files) = @_; # GitHub object, file prefix to remove, files to write
Subroutine writeCommit: not called CC=8 SCAR=42.8
Pod documented
623  
624 0 ▶ -d $folder or confess "No such folder"; # Folder does not exist
Branch: 1 branch
BranchTrueFalseTotal
-d $folder or confess("No such folder") 0 0 0
625  
626 0 my $pat = $gitHub->patKey(1);
627 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
628 0 ▶ my $repo = qm $gitHub->repository; $repo or confess "repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repo or confess("repository required") 0 0 0
629 0 ▶ my $bran = $gitHub->branch; $bran or confess "branch required";
Branch: 1 branch
BranchTrueFalseTotal
$bran or confess("branch required") 0 0 0
630 0 my $url = url;
631  
632 my @sha = processFilesInParallel sub # Create blobs for each file
633 0 ▶ {my ($source) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
634 0 my $target = $gitHub->gitFile = swapFilePrefix($source, $folder);
635 0 [$source, $target, $gitHub->writeBlob(readBinaryFile($source))]
636 0 ▶ }, undef, @files ? @files : searchDirectoryTreesForMatchingFiles($folder);
Branch: 1 branch
BranchTrueFalseTotal
@files ? : 0 0 0
637  
638 my $tree = sub # Create the tree
639 0 ▶ {my @t;
Subroutine __ANON__: not called CC=3 SCAR=24.8
640 0 for my $f(@sha) # Load files into a tree
641 0 {my ($s, $t, $b) = @$f;
642 0 push @t, <<END;
643 {"path" : "$t",
644 "mode" : "100644",
645 "type" : "blob",
646 "sha" : "$b"
647 }
648 END
649 }
650  
651 0 my $t = join ",\n", @t; # Assemble tree
652 0 my $j = qq({"tree" : [$t]}); # Json describing tree
653 0 my $f = writeTempFile($j); # Write Json
654 0 my $c = qq(curl -si -X POST $pat -d \@$f $url/$user/$repo/git/trees);
655  
656 0 my $r = GitHub::Crud::Response::new($gitHub, $c);
657 0 my $success = $r->status == 201; # Check response code
658 0 unlink $f; # Cleanup
659  
660 0 ▶ $success or confess "Unable to create tree: ".dump($r);
Branch: 1 branch
BranchTrueFalseTotal
$success or confess('Unable to create tree: ' . GitHub::Crud::dump($r)) 0 0 0
661  
662 0 $r
663 0 }->();
664  
665 my $parents = sub # Prior commits
666 0 ▶ {my %c = listCommitShas $gitHub->listCommits;
Subroutine __ANON__: not called CC=2 SCAR=17.9
667 0 my $b = $gitHub->branch;
668 0 ▶ return '' unless my $s = $c{$b};
Branch: 1 branch
BranchTrueFalseTotal
unless my $s = $c{$b} 0 0 0
669 0 qq(, "parents" : ["$s"])
670 0 }->();
671  
672 my $commit = sub # Create a commit to hold the tree
673 0 ▶ {my $s = $tree->data->sha;
Subroutine __ANON__: not called CC=2 SCAR=17.9
674 0 my $d = dateTimeStamp;
675 0 my $j = <<END;
676 { "message" : "Committed by GitHub::Crud on: $d"
677 , "tree" : "$s"
678 $parents
679 }
680 END
681 0 my $f = writeFile(undef, $j); # Write json
682  
683 0 my $c = qq(curl -si -X POST $pat -d \@$f $url/$user/$repo/git/commits); # Execute json
684  
685 0 my $r = GitHub::Crud::Response::new($gitHub, $c);
686 0 my $success = $r->status == 201; # Check response code
687 0 unlink $f; # Cleanup
688  
689 0 ▶ $success or confess "Unable to create commit: ".dump($r);
Branch: 1 branch
BranchTrueFalseTotal
$success or confess('Unable to create commit: ' . GitHub::Crud::dump($r)) 0 0 0
690  
691 0 $r
692 0 }->();
693  
694 my $branch = sub # Update branch - if this fails we will try a force as the next step
695 0 ▶ {my $s = $commit->data->sha;
Subroutine __ANON__: not called CC=1 SCAR=6.9
696 0 my $f = writeFile(undef, <<END);
697 {
698 "ref": "refs/heads/$bran",
699 "sha": "$s"
700 }
701 END
702 0 my $c = qq(curl -si -X POST $pat -d \@$f $url/$user/$repo/git/refs);
703 0 my $r = GitHub::Crud::Response::new($gitHub, $c);
704 0 my $success = $r->status == 201; # Check response code
705 0 unlink $f; # Cleanup
706  
707 0 $r
708 0 }->();
709  
710 0 my $status = $branch->status; # Creation status
711 0 ▶ if ($branch->status == 201) {return $branch} # Branch created
Branch: 2 branches
BranchTrueFalseTotal
if ($branch->status == 201) { } 0 0 0
elsif ($branch->status == 422) { } 0 0 0
712 elsif ($branch->status == 422) # Update existing branch
713 {my $branchUpdate = sub
714 0 ▶ {my $s = $commit->data->sha;
Subroutine __ANON__: not called CC=2 SCAR=17.9
715 0 my $f = writeFile(undef, <<END);
716 { "sha": "$s",
717 "force": true
718 }
719 END
720 0 my $c = qq(curl -si -X PATCH $pat -d \@$f $url/$user/$repo/git/refs/heads/$bran);
721 0 my $r = GitHub::Crud::Response::new($gitHub, $c);
722 0 my $success = $r->status == 200; # Check response code
723 0 unlink $f; # Cleanup
724  
725 0 ▶ $success or confess "Unable to update branch: ".dump($r);
Branch: 1 branch
BranchTrueFalseTotal
$success or confess('Unable to update branch: ' . GitHub::Crud::dump($r)) 0 0 0
726  
727 0 $r
728 0 }->();
729 0 return $branchUpdate;
730 }
731  
732 0 confess "Unable to create/update branch: $bran";
733 }
734  
735 sub listWebHooks($) # List web hooks associated with your L<GitHub> repository.\mRequired: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>. \mIf the list operation is successful, L<failed|/failed> is set to false otherwise it is set to true.\mReturns true if the list operation was successful else false.
736 0 ▶ {my ($gitHub) = @_; # GitHub object
Subroutine listWebHooks: not called CC=7 SCAR=40.3
Pod documented
737 0 my $pat = $gitHub->patKey(1);
738 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
739 0 ▶ my $repo = qm $gitHub->repository; $repo or confess "repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repo or confess("repository required") 0 0 0
740 0 my $bran = qm $gitHub->refOrBranch(0);
741 0 my $url = url;
742  
743 0 my $u = filePath($url, $user, $repo, qw(hooks));
744 0 my $s = "curl -si $pat $u";
745 0 my $r = GitHub::Crud::Response::new($gitHub, $s);
746 0 my $success = $r->status =~ m(200|201); # Present or not present
747 0 ▶ $gitHub->failed = $success ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
748 0 ▶ !$success and $gitHub->confessOnFailure and confess dump($gitHub); # Confess to any failure if so requested
Branch: 1 branch
BranchTrueFalseTotal
not $success and $gitHub->confessOnFailure and confess(GitHub::Crud::dump($gitHub)) 0 0 0
Condition0%0/3
not $success and $gitHub->confessOnFailure
!ll&&!rl&&r
000
MC/DC0%0/2
not $success and $gitHub->confessOnFailure
not $success $gitHub->confessOnFailure
Truth table0%0/3
not(A) and B
A= not $successB= $gitHub->confessOnFailure
ABresult
0X0
100
111
749 0 ▶ $success ? $gitHub->response->data : undef # Return reference to array of web hooks on success. If there are no web hooks set then the referenced array will be empty.
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
750 }
751  
752 sub createPushWebHook($) # Create a web hook for your L<GitHub> userid.\mRequired: L<userid|/userid>, L<repository|/repository>, L<url|/url>, L<patKey|/patKey>.\mOptional: L<secret|/secret>.\mIf the create operation is successful, L<failed|/failed> is set to false otherwise it is set to true.\mReturns true if the web hook was created successfully else false.
753 0 ▶ {my ($gitHub) = @_; # GitHub object
Subroutine createPushWebHook: not called CC=10 SCAR=47.0
Pod documented
754 0 my $pat = $gitHub->patKey(1);
755 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
756 0 ▶ my $repo = qm $gitHub->repository; $repo or confess "repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repo or confess("repository required") 0 0 0
757 0 ▶ my $webUrl = qm $gitHub->webHookUrl; $webUrl or confess "url required";
Branch: 1 branch
BranchTrueFalseTotal
$webUrl or confess("url required") 0 0 0
758 0 my $bran = qm $gitHub->refOrBranch(0);
759 0 my $secret = $gitHub->secret;
760 0 ▶ my $sj = $secret ? qq(, "secret": "$secret") : ''; # Secret for Json
Branch: 1 branch
BranchTrueFalseTotal
$secret ? : 0 0 0
761 0 my $url = url;
762  
763 0 ▶ $webUrl =~ m(\Ahttps?://) or confess # Check that we are using a url like thing for the web hook or complain
Branch: 1 branch
BranchTrueFalseTotal
$webUrl =~ m[\Ahttps?://]u or confess("Web hook has no scheme, should start with https?:// not:\n$webUrl") 0 0 0
764 "Web hook has no scheme, should start with https?:// not:\n$webUrl";
765  
766 0 owf(my $tmpFile = temporaryFile(), my $json = <<END); # Write web hook definition
767 {"name": "web", "active": true, "events": ["push"],
768 "config": {"url": "$webUrl", "content_type": "json" $sj}
769 }
770 END
771 0 my $d = q( -d @).$tmpFile;
772 0 my $u = filePath($url, $user, $repo, qw(hooks));
773 0 my $s = "curl -si -X POST $pat $u $d"; # Create url
774 0 my $r = GitHub::Crud::Response::new($gitHub, $s);
775  
776 0 my $success = $r->status == 201; # Check response code
777 0 unlink $tmpFile; # Cleanup
778 0 ▶ $gitHub->failed = $success ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
779 0 ▶ !$success and $gitHub->confessOnFailure and confess dump($gitHub); # Confess to any failure if so requested
Branch: 1 branch
BranchTrueFalseTotal
not $success and $gitHub->confessOnFailure and confess(GitHub::Crud::dump($gitHub)) 0 0 0
Condition0%0/3
not $success and $gitHub->confessOnFailure
!ll&&!rl&&r
000
MC/DC0%0/2
not $success and $gitHub->confessOnFailure
not $success $gitHub->confessOnFailure
Truth table0%0/3
not(A) and B
A= not $successB= $gitHub->confessOnFailure
ABresult
0X0
100
111
780 0 ▶ $success ? 1 : undef # Return true on success
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
781 }
782  
783 sub listRepositories($) # List the repositories accessible to a user on L<GitHub>.\mRequired: L<userid|/userid>.\mReturns details of the repositories.
784 0 ▶ {my ($gitHub) = @_; # GitHub object
Subroutine listRepositories: not called CC=6 SCAR=37.4
Pod documented
785  
786 0 my $pat = $gitHub->patKey(1);
787 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
788 0 my $url = api;
789  
790 0 my $u = filePath($url, qw(user repos)); # Request url
791 0 my $s = "curl -si $pat $u"; # Create url
792 0 my $r = GitHub::Crud::Response::new($gitHub, $s);
793 0 my $success = $r->status == 200; # Check response code
794 0 ▶ $gitHub->failed = $success ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
795 0 ▶ !$success and $gitHub->confessOnFailure and confess dump($gitHub); # Confess to any failure if so requested
Branch: 1 branch
BranchTrueFalseTotal
not $success and $gitHub->confessOnFailure and confess(GitHub::Crud::dump($gitHub)) 0 0 0
Condition0%0/3
not $success and $gitHub->confessOnFailure
!ll&&!rl&&r
000
MC/DC0%0/2
not $success and $gitHub->confessOnFailure
not $success $gitHub->confessOnFailure
Truth table0%0/3
not(A) and B
A= not $successB= $gitHub->confessOnFailure
ABresult
0X0
100
111
796 0 ▶ $success ? $r->data->@* : undef # Return a list of repositories on success
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
797 }
798  
799 sub createRepository($) # Create a repository on L<GitHub>.\mRequired: L<userid|/userid>, L<repository|/repository>.\mReturns true if the issue was created successfully else false.
800 0 ▶ {my ($gitHub) = @_; # GitHub object
Subroutine createRepository: not called CC=8 SCAR=42.8
Pod documented
801 0 my $pat = $gitHub->patKey(1);
802 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
803 0 ▶ my $repo = qm $gitHub->repository; $repo or confess "repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repo or confess("repository required") 0 0 0
804 0 ▶ my $private= $gitHub->private ? q(, "private":true) : q(); # Private or not
Branch: 1 branch
BranchTrueFalseTotal
$gitHub->private ? : 0 0 0
805 0 my $url = api;
806  
807 0 my $json = qq({"name":"$repo", "auto_init":true $private}); # Issue in json
808 0 my $tmpFile = writeFile(undef, $json); # Write repo definition
809 0 my $d = q( -d @).$tmpFile;
810 0 my $u = filePath($url, qw(user repos)); # Request url
811 0 my $s = "curl -si -X POST $pat $u $d"; # Create url
812 0 my $r = GitHub::Crud::Response::new($gitHub, $s);
813 0 my $success = $r->status == 201; # Check response code
814 0 unlink $tmpFile; # Cleanup
815 0 ▶ $gitHub->failed = $success ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
816 0 ▶ !$success and $gitHub->confessOnFailure and confess dump($gitHub); # Confess to any failure if so requested
Branch: 1 branch
BranchTrueFalseTotal
not $success and $gitHub->confessOnFailure and confess(GitHub::Crud::dump($gitHub)) 0 0 0
Condition0%0/3
not $success and $gitHub->confessOnFailure
!ll&&!rl&&r
000
MC/DC0%0/2
not $success and $gitHub->confessOnFailure
not $success $gitHub->confessOnFailure
Truth table0%0/3
not(A) and B
A= not $successB= $gitHub->confessOnFailure
ABresult
0X0
100
111
817 0 ▶ $success ? 1 : undef # Return true on success
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
818 }
819  
820 sub createRepositoryFromSavedToken($$;$$) # Create a repository on L<GitHub> using an access token either as supplied or saved in a file using L<savePersonalAccessToken|/savePersonalAccessToken>.\mReturns true if the issue was created successfully else false.
821 0 ▶ {my ($userid, $repository, $private, $accessFolderOrToken) = @_; # Userid on GitHub, the repository name, true if the repo is private, location of access token.
Subroutine createRepositoryFromSavedToken: not called CC=1 SCAR=6.9
Pod documented
822 0 my $g = GitHub::Crud::new;
823 0 $g->userid = $userid;
824 0 $g->repository = $repository;
825 0 $g->private = $private;
826 0 $g->personalAccessTokenFolder = $accessFolderOrToken;
827 0 $g->loadPersonalAccessToken;
828 0 $g->confessOnFailure = 0;
829 0 $g->createRepository;
830 }
831  
832 sub createIssue($) # Create an issue on L<GitHub>.\mRequired: L<userid|/userid>, L<repository|/repository>, L<body|/body>, L<title|/title>.\mIf the operation is successful, L<failed|/failed> is set to false otherwise it is set to true.\mReturns true if the issue was created successfully else false.
833 0 ▶ {my ($gitHub) = @_; # GitHub object
Subroutine createIssue: not called CC=9 SCAR=45.0
Pod documented
834 0 my $pat = $gitHub->patKey(1);
835 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
836 0 ▶ my $repo = qm $gitHub->repository; $repo or confess "repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repo or confess("repository required") 0 0 0
837 0 ▶ my $body = $gitHub->body; $body or confess "body required";
Branch: 1 branch
BranchTrueFalseTotal
$body or confess("body required") 0 0 0
838 0 ▶ my $title = $gitHub->title; $title or confess "title required";
Branch: 1 branch
BranchTrueFalseTotal
$title or confess("title required") 0 0 0
839 0 my $bran = qm $gitHub->refOrBranch(0);
840 0 my $url = url;
841  
842 0 my $json = encodeJson({body=>$body, title=>$title}); # Issue in json
843 0 owf(my $tmpFile = temporaryFile(), $json); # Write issue definition
844 0 my $d = q( -d @).$tmpFile;
845 0 my $u = filePath($url, $user, $repo, qw(issues));
846 0 my $s = "curl -si -X POST $pat $u $d"; # Create url
847 0 my $r = GitHub::Crud::Response::new($gitHub, $s);
848 0 my $success = $r->status == 201; # Check response code
849 0 unlink $tmpFile; # Cleanup
850 0 ▶ $gitHub->failed = $success ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
851 0 ▶ !$success and $gitHub->confessOnFailure and # Confess to any failure if so requested
Branch: 1 branch
BranchTrueFalseTotal
not $success and $gitHub->confessOnFailure and confess(join("\n", GitHub::Crud::dump($gitHub), $json, $s)) 0 0 0
Condition0%0/3
not $success and $gitHub->confessOnFailure
!ll&&!rl&&r
000
MC/DC0%0/2
not $success and $gitHub->confessOnFailure
not $success $gitHub->confessOnFailure
Truth table0%0/3
not(A) and B
A= not $successB= $gitHub->confessOnFailure
ABresult
0X0
100
111
852 confess join "\n", dump($gitHub), $json, $s;
853 0 ▶ $success ? 1 : undef # Return true on success
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
854 }
855  
856 sub createIssueFromSavedToken($$$$;$) # Create an issue on L<GitHub> using an access token as supplied or saved in a file using L<savePersonalAccessToken|/savePersonalAccessToken>.\mReturns true if the issue was created successfully else false.
857 0 ▶ {my ($userid, $repository, $title, $body, $accessFolderOrToken) = @_; # Userid on GitHub, repository name, issue title, issue body, location of access token.
Subroutine createIssueFromSavedToken: not called CC=1 SCAR=6.9
Pod documented
858 0 my $g = GitHub::Crud::new;
859 0 $g->userid = $userid;
860 0 $g->repository = $repository;
861 0 $g->title = $title;
862 0 $g->body = $body;
863 0 $g->personalAccessTokenFolder = $accessFolderOrToken;
864 0 $g->loadPersonalAccessToken;
865 0 $g->confessOnFailure = 1;
866 0 $g->createIssue;
867 }
868  
869 sub currentRepo() # Create a github object for the current repo if we are on github actions
870 0 ▶ {if (my $r = $ENV{GITHUB_REPOSITORY}) # We are on GitHub
Subroutine currentRepo: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $ENV{"GITHUB_REPOSITORY"}) 0 0 0
871 0 {my ($user, $repo) = split m(/), $r, 2;
872 0 my $g = GitHub::Crud::new;
873 0 $g->userid = $user;
874 0 $g->repository = $repo;
875 0 $g->personalAccessToken = $ENV{GITHUB_TOKEN};
876 0 $g->confessOnFailure = 1;
877  
878 0 ▶ if (!$g->personalAccessToken)
Branch: 1 branch
BranchTrueFalseTotal
unless ($g->personalAccessToken) 0 0 0
879 0 {confess "Unable to load github token for repository $r from environment variable: GITHUB_TOKEN\nSee: https://github.com/philiprbrenan/postgres/blob/main/.github/workflows/main.yml";
880 }
881  
882 0 return $g;
883 }
884 undef
885 0 }
886  
887 sub createIssueInCurrentRepo($$) # Create an issue in the current GitHub repo if we are running on GitHub
888 0 ▶ {my ($title, $body) = @_; # Title of issue, body of issue
Subroutine createIssueInCurrentRepo: not called CC=2 SCAR=17.9
Pod documented
889 0 ▶ if (my $g = currentRepo) # We are on GitHub
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = &currentRepo()) 0 0 0
890 0 {$g->title = $title;
891 0 $g->body = $body;
892 0 $g->createIssue;
893 }
894 }
895  
896 sub writeFileUsingSavedToken($$$$;$) # Write to a file on L<GitHub> using a personal access token as supplied or saved in a file. Return B<1> on success or confess to any failure.
897 0 ▶ {my ($userid, $repository, $file, $content, $accessFolderOrToken) = @_; # Userid on GitHub, repository name, file name on github, file content, location of access token.
Subroutine writeFileUsingSavedToken: not called CC=4 SCAR=30.0
Pod documented
898 0 my $g = GitHub::Crud::new;
899 0 ▶ $g->userid = $userid; $userid or confess "Userid required";
Branch: 1 branch
BranchTrueFalseTotal
$userid or confess("Userid required") 0 0 0
900 0 ▶ $g->repository = $repository; $repository or confess "Repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repository or confess("Repository required") 0 0 0
901 0 ▶ $g->gitFile = $file; $file or confess "File required";
Branch: 1 branch
BranchTrueFalseTotal
$file or confess("File required") 0 0 0
902 0 $g->personalAccessTokenFolder = $accessFolderOrToken;
903 0 $g->loadPersonalAccessToken;
904 0 $g->write($content);
905 }
906  
907 sub writeFileFromFileUsingSavedToken($$$$;$) # Copy a file to L<github> using a personal access token as supplied or saved in a file. Return B<1> on success or confess to any failure.
908 0 ▶ {my ($userid, $repository, $file, $localFile, $accessFolderOrToken) = @_; # Userid on GitHub, repository name, file name on github, file content, location of access token.
Subroutine writeFileFromFileUsingSavedToken: not called CC=1 SCAR=6.9
Pod documented
909 0 writeFileUsingSavedToken($userid, $repository, $file,
910 readBinaryFile($localFile), $accessFolderOrToken);
911 }
912  
913 sub writeFileFromCurrentRun($$) # Write to a file into the repository from the current run
914 0 ▶ {my ($target, $text) = @_; # The target file name in the repo, the text to write into this file
Subroutine writeFileFromCurrentRun: not called CC=2 SCAR=17.9
Pod documented
915 0 ▶ if (my $g = currentRepo) # We are on GitHub
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = &currentRepo()) 0 0 0
916 0 {$g->gitFile = $target;
917 0 $g->write($text);
918 }
919 }
920  
921 sub writeFileFromFileFromCurrentRun($) # Write a file into the repository from the current run
922 0 ▶ {my ($target) = @_; # File name both locally and in the repo
Subroutine writeFileFromFileFromCurrentRun: not called CC=3 SCAR=24.8
Pod documented
923 0 ▶ -e $target or confess "File to upload does not exist:\n$target";
Branch: 1 branch
BranchTrueFalseTotal
-e $target or confess("File to upload does not exist:\n$target") 0 0 0
924 0 ▶ if (my $g = currentRepo) # We are on GitHub
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = &currentRepo()) 0 0 0
925 0 {$g->gitFile = $target;
926 0 $g->write(scalar(readFile($target)));
927 }
928 }
929  
930 sub writeBinaryFileFromFileInCurrentRun($$) # Upload a binary file from the current run into the repo.
931 0 ▶ {my ($target, $source) = @_; # The target file name in the repo, the current file name in the run
Subroutine writeBinaryFileFromFileInCurrentRun: not called CC=2 SCAR=17.9
Pod documented
932 0 ▶ if (my $g = currentRepo) # We are on GitHub
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = &currentRepo()) 0 0 0
933 0 {$g->gitFile = $target;
934 0 $g->write(readBinaryFile($source));
935 }
936 }
937  
938 sub readFileUsingSavedToken($$$;$) # Read from a file on L<GitHub> using a personal access token as supplied or saved in a file. Return the content of the file on success or confess to any failure.
939 0 ▶ {my ($userid, $repository, $file, $accessFolderOrToken) = @_; # Userid on GitHub, repository name, file name on github, location of access token.
Subroutine readFileUsingSavedToken: not called CC=1 SCAR=6.9
Pod documented
940 0 my $g = GitHub::Crud::new;
941 0 $g->userid = $userid;
942 0 $g->repository = $repository;
943 0 $g->gitFile = $file;
944 0 $g->personalAccessTokenFolder = $accessFolderOrToken;
945 0 $g->loadPersonalAccessToken;
946 0 $g->read;
947 }
948  
949 sub writeFolderUsingSavedToken($$$$;$) # Write all the files in a local folder to a target folder on a named L<GitHub> repository using a personal access token as supplied or saved in a file.
950 0 ▶ {my ($userid,$repository,$targetFolder,$localFolder,$accessFolderOrToken) = @_;# Userid on GitHub, repository name, target folder on github, local folder name, location of access token.
Subroutine writeFolderUsingSavedToken: not called CC=2 SCAR=17.9
Pod documented
951 0 my $g = GitHub::Crud::new;
952 0 $g->userid = $userid;
953 0 $g->repository = $repository;
954 0 $g->personalAccessTokenFolder = $accessFolderOrToken;
955 0 $g->loadPersonalAccessToken;
956  
957 0 for my $file(searchDirectoryTreesForMatchingFiles($localFolder))
958 0 {$g->gitFile = swapFilePrefix($file, $localFolder, $targetFolder);
959 0 $g->write(readBinaryFile($file));
960 }
961 }
962  
963 sub writeCommitUsingSavedToken($$$;$) # Write all the files in a local folder to a named L<GitHub> repository using a personal access token as supplied or saved in a file.
964 0 ▶ {my ($userid, $repository, $source, $accessFolderOrToken) = @_; # Userid on GitHub, repository name, local folder on github, optionally: location of access token.
Subroutine writeCommitUsingSavedToken: not called CC=1 SCAR=6.9
Pod documented
965 0 my $g = GitHub::Crud::new;
966 0 $g->userid = $userid;
967 0 $g->repository = $repository;
968 0 $g->personalAccessTokenFolder = $accessFolderOrToken;
969 0 $g->loadPersonalAccessToken;
970 0 $g->branch = 'master';
971  
972 0 $g->writeCommit($source);
973 }
974  
975 sub deleteFileUsingSavedToken($$$;$) # Delete a file on GitHub using a saved token
976 0 ▶ {my ($userid, $repository, $target, $accessFolderOrToken) = @_; # Userid on GitHub, repository name, file on Github, optional: the folder containing saved access tokens
Subroutine deleteFileUsingSavedToken: not called CC=1 SCAR=6.9
Pod documented
977 0 my $g = GitHub::Crud::new;
978 0 $g->userid = $userid;
979 0 $g->repository = $repository;
980 0 $g->personalAccessTokenFolder = $accessFolderOrToken;
981 0 $g->loadPersonalAccessToken;
982  
983 0 $g->gitFile = $target;
984 0 $g->delete;
985 }
986  
987 sub getRepositoryUsingSavedToken($$;$) # Get repository details using a saved token
988 0 ▶ {my ($userid, $repository, $accessFolderOrToken) = @_; # Userid on GitHub, repository name, optionally: location of access token.
Subroutine getRepositoryUsingSavedToken: not called CC=3 SCAR=24.8
Pod documented
989 0 my $g = GitHub::Crud::new;
990 0 ▶ $g->userid = $userid; $userid or confess "Userid required";
Branch: 1 branch
BranchTrueFalseTotal
$userid or confess("Userid required") 0 0 0
991 0 ▶ $g->repository = $repository; $repository or confess "Repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repository or confess("Repository required") 0 0 0
992 0 $g->personalAccessTokenFolder = $accessFolderOrToken;
993 0 $g->loadPersonalAccessToken;
994 0 $g->getRepository;
995 }
996  
997 sub getRepositoryUpdatedAtUsingSavedToken($$;$) # Get repository 'updated_at' using a saved token and return the time in number of seconds since the Unix epoch.
998 0 ▶ {my ($userid, $repository, $accessFolderOrToken) = @_; # Userid on GitHub, repository name, optionally: location of access token.
Subroutine getRepositoryUpdatedAtUsingSavedToken: not called CC=1 SCAR=6.9
Pod documented
999 0 my $r = &getRepositoryUsingSavedToken(@_); # Get repository details using a saved token
1000 0 my $u = $r->data->{updated_at};
1001 0 return Date::Manip::UnixDate($u,'%s');
1002 }
1003  
1004 #D1 Access tokens # Load and save access tokens. Some L<github> requets must be signed with an L<OAuth> access token. These methods allow you to store and reuse such tokens.
1005  
1006 sub savePersonalAccessToken($) # Save a L<GitHub> personal access token by userid in folder L<personalAccessTokenFolder|/personalAccessTokenFolder>.
1007 0 ▶ {my ($gitHub) = @_; # GitHub object
Subroutine savePersonalAccessToken: not called CC=6 SCAR=37.4
Pod documented
1008 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
1009 0 ▶ my $pat = $gitHub->personalAccessToken; $pat or confess "personal access token required";
Branch: 1 branch
BranchTrueFalseTotal
$pat or confess("personal access token required") 0 0 0
1010 0 ▶ my $dir = $gitHub->personalAccessTokenFolder // accessFolder;
Condition0%0/3
$gitHub->personalAccessTokenFolder // accessFolder()
l!l&&r!l&&!r
000
MC/DC0%0/2
$gitHub->personalAccessTokenFolder // accessFolder()
$gitHub->personalAccessTokenFolder accessFolder()
Truth table0%0/3
A // B
A= $gitHub->personalAccessTokenFolderB= accessFolder()
ABresult
1X1
011
000
1011 0 my $file = filePathExt($dir, $user, q(data));
1012 0 makePath($file);
1013 0 storeFile($file, {pat=>$pat}); # Store personal access token
1014 0 ▶ -e $file or confess "Unable to store personal access token in file:\n$file"; # Complain if store fails
Branch: 1 branch
BranchTrueFalseTotal
-e $file or confess("Unable to store personal access token in file:\n$file") 0 0 0
1015 0 my $p = retrieveFile $file; # Retrieve access token to check that we wrote it successfully
1016 $pat eq $p->{pat} or # Check file format
1017 0 ▶ confess "File contains the wrong personal access token:\n$file";
Branch: 1 branch
BranchTrueFalseTotal
$pat eq $p->{"pat"} or confess("File contains the wrong personal access token:\n$file") 0 0 0
1018 }
1019  
1020 sub loadPersonalAccessToken($) # Load a personal access token by userid from folder L<personalAccessTokenFolder|/personalAccessTokenFolder>.
1021 0 ▶ {my ($gitHub) = @_; # GitHub object
Subroutine loadPersonalAccessToken: not called CC=7 SCAR=40.3
Pod documented
1022 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
1023  
1024 0 ▶ if (length($gitHub->personalAccessTokenFolder//accessFolder) == 43) # Access token supplied directly
Branch: 1 branch
BranchTrueFalseTotal
if (length($gitHub->personalAccessTokenFolder // accessFolder()) == 43) 0 0 0
Condition0%0/3
$gitHub->personalAccessTokenFolder // accessFolder()
l!l&&r!l&&!r
000
MC/DC0%0/2
$gitHub->personalAccessTokenFolder // accessFolder()
$gitHub->personalAccessTokenFolder accessFolder()
Truth table0%0/3
A // B
A= $gitHub->personalAccessTokenFolderB= accessFolder()
ABresult
1X1
011
000
1025 0 {return $gitHub->personalAccessToken = $gitHub->personalAccessTokenFolder;
1026 }
1027  
1028 0 ▶ if ($ENV{GITHUB_TOKEN}) # Access token supplied through environment
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"GITHUB_TOKEN"}) 0 0 0
1029 0 {return $gitHub->personalAccessToken = $ENV{GITHUB_TOKEN};
1030 }
1031  
1032 0 ▶ my $dir = $gitHub->personalAccessTokenFolder // accessFolder;
Condition0%0/3
$gitHub->personalAccessTokenFolder // accessFolder()
l!l&&r!l&&!r
000
MC/DC0%0/2
$gitHub->personalAccessTokenFolder // accessFolder()
$gitHub->personalAccessTokenFolder accessFolder()
Truth table0%0/3
A // B
A= $gitHub->personalAccessTokenFolderB= accessFolder()
ABresult
1X1
011
000
1033 0 my $file = filePathExt($dir, $user, q(data));
1034 0 my $p = retrieveFile $file; # Load personal access token
1035 my $a = $p->{pat} or # Check file format
1036 0 ▶ confess "File does not contain a personal access token:\n$file";
Branch: 1 branch
BranchTrueFalseTotal
my $a = $p->{"pat"} or confess("File does not contain a personal access token:\n$file") 0 0 0
1037 0 $gitHub->personalAccessToken = $a; # Retrieve token
1038 }
1039  
1040 #D0
1041 #-------------------------------------------------------------------------------
1042 # Export - eeee
1043 #-------------------------------------------------------------------------------
1044  
1045 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
1046  
1047 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
1048  
1049 # containingFolder
1050  
1051 1 @ISA = qw(Exporter);
1052 1 @EXPORT_OK = qw(
1053 createIssueFromSavedToken
1054 createIssueInCurrentRepo
1055 createRepositoryFromSavedToken
1056 deleteFileUsingSavedToken
1057 getRepository
1058 getRepositoryUsingSavedToken
1059 getRepositoryUpdatedAtUsingSavedToken
1060 readFileUsingSavedToken
1061 writeBinaryFileFromFileInCurrentRun
1062 writeCommitUsingSavedToken
1063 writeFileFromCurrentRun
1064 writeFileFromFileUsingSavedToken
1065 writeFileUsingSavedToken
1066 writeFolderUsingSavedToken
1067 );
1068 1 %EXPORT_TAGS = (all=>[@EXPORT_OK]);
1069  
1070 #podDocumentation
1071  
1072 =pod
1073  
1074 =encoding utf-8
1075  
1076 =head1 Name
1077  
1078 GitHub::Crud - Create, Read, Update, Delete files, commits, issues, and web hooks on GitHub.
1079  
1080 =head1 Synopsis
1081  
1082 Create, Read, Update, Delete files, commits, issues, and web hooks on GitHub as
1083 described at:
1084  
1085 https://developer.github.com/v3/repos/contents/#update-a-file
1086  
1087 Commit a folder to GitHub then read and check some of the uploaded content:
1088  
1089 use GitHub::Crud;
1090 use Data::Table::Text qw(:all);
1091  
1092 my $f = temporaryFolder; # Folder in which we will create some files to upload in the commit
1093 my $c = dateTimeStamp; # Create some content
1094 my $if = q(/home/phil/.face); # Image file
1095  
1096 writeFile(fpe($f, q(data), $_, qw(txt)), $c) for 1..3; # Place content in files in a sub folder
1097 copyBinaryFile $if, my $If = fpe $f, qw(face jpg); # Add an image
1098  
1099 my $g = GitHub::Crud::new # Create GitHub
1100 (userid => q(philiprbrenan),
1101 repository => q(aaa),
1102 branch => q(test),
1103 confessOnFailure => 1);
1104  
1105 $g->loadPersonalAccessToken; # Load a personal access token
1106 $g->writeCommit($f); # Upload commit - confess to any errors
1107  
1108 my $C = $g->read(q(data/1.txt)); # Read data written in commit
1109 my $I = $g->read(q(face.jpg));
1110 my $i = readBinaryFile $if;
1111  
1112 confess "Date stamp failed" unless $C eq $c; # Check text
1113 confess "Image failed" unless $i eq $I; # Check image
1114 confess "Write commit succeeded";
1115  
1116 =head1 Prerequisites
1117  
1118 sudo apt-get install curl
1119  
1120 =head1 Description
1121  
1122 Create, Read, Update, Delete files, commits, issues, and web hooks on GitHub.
1123  
1124  
1125 Version 20210211.
1126  
1127  
1128 The following sections describe the methods in each functional area of this
1129 module. For an alphabetic listing of all methods by name see L<Index|/Index>.
1130  
1131  
1132  
1133 =head1 Constructor
1134  
1135 Create a L<GitHub|https://github.com/philiprbrenan> object with the specified attributes describing the interface with L<GitHub|https://github.com/philiprbrenan>.
1136  
1137 =head2 new(%attributes)
1138  
1139 Create a new L<GitHub|https://github.com/philiprbrenan> object with attributes as described at: L<GitHub::Crud Definition>.
1140  
1141 Parameter Description
1142 1 %attributes Attribute values
1143  
1144 B<Example:>
1145  
1146  
1147 my $f = temporaryFolder; # Folder in which we will create some files to upload in the commit
1148 my $c = dateTimeStamp; # Create some content
1149 my $if = q(/home/phil/.face); # Image file
1150  
1151 writeFile(fpe($f, q(data), $_, qw(txt)), $c) for 1..3; # Place content in files in a sub folder
1152 copyBinaryFile $if, my $If = fpe $f, qw(face jpg); # Add an image
1153  
1154  
1155 my $g = GitHub::Crud::new # Create GitHub # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1156  
1157 (userid => q(philiprbrenan),
1158 repository => q(aaa),
1159 branch => q(test),
1160 confessOnFailure => 1);
1161  
1162 $g->loadPersonalAccessToken; # Load a personal access token
1163 $g->writeCommit($f); # Upload commit - confess to any errors
1164  
1165 my $C = $g->read(q(data/1.txt)); # Read data written in commit
1166 my $I = $g->read(q(face.jpg));
1167 my $i = readBinaryFile $if;
1168  
1169 confess "Date stamp failed" unless $C eq $c; # Check text
1170 confess "Image failed" unless $i eq $I; # Check image
1171 success "Write commit succeeded";
1172  
1173  
1174 =head1 Files
1175  
1176 File actions on the contents of L<GitHub|https://github.com/philiprbrenan> repositories.
1177  
1178 =head2 list($gitHub)
1179  
1180 List all the files contained in a L<GitHub|https://github.com/philiprbrenan> repository or all the files below a specified folder in the repository.
1181  
1182 Required attributes: L<userid|/userid>, L<repository|/repository>.
1183  
1184 Optional attributes: L<gitFolder|/gitFolder>, L<refOrBranch|/refOrBranch>, L<nonRecursive|/nonRecursive>, L<patKey|/patKey>.
1185  
1186 Use the L<gitFolder|/gitFolder> parameter to specify the folder to start the list from, by default, the listing will start at the root folder of your repository.
1187  
1188 Use the L<nonRecursive|/nonRecursive> option if you require only the files in the start folder as otherwise all the folders in the start folder will be listed as well which might take some time.
1189  
1190 If the list operation is successful, L<failed|/failed> is set to false and L<fileList|/fileList> is set to refer to an array of the file names found.
1191  
1192 If the list operation fails then L<failed|/failed> is set to true and L<fileList|/fileList> is set to refer to an empty array.
1193  
1194 Returns the list of file names found or empty list if no files were found.
1195  
1196 Parameter Description
1197 1 $gitHub GitHub
1198  
1199 B<Example:>
1200  
1201  
1202  
1203 success "list:", gitHub->list; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1204  
1205  
1206 # list: alpha.data .github/workflows/test.yaml images/aaa.txt images/aaa/bbb.txt # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1207  
1208  
1209  
1210 =head2 specialFileData($d)
1211  
1212 Do not encode or decode data with a known file signature
1213  
1214 Parameter Description
1215 1 $d String to check
1216  
1217 =head2 read($gitHub, $File)
1218  
1219 Read data from a file on L<GitHub|https://github.com/philiprbrenan>.
1220  
1221 Required attributes: L<userid|/userid>, L<repository|/repository>.
1222  
1223 Optional attributes: L<gitFile|/gitFile> = the file to read, L<refOrBranch|/refOrBranch>, L<patKey|/patKey>.
1224  
1225 If the read operation is successful, L<failed|/failed> is set to false and L<readData|/readData> is set to the data read from the file.
1226  
1227 If the read operation fails then L<failed|/failed> is set to true and L<readData|/readData> is set to B<undef>.
1228  
1229 Returns the data read or B<undef> if no file was found.
1230  
1231 Parameter Description
1232 1 $gitHub GitHub
1233 2 $File File o read if not specified in gitFile
1234  
1235 B<Example:>
1236  
1237  
1238 my $g = gitHub;
1239 $g->gitFile = my $f = q(z'2 'z"z.data);
1240 my $d = q(𝝰𝝱𝝲);
1241 $g->write($d);
1242  
1243 confess "read FAILED" unless $g->read eq $d; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1244  
1245 success "Read passed";
1246  
1247  
1248 =head2 write($gitHub, $data, $File)
1249  
1250 Write utf8 data into a L<GitHub|https://github.com/philiprbrenan> file.
1251  
1252 Required attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>. Either specify the target file on:<github> using the L<gitFile|/gitFile> attribute or supply it as the third parameter. Returns B<true> on success else L<undef|https://perldoc.perl.org/functions/undef.html>.
1253  
1254 Parameter Description
1255 1 $gitHub GitHub object
1256 2 $data Data to be written
1257 3 $File Optionally the name of the file on github
1258  
1259 B<Example:>
1260  
1261  
1262 my $g = gitHub;
1263 $g->gitFile = "zzz.data";
1264  
1265 my $d = dateTimeStamp.q( 𝝰𝝱𝝲);
1266  
1267 if (1)
1268 {my $t = time();
1269  
1270 $g->write($d); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1271  
1272  
1273 lll "First write time: ", time() - $t; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1274  
1275 }
1276  
1277 my $r = $g->read;
1278 lll "Write bbb: $r";
1279 if (1)
1280 {my $t = time();
1281  
1282 $g->write($d); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1283  
1284  
1285 lll "Second write time: ", time() - $t; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1286  
1287 }
1288  
1289 confess "write FAILED" unless $g->exists; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1290  
1291 success "Write passed";
1292  
1293  
1294 =head2 readBlob($gitHub, $sha)
1295  
1296 Read a L<blob|https://en.wikipedia.org/wiki/Binary_large_object> from L<GitHub|https://github.com/philiprbrenan>.
1297  
1298 Required attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>. Returns the content of the L<blob|https://en.wikipedia.org/wiki/Binary_large_object> identified by the specified L<SHA|https://en.wikipedia.org/wiki/SHA-1>.
1299  
1300 Parameter Description
1301 1 $gitHub GitHub object
1302 2 $sha Data to be written
1303  
1304 B<Example:>
1305  
1306  
1307 my $g = gitHub;
1308 $g->gitFile = "face.jpg";
1309 my $d = readBinaryFile(q(/home/phil/.face));
1310 my $s = $g->writeBlob($d);
1311 my $S = q(4a2df549febb701ba651aae46e041923e9550cb8);
1312 confess q(Write blob FAILED) unless $s eq $S;
1313  
1314  
1315 my $D = $g->readBlob($s); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1316  
1317 confess q(Write/Read blob FAILED) unless $d eq $D;
1318 success q(Write/Read blob passed);
1319  
1320  
1321 =head2 writeBlob($gitHub, $data)
1322  
1323 Write data into a L<GitHub|https://github.com/philiprbrenan> as a L<blob|https://en.wikipedia.org/wiki/Binary_large_object> that can be referenced by future commits.
1324  
1325 Required attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>. Returns the L<SHA|https://en.wikipedia.org/wiki/SHA-1> of the created L<blob|https://en.wikipedia.org/wiki/Binary_large_object> or L<undef|https://perldoc.perl.org/functions/undef.html> in a failure occurred.
1326  
1327 Parameter Description
1328 1 $gitHub GitHub object
1329 2 $data Data to be written
1330  
1331 B<Example:>
1332  
1333  
1334 my $g = gitHub;
1335 $g->gitFile = "face.jpg";
1336 my $d = readBinaryFile(q(/home/phil/.face));
1337  
1338 my $s = $g->writeBlob($d); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1339  
1340 my $S = q(4a2df549febb701ba651aae46e041923e9550cb8);
1341 confess q(Write blob FAILED) unless $s eq $S;
1342  
1343 my $D = $g->readBlob($s);
1344 confess q(Write/Read blob FAILED) unless $d eq $D;
1345 success q(Write/Read blob passed);
1346  
1347  
1348 =head2 copy($gitHub, $target)
1349  
1350 Copy a source file from one location to another target location in your L<GitHub|https://github.com/philiprbrenan> repository, overwriting the target file if it already exists.
1351  
1352 Required attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>, L<gitFile|/gitFile> = the file to be copied.
1353  
1354 Optional attributes: L<refOrBranch|/refOrBranch>.
1355  
1356 If the write operation is successful, L<failed|/failed> is set to false otherwise it is set to true.
1357  
1358 Returns B<updated> if the write updated the file, B<created> if the write created the file else B<undef> if the write failed.
1359  
1360 Parameter Description
1361 1 $gitHub GitHub object
1362 2 $target The name of the file to be created
1363  
1364 B<Example:>
1365  
1366  
1367 my ($f1, $f2) = ("zzz.data", "zzz2.data");
1368 my $g = gitHub;
1369 $g->gitFile = $f2; $g->delete;
1370 $g->gitFile = $f1;
1371 my $d = dateTimeStamp;
1372 my $w = $g->write($d);
1373  
1374 my $r = $g->copy($f2); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1375  
1376 lll "Copy created: $r";
1377 $g->gitFile = $f2;
1378 my $D = $g->read;
1379 lll "Read ccc: $D";
1380  
1381 confess "copy FAILED" unless $d eq $D; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1382  
1383 success "Copy passed"
1384  
1385  
1386 =head2 exists($gitHub)
1387  
1388 Test whether a file exists on L<GitHub|https://github.com/philiprbrenan> or not and returns an object including the B<sha> and B<size> fields if it does else L<undef|https://perldoc.perl.org/functions/undef.html>.
1389  
1390 Required attributes: L<userid|/userid>, L<repository|/repository>, L<gitFile|/gitFile> file to test.
1391  
1392 Optional attributes: L<refOrBranch|/refOrBranch>, L<patKey|/patKey>.
1393  
1394 Parameter Description
1395 1 $gitHub GitHub object
1396  
1397 B<Example:>
1398  
1399  
1400 my $g = gitHub;
1401 $g->gitFile = "test4.html";
1402 my $d = dateTimeStamp;
1403 $g->write($d);
1404  
1405 confess "exists FAILED" unless $g->read eq $d; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1406  
1407 $g->delete;
1408  
1409 confess "exists FAILED" if $g->read eq $d; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1410  
1411 success "Exists passed";
1412  
1413  
1414 =head2 rename($gitHub, $target)
1415  
1416 Rename a source file on L<GitHub|https://github.com/philiprbrenan> if the target file name is not already in use.
1417  
1418 Required attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>, L<gitFile|/gitFile> = the file to be renamed.
1419  
1420 Optional attributes: L<refOrBranch|/refOrBranch>.
1421  
1422 Returns the new name of the file B<renamed> if the rename was successful else B<undef> if the rename failed.
1423  
1424 Parameter Description
1425 1 $gitHub GitHub object
1426 2 $target The new name of the file
1427  
1428 B<Example:>
1429  
1430  
1431 my ($f1, $f2) = qw(zzz.data zzz2.data);
1432 my $g = gitHub;
1433 $g->gitFile = $f2; $g->delete;
1434  
1435 my $d = dateTimeStamp;
1436 $g->gitFile = $f1;
1437 $g->write($d);
1438  
1439 confess "rename FAILED" unless $g->read eq $d; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1440  
1441  
1442  
1443 $g->rename($f2); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1444  
1445  
1446 confess "rename FAILED" if $g->exists; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1447  
1448  
1449 $g->gitFile = $f2;
1450  
1451 confess "rename FAILED" if $g->read eq $d; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1452  
1453 success "Rename passed";
1454  
1455  
1456 =head2 delete($gitHub)
1457  
1458 Delete a file from L<GitHub|https://github.com/philiprbrenan>.
1459  
1460 Required attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>, L<gitFile|/gitFile> = the file to be deleted.
1461  
1462 Optional attributes: L<refOrBranch|/refOrBranch>.
1463  
1464 If the delete operation is successful, L<failed|/failed> is set to false otherwise it is set to true.
1465  
1466 Returns true if the delete was successful else false.
1467  
1468 Parameter Description
1469 1 $gitHub GitHub object
1470  
1471 B<Example:>
1472  
1473  
1474 my $g = gitHub;
1475 my $d = dateTimeStamp;
1476 $g->gitFile = "zzz.data";
1477 $g->write($d);
1478  
1479  
1480 confess "delete FAILED" unless $g->read eq $d; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1481  
1482  
1483 if (1)
1484 {my $t = time();
1485  
1486 my $d = $g->delete; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1487  
1488 lll "Delete 1: ", $d;
1489  
1490 lll "First delete: ", time() - $t; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1491  
1492  
1493 confess "delete FAILED" if $g->exists; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1494  
1495 }
1496  
1497 if (1)
1498 {my $t = time();
1499  
1500 my $d = $g->delete; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1501  
1502 lll "Delete 1: ", $d;
1503  
1504 lll "Second delete: ", time() - $t; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1505  
1506  
1507 confess "delete FAILED" if $g->exists; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1508  
1509 }
1510 success "Delete passed";
1511  
1512  
1513 =head1 Repositories
1514  
1515 Perform actions on L<GitHub|https://github.com/philiprbrenan> repositories.
1516  
1517 =head2 getRepository($gitHub)
1518  
1519 Get the overall details of a repository
1520  
1521 Parameter Description
1522 1 $gitHub GitHub object
1523  
1524 B<Example:>
1525  
1526  
1527  
1528 my $r = gitHub(repository => q(C))->getRepository; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1529  
1530 success "Get repository succeeded";
1531  
1532  
1533 =head2 listCommits($gitHub)
1534  
1535 List all the commits in a L<GitHub|https://github.com/philiprbrenan> repository.
1536  
1537 Required attributes: L<userid|/userid>, L<repository|/repository>.
1538  
1539 Parameter Description
1540 1 $gitHub GitHub object
1541  
1542 B<Example:>
1543  
1544  
1545  
1546 my $c = gitHub->listCommits; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1547  
1548 my %s = listCommitShas $c;
1549 lll "Commits
1550 ", dump $c;
1551 lll "Commit shas
1552 ", dump \%s;
1553 success "ListCommits passed";
1554  
1555  
1556 =head2 listCommitShas($commits)
1557  
1558 Create {commit name => sha} from the results of L<listCommits>.
1559  
1560 Parameter Description
1561 1 $commits Commits from L<listCommits>
1562  
1563 B<Example:>
1564  
1565  
1566 my $c = gitHub->listCommits;
1567  
1568 my %s = listCommitShas $c; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1569  
1570 lll "Commits
1571 ", dump $c;
1572 lll "Commit shas
1573 ", dump \%s;
1574 success "ListCommits passed";
1575  
1576  
1577 =head2 writeCommit($gitHub, $folder, @files)
1578  
1579 Write all the files in a B<$folder> (or just the the named files) into a L<GitHub|https://github.com/philiprbrenan> repository in parallel as a commit on the specified branch.
1580  
1581 Required attributes: L<userid|/userid>, L<repository|/repository>, L<refOrBranch|/refOrBranch>.
1582  
1583 Parameter Description
1584 1 $gitHub GitHub object
1585 2 $folder File prefix to remove
1586 3 @files Files to write
1587  
1588 B<Example:>
1589  
1590  
1591 my $f = temporaryFolder; # Folder in which we will create some files to upload in the commit
1592 my $c = dateTimeStamp; # Create some content
1593 my $if = q(/home/phil/.face); # Image file
1594  
1595 writeFile(fpe($f, q(data), $_, qw(txt)), $c) for 1..3; # Place content in files in a sub folder
1596 copyBinaryFile $if, my $If = fpe $f, qw(face jpg); # Add an image
1597  
1598 my $g = GitHub::Crud::new # Create GitHub
1599 (userid => q(philiprbrenan),
1600 repository => q(aaa),
1601 branch => q(test),
1602 confessOnFailure => 1);
1603  
1604 $g->loadPersonalAccessToken; # Load a personal access token
1605  
1606 $g->writeCommit($f); # Upload commit - confess to any errors # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1607  
1608  
1609 my $C = $g->read(q(data/1.txt)); # Read data written in commit
1610 my $I = $g->read(q(face.jpg));
1611 my $i = readBinaryFile $if;
1612  
1613 confess "Date stamp failed" unless $C eq $c; # Check text
1614 confess "Image failed" unless $i eq $I; # Check image
1615 success "Write commit succeeded";
1616  
1617  
1618 =head2 listWebHooks($gitHub)
1619  
1620 List web hooks associated with your L<GitHub|https://github.com/philiprbrenan> repository.
1621  
1622 Required: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>.
1623  
1624 If the list operation is successful, L<failed|/failed> is set to false otherwise it is set to true.
1625  
1626 Returns true if the list operation was successful else false.
1627  
1628 Parameter Description
1629 1 $gitHub GitHub object
1630  
1631 B<Example:>
1632  
1633  
1634  
1635 success join ' ', q(Webhooks:), dump(gitHub->listWebHooks); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1636  
1637  
1638  
1639 =head2 createPushWebHook($gitHub)
1640  
1641 Create a web hook for your L<GitHub|https://github.com/philiprbrenan> userid.
1642  
1643 Required: L<userid|/userid>, L<repository|/repository>, L<url|/url>, L<patKey|/patKey>.
1644  
1645 Optional: L<secret|/secret>.
1646  
1647 If the create operation is successful, L<failed|/failed> is set to false otherwise it is set to true.
1648  
1649 Returns true if the web hook was created successfully else false.
1650  
1651 Parameter Description
1652 1 $gitHub GitHub object
1653  
1654 B<Example:>
1655  
1656  
1657 my $g = gitHub;
1658  
1659 my $d = $g->createPushWebHook; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1660  
1661 success join ' ', "Create web hook:", dump($d);
1662  
1663  
1664 =head2 listRepositories($gitHub)
1665  
1666 List the repositories accessible to a user on L<GitHub|https://github.com/philiprbrenan>.
1667  
1668 Required: L<userid|/userid>.
1669  
1670 Returns details of the repositories.
1671  
1672 Parameter Description
1673 1 $gitHub GitHub object
1674  
1675 B<Example:>
1676  
1677  
1678  
1679 success "List repositories: ", dump(gitHub()->listRepositories); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1680  
1681  
1682  
1683 =head2 createRepository($gitHub)
1684  
1685 Create a repository on L<GitHub|https://github.com/philiprbrenan>.
1686  
1687 Required: L<userid|/userid>, L<repository|/repository>.
1688  
1689 Returns true if the issue was created successfully else false.
1690  
1691 Parameter Description
1692 1 $gitHub GitHub object
1693  
1694 B<Example:>
1695  
1696  
1697  
1698 gitHub(repository => q(ccc))->createRepository; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1699  
1700 success "Create repository succeeded";
1701  
1702  
1703 =head2 createRepositoryFromSavedToken($userid, $repository, $private, $accessFolderOrToken)
1704  
1705 Create a repository on L<GitHub|https://github.com/philiprbrenan> using an access token either as supplied or saved in a file using L<savePersonalAccessToken|/savePersonalAccessToken>.
1706  
1707 Returns true if the issue was created successfully else false.
1708  
1709 Parameter Description
1710 1 $userid Userid on GitHub
1711 2 $repository The repository name
1712 3 $private True if the repo is private
1713 4 $accessFolderOrToken Location of access token.
1714  
1715 B<Example:>
1716  
1717  
1718  
1719 createRepositoryFromSavedToken(q(philiprbrenan), q(ddd)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1720  
1721 success "Create repository succeeded";
1722  
1723  
1724 =head2 createIssue($gitHub)
1725  
1726 Create an issue on L<GitHub|https://github.com/philiprbrenan>.
1727  
1728 Required: L<userid|/userid>, L<repository|/repository>, L<body|/body>, L<title|/title>.
1729  
1730 If the operation is successful, L<failed|/failed> is set to false otherwise it is set to true.
1731  
1732 Returns true if the issue was created successfully else false.
1733  
1734 Parameter Description
1735 1 $gitHub GitHub object
1736  
1737 B<Example:>
1738  
1739  
1740  
1741 gitHub(title=>q(Hello), body=>q(World))->createIssue; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1742  
1743 success "Create issue succeeded";
1744  
1745  
1746 =head2 createIssueFromSavedToken($userid, $repository, $title, $body, $accessFolderOrToken)
1747  
1748 Create an issue on L<GitHub|https://github.com/philiprbrenan> using an access token as supplied or saved in a file using L<savePersonalAccessToken|/savePersonalAccessToken>.
1749  
1750 Returns true if the issue was created successfully else false.
1751  
1752 Parameter Description
1753 1 $userid Userid on GitHub
1754 2 $repository Repository name
1755 3 $title Issue title
1756 4 $body Issue body
1757 5 $accessFolderOrToken Location of access token.
1758  
1759 B<Example:>
1760  
1761  
1762  
1763 &createIssueFromSavedToken(qw(philiprbrenan ddd hello World)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1764  
1765 success "Create issue succeeded";
1766  
1767  
1768 =head2 currentRepo()
1769  
1770 Create a github object for the current repo if we are on github actions
1771  
1772  
1773 =head2 createIssueInCurrentRepo($title, $body)
1774  
1775 Create an issue in the current GitHub repo if we are running on GitHub
1776  
1777 Parameter Description
1778 1 $title Title of issue
1779 2 $body Body of issue
1780  
1781 =head2 writeFileUsingSavedToken($userid, $repository, $file, $content, $accessFolderOrToken)
1782  
1783 Write to a file on L<GitHub|https://github.com/philiprbrenan> using a personal access token as supplied or saved in a file. Return B<1> on success or confess to any failure.
1784  
1785 Parameter Description
1786 1 $userid Userid on GitHub
1787 2 $repository Repository name
1788 3 $file File name on github
1789 4 $content File content
1790 5 $accessFolderOrToken Location of access token.
1791  
1792 B<Example:>
1793  
1794  
1795 my $s = q(HelloWorld);
1796  
1797 &writeFileUsingSavedToken(qw(philiprbrenan ddd hello.txt), $s); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1798  
1799 my $S = gitHub(repository=>q(ddd), gitFile=>q(hello.txt))->read;
1800  
1801 confess "Write file using saved token FAILED" unless $s eq $S;
1802 success "Write file using saved token succeeded";
1803  
1804  
1805 =head2 writeFileFromFileUsingSavedToken($userid, $repository, $file, $localFile, $accessFolderOrToken)
1806  
1807 Copy a file to L<GitHub|https://github.com/philiprbrenan> using a personal access token as supplied or saved in a file. Return B<1> on success or confess to any failure.
1808  
1809 Parameter Description
1810 1 $userid Userid on GitHub
1811 2 $repository Repository name
1812 3 $file File name on github
1813 4 $localFile File content
1814 5 $accessFolderOrToken Location of access token.
1815  
1816 B<Example:>
1817  
1818  
1819 my $f = writeFile(undef, my $s = "World
1820 ");
1821  
1822 &writeFileFromFileUsingSavedToken(qw(philiprbrenan ddd hello.txt), $f); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1823  
1824 my $S = gitHub(repository=>q(ddd), gitFile=>q(hello.txt))->read;
1825 confess "Write file from file using saved token FAILED" unless $s eq $S;
1826 success "Write file from file using saved token succeeded"
1827  
1828  
1829 =head2 writeFileFromCurrentRun($target, $text)
1830  
1831 Write to a file into the repository from the current run
1832  
1833 Parameter Description
1834 1 $target The target file name in the repo
1835 2 $text The text to write into this file
1836  
1837 =head2 writeFileFromFileFromCurrentRun($target)
1838  
1839 Write a file into the repository from the current run
1840  
1841 Parameter Description
1842 1 $target File name both locally and in the repo
1843  
1844 =head2 writeBinaryFileFromFileInCurrentRun($target, $source)
1845  
1846 Upload a binary file from the current run into the repo.
1847  
1848 Parameter Description
1849 1 $target The target file name in the repo
1850 2 $source The current file name in the run
1851  
1852 =head2 readFileUsingSavedToken($userid, $repository, $file, $accessFolderOrToken)
1853  
1854 Read from a file on L<GitHub|https://github.com/philiprbrenan> using a personal access token as supplied or saved in a file. Return the content of the file on success or confess to any failure.
1855  
1856 Parameter Description
1857 1 $userid Userid on GitHub
1858 2 $repository Repository name
1859 3 $file File name on github
1860 4 $accessFolderOrToken Location of access token.
1861  
1862 B<Example:>
1863  
1864  
1865 my $s = q(Hello to the World);
1866 &writeFileUsingSavedToken(qw(philiprbrenan ddd hello.txt), $s);
1867  
1868 my $S = &readFileUsingSavedToken (qw(philiprbrenan ddd hello.txt)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1869  
1870  
1871 confess "Read file using saved token FAILED" unless $s eq $S;
1872 success "Read file using saved token succeeded"
1873  
1874  
1875 =head2 writeFolderUsingSavedToken($userid, $repository, $targetFolder, $localFolder, $accessFolderOrToken)
1876  
1877 Write all the files in a local folder to a target folder on a named L<GitHub|https://github.com/philiprbrenan> repository using a personal access token as supplied or saved in a file.
1878  
1879 Parameter Description
1880 1 $userid Userid on GitHub
1881 2 $repository Repository name
1882 3 $targetFolder Target folder on github
1883 4 $localFolder Local folder name
1884 5 $accessFolderOrToken Location of access token.
1885  
1886 =head2 writeCommitUsingSavedToken($userid, $repository, $source, $accessFolderOrToken)
1887  
1888 Write all the files in a local folder to a named L<GitHub|https://github.com/philiprbrenan> repository using a personal access token as supplied or saved in a file.
1889  
1890 Parameter Description
1891 1 $userid Userid on GitHub
1892 2 $repository Repository name
1893 3 $source Local folder on github
1894 4 $accessFolderOrToken Optionally: location of access token.
1895  
1896 =head2 deleteFileUsingSavedToken($userid, $repository, $target, $accessFolderOrToken)
1897  
1898 Delete a file on GitHub using a saved token
1899  
1900 Parameter Description
1901 1 $userid Userid on GitHub
1902 2 $repository Repository name
1903 3 $target File on Github
1904 4 $accessFolderOrToken Optional: the folder containing saved access tokens
1905  
1906 =head2 getRepositoryUsingSavedToken($userid, $repository, $accessFolderOrToken)
1907  
1908 Get repository details using a saved token
1909  
1910 Parameter Description
1911 1 $userid Userid on GitHub
1912 2 $repository Repository name
1913 3 $accessFolderOrToken Optionally: location of access token.
1914  
1915 B<Example:>
1916  
1917  
1918  
1919 my $r = getRepositoryUsingSavedToken(q(philiprbrenan), q(aaa)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1920  
1921 success "Get repository using saved access token succeeded";
1922  
1923  
1924 =head2 getRepositoryUpdatedAtUsingSavedToken($userid, $repository, $accessFolderOrToken)
1925  
1926 Get repository 'updated_at' using a saved token and return the time in number of seconds since the Unix epoch.
1927  
1928 Parameter Description
1929 1 $userid Userid on GitHub
1930 2 $repository Repository name
1931 3 $accessFolderOrToken Optionally: location of access token.
1932  
1933 B<Example:>
1934  
1935  
1936  
1937 my $u = getRepositoryUpdatedAtUsingSavedToken(q(philiprbrenan), q(aaa)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1938  
1939 success "Get repository updated_at field succeeded";
1940  
1941  
1942 =head1 Access tokens
1943  
1944 Load and save access tokens. Some L<GitHub|https://github.com/philiprbrenan> requets must be signed with an L<Oauth|https://en.wikipedia.org/wiki/OAuth> access token. These methods allow you to store and reuse such tokens.
1945  
1946 =head2 savePersonalAccessToken($gitHub)
1947  
1948 Save a L<GitHub|https://github.com/philiprbrenan> personal access token by userid in folder L<personalAccessTokenFolder|/personalAccessTokenFolder>.
1949  
1950 Parameter Description
1951 1 $gitHub GitHub object
1952  
1953 B<Example:>
1954  
1955  
1956 my $d = temporaryFolder;
1957 my $t = join '', 1..20;
1958  
1959 my $g = gitHub
1960 (userid => q(philiprbrenan),
1961 personalAccessToken => $t,
1962 personalAccessTokenFolder => $d,
1963 );
1964  
1965  
1966 $g->savePersonalAccessToken; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1967  
1968 my $T = $g->loadPersonalAccessToken;
1969  
1970 confess "Load/Save token FAILED" unless $t eq $T;
1971 success "Load/Save token succeeded"
1972  
1973  
1974 =head2 loadPersonalAccessToken($gitHub)
1975  
1976 Load a personal access token by userid from folder L<personalAccessTokenFolder|/personalAccessTokenFolder>.
1977  
1978 Parameter Description
1979 1 $gitHub GitHub object
1980  
1981 B<Example:>
1982  
1983  
1984 my $d = temporaryFolder;
1985 my $t = join '', 1..20;
1986  
1987 my $g = gitHub
1988 (userid => q(philiprbrenan),
1989 personalAccessToken => $t,
1990 personalAccessTokenFolder => $d,
1991 );
1992  
1993 $g->savePersonalAccessToken;
1994  
1995 my $T = $g->loadPersonalAccessToken; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1996  
1997  
1998 confess "Load/Save token FAILED" unless $t eq $T;
1999 success "Load/Save token succeeded"
2000  
2001  
2002  
2003 =head2 GitHub::Crud Definition
2004  
2005  
2006 Attributes describing the interface with L<GitHub|https://github.com/philiprbrenan>.
2007  
2008  
2009  
2010  
2011 =head3 Input fields
2012  
2013  
2014 =head4 body
2015  
2016 The body of an issue.
2017  
2018 =head4 branch
2019  
2020 Branch name (you should create this branch first) or omit it for the default branch which is usually 'master'.
2021  
2022 =head4 confessOnFailure
2023  
2024 Confess to any failures
2025  
2026 =head4 gitFile
2027  
2028 File name on L<GitHub|https://github.com/philiprbrenan> - this name can contain '/'. This is the file to be read from, written to, copied from, checked for existence or deleted.
2029  
2030 =head4 gitFolder
2031  
2032 Folder name on L<GitHub|https://github.com/philiprbrenan> - this name can contain '/'.
2033  
2034 =head4 message
2035  
2036 Optional commit message
2037  
2038 =head4 nonRecursive
2039  
2040 Fetch only one level of files with L<list|https://en.wikipedia.org/wiki/Linked_list>.
2041  
2042 =head4 personalAccessToken
2043  
2044 A personal access token with scope "public_repo" as generated on page: https://github.com/settings/tokens.
2045  
2046 =head4 personalAccessTokenFolder
2047  
2048 The folder into which to save personal access tokens. Set to q(/etc/GitHubCrudPersonalAccessToken) by default.
2049  
2050 =head4 private
2051  
2052 Whether the repository being created should be private or not.
2053  
2054 =head4 repository
2055  
2056 The name of the repository to be worked on minus the userid - you should create this repository first manually.
2057  
2058 =head4 secret
2059  
2060 The secret for a web hook - this is created by the creator of the web hook and remembered by L<GitHub|https://github.com/philiprbrenan>,
2061  
2062 =head4 title
2063  
2064 The title of an issue.
2065  
2066 =head4 userid
2067  
2068 Userid on L<GitHub|https://github.com/philiprbrenan> of the repository to be worked on.
2069  
2070 =head4 webHookUrl
2071  
2072 The url for a web hook.
2073  
2074  
2075  
2076 =head3 Output fields
2077  
2078  
2079 =head4 failed
2080  
2081 Defined if the last request to L<GitHub|https://github.com/philiprbrenan> failed else B<undef>.
2082  
2083 =head4 fileList
2084  
2085 Reference to an array of files produced by L<list|/list>.
2086  
2087 =head4 readData
2088  
2089 Data produced by L<read|/read>.
2090  
2091 =head4 response
2092  
2093 A reference to L<GitHub|https://github.com/philiprbrenan>'s response to the latest request.
2094  
2095  
2096  
2097 =head2 GitHub::Crud::Response Definition
2098  
2099  
2100 Attributes describing a response from L<GitHub|https://github.com/philiprbrenan>.
2101  
2102  
2103  
2104  
2105 =head3 Output fields
2106  
2107  
2108 =head4 content
2109  
2110 The actual content of the file from L<GitHub|https://github.com/philiprbrenan>.
2111  
2112 =head4 data
2113  
2114 The data received from L<GitHub|https://github.com/philiprbrenan>, normally in L<Json|https://en.wikipedia.org/wiki/JSON> format.
2115  
2116 =head4 status
2117  
2118 Our version of Status.
2119  
2120  
2121  
2122 =head1 Index
2123  
2124  
2125 1 L<copy|/copy> - Copy a source file from one location to another target location in your L<GitHub|https://github.com/philiprbrenan> repository, overwriting the target file if it already exists.
2126  
2127 2 L<createIssue|/createIssue> - Create an issue on L<GitHub|https://github.com/philiprbrenan>.
2128  
2129 3 L<createIssueFromSavedToken|/createIssueFromSavedToken> - Create an issue on L<GitHub|https://github.com/philiprbrenan> using an access token as supplied or saved in a file using L<savePersonalAccessToken|/savePersonalAccessToken>.
2130  
2131 4 L<createIssueInCurrentRepo|/createIssueInCurrentRepo> - Create an issue in the current GitHub repo if we are running on GitHub
2132  
2133 5 L<createPushWebHook|/createPushWebHook> - Create a web hook for your L<GitHub|https://github.com/philiprbrenan> userid.
2134  
2135 6 L<createRepository|/createRepository> - Create a repository on L<GitHub|https://github.com/philiprbrenan>.
2136  
2137 7 L<createRepositoryFromSavedToken|/createRepositoryFromSavedToken> - Create a repository on L<GitHub|https://github.com/philiprbrenan> using an access token either as supplied or saved in a file using L<savePersonalAccessToken|/savePersonalAccessToken>.
2138  
2139 8 L<currentRepo|/currentRepo> - Create a github object for the current repo if we are on github actions
2140  
2141 9 L<delete|/delete> - Delete a file from L<GitHub|https://github.com/philiprbrenan>.
2142  
2143 10 L<deleteFileUsingSavedToken|/deleteFileUsingSavedToken> - Delete a file on GitHub using a saved token
2144  
2145 11 L<exists|/exists> - Test whether a file exists on L<GitHub|https://github.com/philiprbrenan> or not and returns an object including the B<sha> and B<size> fields if it does else L<undef|https://perldoc.perl.org/functions/undef.html>.
2146  
2147 12 L<getRepository|/getRepository> - Get the overall details of a repository
2148  
2149 13 L<getRepositoryUpdatedAtUsingSavedToken|/getRepositoryUpdatedAtUsingSavedToken> - Get repository 'updated_at' using a saved token and return the time in number of seconds since the Unix epoch.
2150  
2151 14 L<getRepositoryUsingSavedToken|/getRepositoryUsingSavedToken> - Get repository details using a saved token
2152  
2153 15 L<list|/list> - List all the files contained in a L<GitHub|https://github.com/philiprbrenan> repository or all the files below a specified folder in the repository.
2154  
2155 16 L<listCommits|/listCommits> - List all the commits in a L<GitHub|https://github.com/philiprbrenan> repository.
2156  
2157 17 L<listCommitShas|/listCommitShas> - Create {commit name => sha} from the results of L<listCommits>.
2158  
2159 18 L<listRepositories|/listRepositories> - List the repositories accessible to a user on L<GitHub|https://github.com/philiprbrenan>.
2160  
2161 19 L<listWebHooks|/listWebHooks> - List web hooks associated with your L<GitHub|https://github.com/philiprbrenan> repository.
2162  
2163 20 L<loadPersonalAccessToken|/loadPersonalAccessToken> - Load a personal access token by userid from folder L<personalAccessTokenFolder|/personalAccessTokenFolder>.
2164  
2165 21 L<new|/new> - Create a new L<GitHub|https://github.com/philiprbrenan> object with attributes as described at: L<GitHub::Crud Definition>.
2166  
2167 22 L<read|/read> - Read data from a file on L<GitHub|https://github.com/philiprbrenan>.
2168  
2169 23 L<readBlob|/readBlob> - Read a L<blob|https://en.wikipedia.org/wiki/Binary_large_object> from L<GitHub|https://github.com/philiprbrenan>.
2170  
2171 24 L<readFileUsingSavedToken|/readFileUsingSavedToken> - Read from a file on L<GitHub|https://github.com/philiprbrenan> using a personal access token as supplied or saved in a file.
2172  
2173 25 L<rename|/rename> - Rename a source file on L<GitHub|https://github.com/philiprbrenan> if the target file name is not already in use.
2174  
2175 26 L<savePersonalAccessToken|/savePersonalAccessToken> - Save a L<GitHub|https://github.com/philiprbrenan> personal access token by userid in folder L<personalAccessTokenFolder|/personalAccessTokenFolder>.
2176  
2177 27 L<specialFileData|/specialFileData> - Do not encode or decode data with a known file signature
2178  
2179 28 L<write|/write> - Write utf8 data into a L<GitHub|https://github.com/philiprbrenan> file.
2180  
2181 29 L<writeBinaryFileFromFileInCurrentRun|/writeBinaryFileFromFileInCurrentRun> - Upload a binary file from the current run into the repo.
2182  
2183 30 L<writeBlob|/writeBlob> - Write data into a L<GitHub|https://github.com/philiprbrenan> as a L<blob|https://en.wikipedia.org/wiki/Binary_large_object> that can be referenced by future commits.
2184  
2185 31 L<writeCommit|/writeCommit> - Write all the files in a B<$folder> (or just the the named files) into a L<GitHub|https://github.com/philiprbrenan> repository in parallel as a commit on the specified branch.
2186  
2187 32 L<writeCommitUsingSavedToken|/writeCommitUsingSavedToken> - Write all the files in a local folder to a named L<GitHub|https://github.com/philiprbrenan> repository using a personal access token as supplied or saved in a file.
2188  
2189 33 L<writeFileFromCurrentRun|/writeFileFromCurrentRun> - Write to a file into the repository from the current run
2190  
2191 34 L<writeFileFromFileFromCurrentRun|/writeFileFromFileFromCurrentRun> - Write a file into the repository from the current run
2192  
2193 35 L<writeFileFromFileUsingSavedToken|/writeFileFromFileUsingSavedToken> - Copy a file to L<GitHub|https://github.com/philiprbrenan> using a personal access token as supplied or saved in a file.
2194  
2195 36 L<writeFileUsingSavedToken|/writeFileUsingSavedToken> - Write to a file on L<GitHub|https://github.com/philiprbrenan> using a personal access token as supplied or saved in a file.
2196  
2197 37 L<writeFolderUsingSavedToken|/writeFolderUsingSavedToken> - Write all the files in a local folder to a target folder on a named L<GitHub|https://github.com/philiprbrenan> repository using a personal access token as supplied or saved in a file.
2198  
2199 =head1 Installation
2200  
2201 This module is written in 100% Pure Perl and, thus, it is easy to read,
2202 comprehend, use, modify and install via B<cpan>:
2203  
2204 sudo cpan install GitHub::Crud
2205  
2206 =head1 Author
2207  
2208 L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com>
2209  
2210 L<http://www.appaapps.com|http://www.appaapps.com>
2211  
2212 =head1 Copyright
2213  
2214 Copyright (c) 2016-2021 Philip R Brenan.
2215  
2216 This module is free software. It may be used, redistributed and/or modified
2217 under the same terms as Perl itself.
2218  
2219 =cut
2220  
2221  
2222  
2223 # Tests and documentation
2224  
2225 sub test
2226 1 ▶ {my $p = __PACKAGE__;
Subroutine test: called CC=5 SCAR=16.6
Pod undocumented
2227 1 binmode($_, ":utf8") for *STDOUT, *STDERR;
2228 1 ▶ return if eval "eof(${p}::DATA)";
Branch: 1 branch
BranchTrueFalseTotal
if eval "eof(${p}::DATA)" 0 1 1
2229 1 my $s = eval "join('', <${p}::DATA>)";
2230 1 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
2231 1 ▶ eval $s;
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine success: not called CC=1 SCAR=0.1
Pod undocumented
2232 1 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
2233 1 1
2234 }
2235  
2236 1 ▶test unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
2237  
2238 1 1;
2239 #podDocumentation
2240 __DATA__
↑ Summary Build »