blib/lib/GitHub/Crud.pm

filter: Statementstmt 13.4% 90 / 668 Branchbran 3.6% 11 / 300 Conditioncond 2.1% 2 / 92 MC/DCmcdc 0.0% 0 / 57 Subroutinesub 28.9% 22 / 76 Podpod 71.1% 37 / 52 totaltotal 13.0% 162 / 1245 CC 192 SCAR 43.8
CC
192
Cov
10%
new
342.0
write
275.8
specialFileData
110.0
CRAP 80.2
↑ 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 = 20210211;
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 1 ▶use Date::Manip;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Encode qw(encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use utf8; # To allow utf8 constants for testing
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 0 ▶sub url { "https://api.github.com/repos" } # Github repository api url
Subroutine url: not called CC=1 SCAR=6.9
Pod undocumented
23 0 ▶sub api { "https://api.github.com/" } # Github api url
Subroutine api: not called CC=1 SCAR=6.9
Pod undocumented
24 3 ▶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: called CC=1 SCAR=0.0
Pod undocumented
25  
26 1 my %shas; # L<SHA> digests already seen - used to optimize write and delete
27  
28 sub GitHub::Crud::Response::new($$) #P Execute a request against L<GitHub> and decode the response
29 0 ▶ {my ($gitHub, $request) = @_; # Github, request string
Subroutine new: not called CC=18 SCAR=58.3
30  
31 0 my $R = bless {command=>$request}, "GitHub::Crud::Response"; # Construct the response
32  
33 0 my $r = xxx $request, qr(HTTP);
34  
35 0 $r =~ s/\r//gs; # Internet line ends
36 0 my ($http, @r) = split /\n/, $r;
37 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
38 0 {shift @r; $http = shift @r;
39 }
40  
41 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
42 0 {my $ps = 0; # Parse the response
43 0 my @data;
44 my %can;
45  
46 0 for(@r)
47 0 ▶ {if ($ps == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($ps == 0) { } 0 0 0
48 0 ▶ {if (length == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 0) { } 0 0 0
49 0 {$ps = 1;
50 }
51 else
52 0 {my ($name, $content) = split /\s*:\s*/, $_, 2; # Parse each header
53 0 $name =~ s/-/_/gs; # Translate - in names to _
54 0 ▶ if ($R->can($name))
Branch: 1 branch
BranchTrueFalseTotal
if ($R->can($name)) { } 0 0 0
55 0 {$R->$name = $content;
56 }
57 0 else {$can{$name}++} # Update list of new methods required
58 }
59 }
60 else
61 0 {push @data, $_;
62 }
63 }
64  
65 0 ▶ if (keys %can) # List of new methods required
Branch: 1 branch
BranchTrueFalseTotal
if (keys %can) 0 0 0
66 0 {lll "Add the following fields to package GitHub::Crud::Response";
67 0 say STDERR " $_=> undef," for(sort keys %can);
68 }
69  
70 0 ▶ if (@data) # Save any data
Branch: 1 branch
BranchTrueFalseTotal
if (@data) 0 0 0
71 0 {my $j = join ' ', @data;
72 0 my $p = $R->data = bless decodeJson($j), "GitHub::Crud::Response::Data";
73 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
74 0 {$R->content = decodeBase64($c); # Decode the data
75 }
76 }
77  
78 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
79  
80 0 return $gitHub->response = $R; # Return successful response
81 }
82 else
83 0 {confess "Unexpected response from GitHub:\n$r\n$request\n"; # Confess to failure
84 }
85 }
86  
87 1 genHash(q(GitHub::Crud::Response), # Attributes describing a response from L<GitHub>.
88 Accept_Ranges => undef,
89 access_control_allow_origin => undef,
90 Access_Control_Allow_Origin => undef,
91 access_control_expose_headers => undef,
92 Access_Control_Expose_Headers => undef,
93 cache_control => undef,
94 Cache_Control => undef,
95 Connection => undef,
96 content_length => undef,
97 Content_Length => undef,
98 content_security_policy => undef,
99 Content_Security_Policy => undef,
100 content_type => undef,
101 Content_Type => undef,
102 content => undef, # The actual content of the file from L<GitHub>.
103 data => undef, # The data received from L<GitHub>, normally in L<json> format.
104 date => undef,
105 Date => undef,
106 etag => undef,
107 ETag => undef,
108 Expires => undef,
109 last_modified => undef,
110 Last_Modified => undef,
111 Link => undef,
112 Location => undef,
113 referrer_policy => undef,
114 Referrer_Policy => undef,
115 server => undef,
116 Server => undef,
117 Source_Age => undef,
118 Status => undef,
119 status => undef, # Our version of Status.
120 strict_transport_security => undef,
121 Strict_Transport_Security => undef,
122 vary => undef,
123 Vary => undef,
124 Via => undef,
125 x_accepted_oauth_scopes => undef,
126 X_Accepted_OAuth_Scopes => undef,
127 X_Cache_Hits => undef,
128 X_Cache => undef,
129 x_content_type_options => undef,
130 X_Content_Type_Options => undef,
131 X_Content_Type => undef,
132 X_Fastly_Request_ID => undef,
133 x_frame_options => undef,
134 X_Frame_Options => undef,
135 X_Geo_Block_List => undef,
136 x_github_media_type => undef,
137 X_GitHub_Media_Type => undef,
138 x_github_request_id => undef,
139 X_GitHub_Request_Id => undef,
140 x_oauth_scopes => undef,
141 X_OAuth_Scopes => undef,
142 x_ratelimit_limit => undef,
143 X_RateLimit_Limit => undef,
144 x_ratelimit_remaining => undef,
145 X_RateLimit_Remaining => undef,
146 x_ratelimit_reset => undef,
147 X_RateLimit_Reset => undef,
148 x_ratelimit_used => undef,
149 X_RateLimit_Used => undef,
150 X_Runtime_rack => undef,
151 X_Served_By => undef,
152 X_Timer => undef,
153 x_xss_protection => undef,
154 X_XSS_Protection => undef,
155 );
156  
157 1 genHash(q(GitHub::Crud::Response::Data), # Response from a request made to L<GitHub>.
158 command => undef,
159 content => undef,
160 documentation_url => undef,
161 download_url => undef,
162 encoding => undef,
163 git => undef,
164 git_url => undef,
165 html => undef,
166 html_url => undef,
167 _links => undef,
168 message => undef,
169 name => undef,
170 path => undef,
171 self => undef,
172 sha => undef,
173 size => undef,
174 type => undef,
175 url => undef,
176 );
177  
178 sub getSha($) #P Compute L<sha> for data after encoding any unicode characters as utf8.
179 0 ▶ {my ($data) = @_; # String possibly containing non ascii code points
Subroutine getSha: not called CC=2 SCAR=17.9
Pod undocumented
180  
181 0 my $length = length($data);
182 0 my $blob = 'blob' . " $length\0" . $data;
183 0 utf8::encode($blob);
184 0 my $r = eval{sha1_hex($blob)};
185 0 ▶ confess $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
186 0 $r
187 }
188  
189 1 if (0) # Test L<sha>
190 {my $sha = getSha("<h1>Hello World</h1>\n");
191 my $Sha = "f3e333e80d224c631f2ff51b9b9f7189ad349c15";
192 unless($sha eq $Sha)
193 {confess "Wrong SHA: $sha".
194 "Should be: $Sha";
195 }
196 confess "getSha success";
197 }
198  
199 sub shaKey($;$) #P Add a L<SHA> key to a L<url>
200 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
201 0 ▶ filePath($gitHub->repository,
Branch: 1 branch
BranchTrueFalseTotal
$fileData ? : 0 0 0
202 $fileData ? ($fileData->path, $fileData->name) : $gitHub->gitFile)
203 }
204  
205 sub saveSha($$) #P Save the L<sha> of a file
206 0 ▶ {my ($gitHub, $fileData) = @_; # Github, file details returned by list or exists
Subroutine saveSha: not called CC=1 SCAR=6.9
Pod undocumented
207 0 $shas{$gitHub->shaKey($fileData)} = $fileData->sha;
208 }
209  
210 sub copySha($) #P Save the L<sha> of a file just read to a file just about to be written
211 0 ▶ {my ($gitHub) = @_; # Github
Subroutine copySha: not called CC=1 SCAR=6.9
Pod undocumented
212 0 $shas{$gitHub->shaKey} = $gitHub->response->data->sha;
213 }
214  
215 sub getExistingSha($) #P Get the L<sha> of a file that already exists
216 0 ▶ {my ($gitHub) = @_; # Github
Subroutine getExistingSha: not called CC=3 SCAR=24.8
Pod undocumented
217 0 my $s = $shas{$gitHub->shaKey}; # Get the L<sha> from the cache
218 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
219 0 my $r = $gitHub->exists; # Get the L<sha> of the file via exists if the file exists
220 0 ▶ return $r->sha if $r; # L<sha> of existing file
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
221 undef # Undef if no such file
222 0 }
223  
224 sub deleteSha($) #P Delete a L<sha> that is no longer valid
225 0 ▶ {my ($gitHub) = @_; # Github
Subroutine deleteSha: not called CC=1 SCAR=6.9
Pod undocumented
226 0 $shas{$gitHub->shaKey} = undef # Mark the L<sha> as deleted
227 }
228  
229 sub qm($) #P Quotemeta extended to include undef
230 1 ▶ {my ($s) = @_; # String to quote
Subroutine qm: called CC=2 SCAR=7.0
Pod undocumented
231 1 ▶ return '' unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 1 1
232 1 $s =~ s((\'|\"|\\)) (\\$1)gs;
233 1 $s =~ s(\s) (%20)gsr; # Url encode blanks
234 }
235  
236 sub patKey($) #P Create an authorization header by locating an appropriate personal access token
237 0 ▶ {my ($gitHub) = @_; # GitHub
Subroutine patKey: not called CC=3 SCAR=24.8
Pod undocumented
238  
239 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
240  
241 0 ▶ if (my $pat = $gitHub->personalAccessToken) # User supplied personal access token explicitly
Branch: 1 branch
BranchTrueFalseTotal
if (my $pat = $gitHub->personalAccessToken) 0 0 0
242 0 {return "-H \"Authorization: token $pat\""
243 }
244  
245 0 confess "Personal access token required with scope \"public_repo\"". # We must have a personal access token to do anything useful!
246 " as generated on page:\nhttps://github.com/settings/tokens";
247 }
248  
249 sub refOrBranch($$) #P Add a ref or branch keyword
250 0 ▶ {my ($gitHub, $ref) = @_; # Github, whether to use ref rather than branch
Subroutine refOrBranch: not called CC=5 SCAR=34.0
Pod undocumented
251 0 my $b = $gitHub->branch;
252 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
253 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
254 0 ''
255 }
256  
257 sub gitHub(%) #P Create a test L<GitHub> object
258 0 ▶ {my (%options) = @_; # Options
Subroutine gitHub: not called CC=1 SCAR=6.9
Pod undocumented
259 0 GitHub::Crud::new
260 (userid => q(philiprbrenan),
261 repository => q(aaa),
262 confessOnFailure => 1,
263 @_);
264 }
265  
266 #D1 Constructor # Create a L<github> object with the specified attributes describing the interface with L<github>.
267  
268 sub new(@) # Create a new L<GitHub> object with attributes as described at: L<GitHub::Crud Definition>.
269 1 ▶ {my (%attributes) = @_; # Attribute values
Subroutine new: called CC=3 SCAR=11.3
Pod documented
270  
271 1 my $curl = qx(curl -V); # Check Curl
272 1 ▶ if ($curl =~ /command not found/)
Branch: 1 branch
BranchTrueFalseTotal
if ($curl =~ /command not found/u) 0 1 1
273 0 {confess "Command curl not found"
274 }
275  
276 1 my $g = genHash(__PACKAGE__, # Attributes describing the interface with L<github>.
277 body => undef, #I The body of an issue.
278 branch => undef, #I Branch name (you should create this branch first) or omit it for the default branch which is usually 'master'.
279 confessOnFailure => undef, #I Confess to any failures
280 failed => undef, # Defined if the last request to L<GitHub> failed else B<undef>.
281 fileList => undef, # Reference to an array of files produced by L<list|/list>.
282 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.
283 gitFolder => undef, #I Folder name on L<GitHub> - this name can contain '/'.
284 message => undef, #I Optional commit message
285 nonRecursive => undef, #I Fetch only one level of files with L<list>.
286 personalAccessToken => undef, #I A personal access token with scope "public_repo" as generated on page: https://github.com/settings/tokens.
287 personalAccessTokenFolder => accessFolder, #I The folder into which to save personal access tokens. Set to q(/etc/GitHubCrudPersonalAccessToken) by default.
288 private => undef, #I Whether the repository being created should be private or not.
289 readData => undef, # Data produced by L<read|/read>.
290 repository => undef, #I The name of the repository to be worked on minus the userid - you should create this repository first manually.
291 response => undef, # A reference to L<GitHub>'s response to the latest request.
292 secret => undef, #I The secret for a web hook - this is created by the creator of the web hook and remembered by L<GitHub>,
293 title => undef, #I The title of an issue.
294 webHookUrl => undef, #I The url for a web hook.
295 userid => undef, #I Userid on L<GitHub> of the repository to be worked on.
296 );
297  
298 1 $g->$_ = $attributes{$_} for sort keys %attributes;
299  
300 1 $g
301 }
302  
303 #D1 Files # File actions on the contents of L<GitHub> repositories.
304  
305 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.
306 0 ▶ {my ($gitHub) = @_; # GitHub
Subroutine list: not called CC=8 SCAR=42.8
Pod documented
307 my $r = sub # Get contents
308 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
309 0 ▶ my $repo = qm $gitHub->repository; $repo or confess "repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repo or confess("repository required") 0 0 0
310 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
311 0 my $bran = qm $gitHub->refOrBranch(1);
312 0 my $pat = $gitHub->patKey(0);
313 0 my $url = url;
314 0 my $s = filePath
315 ("curl -si $pat $url", $user, $repo, qq(contents), $path.$bran);
316 0 GitHub::Crud::Response::new($gitHub, $s);
317 0 }->();
318  
319 0 my $failed = $gitHub->failed = $r->status != 200; # Check response code
320 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
321  
322 0 ▶ if ($failed) # Failed to retrieve a list of files
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) { } 0 0 0
323 0 {$gitHub->fileList = [];
324 }
325 else
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=5 SCAR=34.0
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) # Look for requested file in file listing
Branch: 1 branch
BranchTrueFalseTotal
unless ($gitHub->failed) 0 0 0
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=2 SCAR=17.9
Pod documented
923 0 ▶ if (my $g = currentRepo) # We are on GitHub
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = &currentRepo()) 0 0 0
924 0 {$g->gitFile = $target;
925 0 $g->write(scalar(readFile($target)));
926 }
927 }
928  
929 sub writeBinaryFileFromFileInCurrentRun($$) # Upload a binary file from the current run into the repo.
930 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
931 0 ▶ if (my $g = currentRepo) # We are on GitHub
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = &currentRepo()) 0 0 0
932 0 {$g->gitFile = $target;
933 0 $g->write(readBinaryFile($source));
934 }
935 }
936  
937 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.
938 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
939 0 my $g = GitHub::Crud::new;
940 0 $g->userid = $userid;
941 0 $g->repository = $repository;
942 0 $g->gitFile = $file;
943 0 $g->personalAccessTokenFolder = $accessFolderOrToken;
944 0 $g->loadPersonalAccessToken;
945 0 $g->read;
946 }
947  
948 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.
949 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
950 0 my $g = GitHub::Crud::new;
951 0 $g->userid = $userid;
952 0 $g->repository = $repository;
953 0 $g->personalAccessTokenFolder = $accessFolderOrToken;
954 0 $g->loadPersonalAccessToken;
955  
956 0 for my $file(searchDirectoryTreesForMatchingFiles($localFolder))
957 0 {$g->gitFile = swapFilePrefix($file, $localFolder, $targetFolder);
958 0 $g->write(readBinaryFile($file));
959 }
960 }
961  
962 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.
963 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
964 0 my $g = GitHub::Crud::new;
965 0 $g->userid = $userid;
966 0 $g->repository = $repository;
967 0 $g->personalAccessTokenFolder = $accessFolderOrToken;
968 0 $g->loadPersonalAccessToken;
969 0 $g->branch = 'master';
970  
971 0 $g->writeCommit($source);
972 }
973  
974 sub deleteFileUsingSavedToken($$$;$) # Delete a file on GitHub using a saved token
975 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
976 0 my $g = GitHub::Crud::new;
977 0 $g->userid = $userid;
978 0 $g->repository = $repository;
979 0 $g->personalAccessTokenFolder = $accessFolderOrToken;
980 0 $g->loadPersonalAccessToken;
981  
982 0 $g->gitFile = $target;
983 0 $g->delete;
984 }
985  
986 sub getRepositoryUsingSavedToken($$;$) # Get repository details using a saved token
987 1 ▶ {my ($userid, $repository, $accessFolderOrToken) = @_; # Userid on GitHub, repository name, optionally: location of access token.
Subroutine getRepositoryUsingSavedToken: called CC=3 SCAR=11.3
Pod documented
988 1 my $g = GitHub::Crud::new;
989 1 ▶ $g->userid = $userid; $userid or confess "Userid required";
Branch: 1 branch
BranchTrueFalseTotal
$userid or confess("Userid required") 0 1 1
990 1 ▶ $g->repository = $repository; $repository or confess "Repository required";
Branch: 1 branch
BranchTrueFalseTotal
$repository or confess("Repository required") 0 1 1
991 1 $g->personalAccessTokenFolder = $accessFolderOrToken;
992 1 $g->loadPersonalAccessToken;
993 0 $g->getRepository;
994 }
995  
996 sub getRepositoryUpdatedAtUsingSavedToken($$;$) # Get repository 'updated_at' using a saved token and return the time in number of seconds since the Unix epoch.
997 1 ▶ {my ($userid, $repository, $accessFolderOrToken) = @_; # Userid on GitHub, repository name, optionally: location of access token.
Subroutine getRepositoryUpdatedAtUsingSavedToken: called CC=1 SCAR=1.2
Pod documented
998 1 my $r = &getRepositoryUsingSavedToken(@_); # Get repository details using a saved token
999 0 my $u = $r->data->{updated_at};
1000 0 return Date::Manip::UnixDate($u,'%s');
1001 }
1002  
1003 #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.
1004  
1005 sub savePersonalAccessToken($) # Save a L<GitHub> personal access token by userid in folder L<personalAccessTokenFolder|/personalAccessTokenFolder>.
1006 0 ▶ {my ($gitHub) = @_; # GitHub object
Subroutine savePersonalAccessToken: not called CC=6 SCAR=37.4
Pod documented
1007 0 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 0 0
1008 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
1009 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
1010 0 my $file = filePathExt($dir, $user, q(data));
1011 0 makePath($file);
1012 0 storeFile($file, {pat=>$pat}); # Store personal access token
1013 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
1014 0 my $p = retrieveFile $file; # Retrieve access token to check that we wrote it successfully
1015 $pat eq $p->{pat} or # Check file format
1016 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
1017 }
1018  
1019 sub loadPersonalAccessToken($) # Load a personal access token by userid from folder L<personalAccessTokenFolder|/personalAccessTokenFolder>.
1020 1 ▶ {my ($gitHub) = @_; # GitHub object
Subroutine loadPersonalAccessToken: called CC=7 SCAR=25.7
Pod documented
1021 1 ▶ my $user = qm $gitHub->userid; $user or confess "userid required";
Branch: 1 branch
BranchTrueFalseTotal
$user or confess("userid required") 0 1 1
1022  
1023 1 ▶ if (length($gitHub->personalAccessTokenFolder//accessFolder) == 43) # Access token supplied directly
Branch: 1 branch
BranchTrueFalseTotal
if (length($gitHub->personalAccessTokenFolder // accessFolder()) == 43) 0 1 1
Condition33%1/3
$gitHub->personalAccessTokenFolder // accessFolder()
l!l&&r!l&&!r
010
MC/DC0%0/2
$gitHub->personalAccessTokenFolder // accessFolder()
$gitHub->personalAccessTokenFolder accessFolder()
Truth table33%1/3
A // B
A= $gitHub->personalAccessTokenFolderB= accessFolder()
ABresult
1X1
011
000
1024 0 {return $gitHub->personalAccessToken = $gitHub->personalAccessTokenFolder;
1025 }
1026  
1027 1 ▶ if ($ENV{GITHUB_TOKEN}) # Access token supplied through environment
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"GITHUB_TOKEN"}) 0 1 1
1028 0 {return $gitHub->personalAccessToken = $ENV{GITHUB_TOKEN};
1029 }
1030  
1031 1 ▶ my $dir = $gitHub->personalAccessTokenFolder // accessFolder;
Condition33%1/3
$gitHub->personalAccessTokenFolder // accessFolder()
l!l&&r!l&&!r
010
MC/DC0%0/2
$gitHub->personalAccessTokenFolder // accessFolder()
$gitHub->personalAccessTokenFolder accessFolder()
Truth table33%1/3
A // B
A= $gitHub->personalAccessTokenFolderB= accessFolder()
ABresult
1X1
011
000
1032 1 my $file = filePathExt($dir, $user, q(data));
1033 1 my $p = retrieveFile $file; # Load personal access token
1034 my $a = $p->{pat} or # Check file format
1035 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
1036 0 $gitHub->personalAccessToken = $a; # Retrieve token
1037 }
1038  
1039 #D0
1040 #-------------------------------------------------------------------------------
1041 # Export - eeee
1042 #-------------------------------------------------------------------------------
1043  
1044 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
1045  
1046 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
1047  
1048 # containingFolder
1049  
1050 1 @ISA = qw(Exporter);
1051 1 @EXPORT_OK = qw(
1052 createIssueFromSavedToken
1053 createIssueInCurrentRepo
1054 createRepositoryFromSavedToken
1055 deleteFileUsingSavedToken
1056 getRepository
1057 getRepositoryUsingSavedToken
1058 getRepositoryUpdatedAtUsingSavedToken
1059 readFileUsingSavedToken
1060 writeBinaryFileFromFileInCurrentRun
1061 writeCommitUsingSavedToken
1062 writeFileFromCurrentRun
1063 writeFileFromFileUsingSavedToken
1064 writeFileUsingSavedToken
1065 writeFolderUsingSavedToken
1066 );
1067 1 %EXPORT_TAGS = (all=>[@EXPORT_OK]);
1068  
1069 #podDocumentation
1070  
1071 =pod
1072  
1073 =encoding utf-8
1074  
1075 =head1 Name
1076  
1077 GitHub::Crud - Create, Read, Update, Delete files, commits, issues, and web hooks on GitHub.
1078  
1079 =head1 Synopsis
1080  
1081 Create, Read, Update, Delete files, commits, issues, and web hooks on GitHub as
1082 described at:
1083  
1084 https://developer.github.com/v3/repos/contents/#update-a-file
1085  
1086 Commit a folder to GitHub then read and check some of the uploaded content:
1087  
1088 use GitHub::Crud;
1089 use Data::Table::Text qw(:all);
1090  
1091 my $f = temporaryFolder; # Folder in which we will create some files to upload in the commit
1092 my $c = dateTimeStamp; # Create some content
1093 my $if = q(/home/phil/.face); # Image file
1094  
1095 writeFile(fpe($f, q(data), $_, qw(txt)), $c) for 1..3; # Place content in files in a sub folder
1096 copyBinaryFile $if, my $If = fpe $f, qw(face jpg); # Add an image
1097  
1098 my $g = GitHub::Crud::new # Create GitHub
1099 (userid => q(philiprbrenan),
1100 repository => q(aaa),
1101 branch => q(test),
1102 confessOnFailure => 1);
1103  
1104 $g->loadPersonalAccessToken; # Load a personal access token
1105 $g->writeCommit($f); # Upload commit - confess to any errors
1106  
1107 my $C = $g->read(q(data/1.txt)); # Read data written in commit
1108 my $I = $g->read(q(face.jpg));
1109 my $i = readBinaryFile $if;
1110  
1111 confess "Date stamp failed" unless $C eq $c; # Check text
1112 confess "Image failed" unless $i eq $I; # Check image
1113 confess "Write commit succeeded";
1114  
1115 =head1 Prerequisites
1116  
1117 sudo apt-get install curl
1118  
1119 =head1 Description
1120  
1121 Create, Read, Update, Delete files, commits, issues, and web hooks on GitHub.
1122  
1123  
1124 Version 20210211.
1125  
1126  
1127 The following sections describe the methods in each functional area of this
1128 module. For an alphabetic listing of all methods by name see L<Index|/Index>.
1129  
1130  
1131  
1132 =head1 Constructor
1133  
1134 Create a L<GitHub|https://github.com/philiprbrenan> object with the specified attributes describing the interface with L<GitHub|https://github.com/philiprbrenan>.
1135  
1136 =head2 new(%attributes)
1137  
1138 Create a new L<GitHub|https://github.com/philiprbrenan> object with attributes as described at: L<GitHub::Crud Definition>.
1139  
1140 Parameter Description
1141 1 %attributes Attribute values
1142  
1143 B<Example:>
1144  
1145  
1146 my $f = temporaryFolder; # Folder in which we will create some files to upload in the commit
1147 my $c = dateTimeStamp; # Create some content
1148 my $if = q(/home/phil/.face); # Image file
1149
1150 writeFile(fpe($f, q(data), $_, qw(txt)), $c) for 1..3; # Place content in files in a sub folder
1151 copyBinaryFile $if, my $If = fpe $f, qw(face jpg); # Add an image
1152
1153
1154 my $g = GitHub::Crud::new # Create GitHub # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1155  
1156 (userid => q(philiprbrenan),
1157 repository => q(aaa),
1158 branch => q(test),
1159 confessOnFailure => 1);
1160
1161 $g->loadPersonalAccessToken; # Load a personal access token
1162 $g->writeCommit($f); # Upload commit - confess to any errors
1163
1164 my $C = $g->read(q(data/1.txt)); # Read data written in commit
1165 my $I = $g->read(q(face.jpg));
1166 my $i = readBinaryFile $if;
1167
1168 confess "Date stamp failed" unless $C eq $c; # Check text
1169 confess "Image failed" unless $i eq $I; # Check image
1170 success "Write commit succeeded";
1171
1172  
1173 =head1 Files
1174  
1175 File actions on the contents of L<GitHub|https://github.com/philiprbrenan> repositories.
1176  
1177 =head2 list($gitHub)
1178  
1179 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.
1180  
1181 Required attributes: L<userid|/userid>, L<repository|/repository>.
1182  
1183 Optional attributes: L<gitFolder|/gitFolder>, L<refOrBranch|/refOrBranch>, L<nonRecursive|/nonRecursive>, L<patKey|/patKey>.
1184  
1185 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.
1186  
1187 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.
1188  
1189 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.
1190  
1191 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.
1192  
1193 Returns the list of file names found or empty list if no files were found.
1194  
1195 Parameter Description
1196 1 $gitHub GitHub
1197  
1198 B<Example:>
1199  
1200  
1201
1202 success "list:", gitHub->list; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1203  
1204
1205 # list: alpha.data .github/workflows/test.yaml images/aaa.txt images/aaa/bbb.txt # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1206  
1207
1208  
1209 =head2 specialFileData($d)
1210  
1211 Do not encode or decode data with a known file signature
1212  
1213 Parameter Description
1214 1 $d String to check
1215  
1216 =head2 read($gitHub, $File)
1217  
1218 Read data from a file on L<GitHub|https://github.com/philiprbrenan>.
1219  
1220 Required attributes: L<userid|/userid>, L<repository|/repository>.
1221  
1222 Optional attributes: L<gitFile|/gitFile> = the file to read, L<refOrBranch|/refOrBranch>, L<patKey|/patKey>.
1223  
1224 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.
1225  
1226 If the read operation fails then L<failed|/failed> is set to true and L<readData|/readData> is set to B<undef>.
1227  
1228 Returns the data read or B<undef> if no file was found.
1229  
1230 Parameter Description
1231 1 $gitHub GitHub
1232 2 $File File o read if not specified in gitFile
1233  
1234 B<Example:>
1235  
1236  
1237 my $g = gitHub;
1238 $g->gitFile = my $f = q(z'2 'z"z.data);
1239 my $d = q(𝝰𝝱𝝲);
1240 $g->write($d);
1241
1242 confess "read FAILED" unless $g->read eq $d; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1243  
1244 success "Read passed";
1245
1246  
1247 =head2 write($gitHub, $data, $File)
1248  
1249 Write utf8 data into a L<GitHub|https://github.com/philiprbrenan> file.
1250  
1251 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>.
1252  
1253 Parameter Description
1254 1 $gitHub GitHub object
1255 2 $data Data to be written
1256 3 $File Optionally the name of the file on github
1257  
1258 B<Example:>
1259  
1260  
1261 my $g = gitHub;
1262 $g->gitFile = "zzz.data";
1263
1264 my $d = dateTimeStamp.q( 𝝰𝝱𝝲);
1265
1266 if (1)
1267 {my $t = time();
1268
1269 $g->write($d); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1270  
1271
1272 lll "First write time: ", time() - $t; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1273  
1274 }
1275
1276 my $r = $g->read;
1277 lll "Write bbb: $r";
1278 if (1)
1279 {my $t = time();
1280
1281 $g->write($d); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1282  
1283
1284 lll "Second write time: ", time() - $t; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1285  
1286 }
1287
1288 confess "write FAILED" unless $g->exists; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1289  
1290 success "Write passed";
1291
1292  
1293 =head2 readBlob($gitHub, $sha)
1294  
1295 Read a L<blob|https://en.wikipedia.org/wiki/Binary_large_object> from L<GitHub|https://github.com/philiprbrenan>.
1296  
1297 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>.
1298  
1299 Parameter Description
1300 1 $gitHub GitHub object
1301 2 $sha Data to be written
1302  
1303 B<Example:>
1304  
1305  
1306 my $g = gitHub;
1307 $g->gitFile = "face.jpg";
1308 my $d = readBinaryFile(q(/home/phil/.face));
1309 my $s = $g->writeBlob($d);
1310 my $S = q(4a2df549febb701ba651aae46e041923e9550cb8);
1311 confess q(Write blob FAILED) unless $s eq $S;
1312
1313
1314 my $D = $g->readBlob($s); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1315  
1316 confess q(Write/Read blob FAILED) unless $d eq $D;
1317 success q(Write/Read blob passed);
1318
1319  
1320 =head2 writeBlob($gitHub, $data)
1321  
1322 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.
1323  
1324 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.
1325  
1326 Parameter Description
1327 1 $gitHub GitHub object
1328 2 $data Data to be written
1329  
1330 B<Example:>
1331  
1332  
1333 my $g = gitHub;
1334 $g->gitFile = "face.jpg";
1335 my $d = readBinaryFile(q(/home/phil/.face));
1336
1337 my $s = $g->writeBlob($d); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1338  
1339 my $S = q(4a2df549febb701ba651aae46e041923e9550cb8);
1340 confess q(Write blob FAILED) unless $s eq $S;
1341
1342 my $D = $g->readBlob($s);
1343 confess q(Write/Read blob FAILED) unless $d eq $D;
1344 success q(Write/Read blob passed);
1345
1346  
1347 =head2 copy($gitHub, $target)
1348  
1349 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.
1350  
1351 Required attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>, L<gitFile|/gitFile> = the file to be copied.
1352  
1353 Optional attributes: L<refOrBranch|/refOrBranch>.
1354  
1355 If the write operation is successful, L<failed|/failed> is set to false otherwise it is set to true.
1356  
1357 Returns B<updated> if the write updated the file, B<created> if the write created the file else B<undef> if the write failed.
1358  
1359 Parameter Description
1360 1 $gitHub GitHub object
1361 2 $target The name of the file to be created
1362  
1363 B<Example:>
1364  
1365  
1366 my ($f1, $f2) = ("zzz.data", "zzz2.data");
1367 my $g = gitHub;
1368 $g->gitFile = $f2; $g->delete;
1369 $g->gitFile = $f1;
1370 my $d = dateTimeStamp;
1371 my $w = $g->write($d);
1372
1373 my $r = $g->copy($f2); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1374  
1375 lll "Copy created: $r";
1376 $g->gitFile = $f2;
1377 my $D = $g->read;
1378 lll "Read ccc: $D";
1379
1380 confess "copy FAILED" unless $d eq $D; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1381  
1382 success "Copy passed"
1383
1384  
1385 =head2 exists($gitHub)
1386  
1387 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>.
1388  
1389 Required attributes: L<userid|/userid>, L<repository|/repository>, L<gitFile|/gitFile> file to test.
1390  
1391 Optional attributes: L<refOrBranch|/refOrBranch>, L<patKey|/patKey>.
1392  
1393 Parameter Description
1394 1 $gitHub GitHub object
1395  
1396 B<Example:>
1397  
1398  
1399 my $g = gitHub;
1400 $g->gitFile = "test4.html";
1401 my $d = dateTimeStamp;
1402 $g->write($d);
1403
1404 confess "exists FAILED" unless $g->read eq $d; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1405  
1406 $g->delete;
1407
1408 confess "exists FAILED" if $g->read eq $d; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1409  
1410 success "Exists passed";
1411
1412  
1413 =head2 rename($gitHub, $target)
1414  
1415 Rename a source file on L<GitHub|https://github.com/philiprbrenan> if the target file name is not already in use.
1416  
1417 Required attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>, L<gitFile|/gitFile> = the file to be renamed.
1418  
1419 Optional attributes: L<refOrBranch|/refOrBranch>.
1420  
1421 Returns the new name of the file B<renamed> if the rename was successful else B<undef> if the rename failed.
1422  
1423 Parameter Description
1424 1 $gitHub GitHub object
1425 2 $target The new name of the file
1426  
1427 B<Example:>
1428  
1429  
1430 my ($f1, $f2) = qw(zzz.data zzz2.data);
1431 my $g = gitHub;
1432 $g->gitFile = $f2; $g->delete;
1433
1434 my $d = dateTimeStamp;
1435 $g->gitFile = $f1;
1436 $g->write($d);
1437
1438 confess "rename FAILED" unless $g->read eq $d; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1439  
1440
1441
1442 $g->rename($f2); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1443  
1444
1445 confess "rename FAILED" if $g->exists; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1446  
1447
1448 $g->gitFile = $f2;
1449
1450 confess "rename FAILED" if $g->read eq $d; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1451  
1452 success "Rename passed";
1453
1454  
1455 =head2 delete($gitHub)
1456  
1457 Delete a file from L<GitHub|https://github.com/philiprbrenan>.
1458  
1459 Required attributes: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>, L<gitFile|/gitFile> = the file to be deleted.
1460  
1461 Optional attributes: L<refOrBranch|/refOrBranch>.
1462  
1463 If the delete operation is successful, L<failed|/failed> is set to false otherwise it is set to true.
1464  
1465 Returns true if the delete was successful else false.
1466  
1467 Parameter Description
1468 1 $gitHub GitHub object
1469  
1470 B<Example:>
1471  
1472  
1473 my $g = gitHub;
1474 my $d = dateTimeStamp;
1475 $g->gitFile = "zzz.data";
1476 $g->write($d);
1477
1478
1479 confess "delete FAILED" unless $g->read eq $d; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1480  
1481
1482 if (1)
1483 {my $t = time();
1484
1485 my $d = $g->delete; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1486  
1487 lll "Delete 1: ", $d;
1488
1489 lll "First delete: ", time() - $t; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1490  
1491
1492 confess "delete FAILED" if $g->exists; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1493  
1494 }
1495
1496 if (1)
1497 {my $t = time();
1498
1499 my $d = $g->delete; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1500  
1501 lll "Delete 1: ", $d;
1502
1503 lll "Second delete: ", time() - $t; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1504  
1505
1506 confess "delete FAILED" if $g->exists; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1507  
1508 }
1509 success "Delete passed";
1510
1511  
1512 =head1 Repositories
1513  
1514 Perform actions on L<GitHub|https://github.com/philiprbrenan> repositories.
1515  
1516 =head2 getRepository($gitHub)
1517  
1518 Get the overall details of a repository
1519  
1520 Parameter Description
1521 1 $gitHub GitHub object
1522  
1523 B<Example:>
1524  
1525  
1526
1527 my $r = gitHub(repository => q(C))->getRepository; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1528  
1529 success "Get repository succeeded";
1530
1531  
1532 =head2 listCommits($gitHub)
1533  
1534 List all the commits in a L<GitHub|https://github.com/philiprbrenan> repository.
1535  
1536 Required attributes: L<userid|/userid>, L<repository|/repository>.
1537  
1538 Parameter Description
1539 1 $gitHub GitHub object
1540  
1541 B<Example:>
1542  
1543  
1544
1545 my $c = gitHub->listCommits; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1546  
1547 my %s = listCommitShas $c;
1548 lll "Commits
1549 ", dump $c;
1550 lll "Commit shas
1551 ", dump \%s;
1552 success "ListCommits passed";
1553
1554  
1555 =head2 listCommitShas($commits)
1556  
1557 Create {commit name => sha} from the results of L<listCommits>.
1558  
1559 Parameter Description
1560 1 $commits Commits from L<listCommits>
1561  
1562 B<Example:>
1563  
1564  
1565 my $c = gitHub->listCommits;
1566
1567 my %s = listCommitShas $c; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1568  
1569 lll "Commits
1570 ", dump $c;
1571 lll "Commit shas
1572 ", dump \%s;
1573 success "ListCommits passed";
1574
1575  
1576 =head2 writeCommit($gitHub, $folder, @files)
1577  
1578 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.
1579  
1580 Required attributes: L<userid|/userid>, L<repository|/repository>, L<refOrBranch|/refOrBranch>.
1581  
1582 Parameter Description
1583 1 $gitHub GitHub object
1584 2 $folder File prefix to remove
1585 3 @files Files to write
1586  
1587 B<Example:>
1588  
1589  
1590 my $f = temporaryFolder; # Folder in which we will create some files to upload in the commit
1591 my $c = dateTimeStamp; # Create some content
1592 my $if = q(/home/phil/.face); # Image file
1593
1594 writeFile(fpe($f, q(data), $_, qw(txt)), $c) for 1..3; # Place content in files in a sub folder
1595 copyBinaryFile $if, my $If = fpe $f, qw(face jpg); # Add an image
1596
1597 my $g = GitHub::Crud::new # Create GitHub
1598 (userid => q(philiprbrenan),
1599 repository => q(aaa),
1600 branch => q(test),
1601 confessOnFailure => 1);
1602
1603 $g->loadPersonalAccessToken; # Load a personal access token
1604
1605 $g->writeCommit($f); # Upload commit - confess to any errors # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1606  
1607
1608 my $C = $g->read(q(data/1.txt)); # Read data written in commit
1609 my $I = $g->read(q(face.jpg));
1610 my $i = readBinaryFile $if;
1611
1612 confess "Date stamp failed" unless $C eq $c; # Check text
1613 confess "Image failed" unless $i eq $I; # Check image
1614 success "Write commit succeeded";
1615
1616  
1617 =head2 listWebHooks($gitHub)
1618  
1619 List web hooks associated with your L<GitHub|https://github.com/philiprbrenan> repository.
1620  
1621 Required: L<userid|/userid>, L<repository|/repository>, L<patKey|/patKey>.
1622  
1623 If the list operation is successful, L<failed|/failed> is set to false otherwise it is set to true.
1624  
1625 Returns true if the list operation was successful else false.
1626  
1627 Parameter Description
1628 1 $gitHub GitHub object
1629  
1630 B<Example:>
1631  
1632  
1633
1634 success join ' ', q(Webhooks:), dump(gitHub->listWebHooks); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1635  
1636
1637  
1638 =head2 createPushWebHook($gitHub)
1639  
1640 Create a web hook for your L<GitHub|https://github.com/philiprbrenan> userid.
1641  
1642 Required: L<userid|/userid>, L<repository|/repository>, L<url|/url>, L<patKey|/patKey>.
1643  
1644 Optional: L<secret|/secret>.
1645  
1646 If the create operation is successful, L<failed|/failed> is set to false otherwise it is set to true.
1647  
1648 Returns true if the web hook was created successfully else false.
1649  
1650 Parameter Description
1651 1 $gitHub GitHub object
1652  
1653 B<Example:>
1654  
1655  
1656 my $g = gitHub;
1657
1658 my $d = $g->createPushWebHook; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1659  
1660 success join ' ', "Create web hook:", dump($d);
1661
1662  
1663 =head2 listRepositories($gitHub)
1664  
1665 List the repositories accessible to a user on L<GitHub|https://github.com/philiprbrenan>.
1666  
1667 Required: L<userid|/userid>.
1668  
1669 Returns details of the repositories.
1670  
1671 Parameter Description
1672 1 $gitHub GitHub object
1673  
1674 B<Example:>
1675  
1676  
1677
1678 success "List repositories: ", dump(gitHub()->listRepositories); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1679  
1680
1681  
1682 =head2 createRepository($gitHub)
1683  
1684 Create a repository on L<GitHub|https://github.com/philiprbrenan>.
1685  
1686 Required: L<userid|/userid>, L<repository|/repository>.
1687  
1688 Returns true if the issue was created successfully else false.
1689  
1690 Parameter Description
1691 1 $gitHub GitHub object
1692  
1693 B<Example:>
1694  
1695  
1696
1697 gitHub(repository => q(ccc))->createRepository; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1698  
1699 success "Create repository succeeded";
1700
1701  
1702 =head2 createRepositoryFromSavedToken($userid, $repository, $private, $accessFolderOrToken)
1703  
1704 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>.
1705  
1706 Returns true if the issue was created successfully else false.
1707  
1708 Parameter Description
1709 1 $userid Userid on GitHub
1710 2 $repository The repository name
1711 3 $private True if the repo is private
1712 4 $accessFolderOrToken Location of access token.
1713  
1714 B<Example:>
1715  
1716  
1717
1718 createRepositoryFromSavedToken(q(philiprbrenan), q(ddd)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1719  
1720 success "Create repository succeeded";
1721
1722  
1723 =head2 createIssue($gitHub)
1724  
1725 Create an issue on L<GitHub|https://github.com/philiprbrenan>.
1726  
1727 Required: L<userid|/userid>, L<repository|/repository>, L<body|/body>, L<title|/title>.
1728  
1729 If the operation is successful, L<failed|/failed> is set to false otherwise it is set to true.
1730  
1731 Returns true if the issue was created successfully else false.
1732  
1733 Parameter Description
1734 1 $gitHub GitHub object
1735  
1736 B<Example:>
1737  
1738  
1739
1740 gitHub(title=>q(Hello), body=>q(World))->createIssue; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1741  
1742 success "Create issue succeeded";
1743
1744  
1745 =head2 createIssueFromSavedToken($userid, $repository, $title, $body, $accessFolderOrToken)
1746  
1747 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>.
1748  
1749 Returns true if the issue was created successfully else false.
1750  
1751 Parameter Description
1752 1 $userid Userid on GitHub
1753 2 $repository Repository name
1754 3 $title Issue title
1755 4 $body Issue body
1756 5 $accessFolderOrToken Location of access token.
1757  
1758 B<Example:>
1759  
1760  
1761
1762 &createIssueFromSavedToken(qw(philiprbrenan ddd hello World)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1763  
1764 success "Create issue succeeded";
1765
1766  
1767 =head2 currentRepo()
1768  
1769 Create a github object for the current repo if we are on github actions
1770  
1771  
1772 =head2 createIssueInCurrentRepo($title, $body)
1773  
1774 Create an issue in the current GitHub repo if we are running on GitHub
1775  
1776 Parameter Description
1777 1 $title Title of issue
1778 2 $body Body of issue
1779  
1780 =head2 writeFileUsingSavedToken($userid, $repository, $file, $content, $accessFolderOrToken)
1781  
1782 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.
1783  
1784 Parameter Description
1785 1 $userid Userid on GitHub
1786 2 $repository Repository name
1787 3 $file File name on github
1788 4 $content File content
1789 5 $accessFolderOrToken Location of access token.
1790  
1791 B<Example:>
1792  
1793  
1794 my $s = q(HelloWorld);
1795
1796 &writeFileUsingSavedToken(qw(philiprbrenan ddd hello.txt), $s); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1797  
1798 my $S = gitHub(repository=>q(ddd), gitFile=>q(hello.txt))->read;
1799
1800 confess "Write file using saved token FAILED" unless $s eq $S;
1801 success "Write file using saved token succeeded";
1802
1803  
1804 =head2 writeFileFromFileUsingSavedToken($userid, $repository, $file, $localFile, $accessFolderOrToken)
1805  
1806 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.
1807  
1808 Parameter Description
1809 1 $userid Userid on GitHub
1810 2 $repository Repository name
1811 3 $file File name on github
1812 4 $localFile File content
1813 5 $accessFolderOrToken Location of access token.
1814  
1815 B<Example:>
1816  
1817  
1818 my $f = writeFile(undef, my $s = "World
1819 ");
1820
1821 &writeFileFromFileUsingSavedToken(qw(philiprbrenan ddd hello.txt), $f); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1822  
1823 my $S = gitHub(repository=>q(ddd), gitFile=>q(hello.txt))->read;
1824 confess "Write file from file using saved token FAILED" unless $s eq $S;
1825 success "Write file from file using saved token succeeded"
1826
1827  
1828 =head2 writeFileFromCurrentRun($target, $text)
1829  
1830 Write to a file into the repository from the current run
1831  
1832 Parameter Description
1833 1 $target The target file name in the repo
1834 2 $text The text to write into this file
1835  
1836 =head2 writeFileFromFileFromCurrentRun($target)
1837  
1838 Write a file into the repository from the current run
1839  
1840 Parameter Description
1841 1 $target File name both locally and in the repo
1842  
1843 =head2 writeBinaryFileFromFileInCurrentRun($target, $source)
1844  
1845 Upload a binary file from the current run into the repo.
1846  
1847 Parameter Description
1848 1 $target The target file name in the repo
1849 2 $source The current file name in the run
1850  
1851 =head2 readFileUsingSavedToken($userid, $repository, $file, $accessFolderOrToken)
1852  
1853 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.
1854  
1855 Parameter Description
1856 1 $userid Userid on GitHub
1857 2 $repository Repository name
1858 3 $file File name on github
1859 4 $accessFolderOrToken Location of access token.
1860  
1861 B<Example:>
1862  
1863  
1864 my $s = q(Hello to the World);
1865 &writeFileUsingSavedToken(qw(philiprbrenan ddd hello.txt), $s);
1866
1867 my $S = &readFileUsingSavedToken (qw(philiprbrenan ddd hello.txt)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1868  
1869
1870 confess "Read file using saved token FAILED" unless $s eq $S;
1871 success "Read file using saved token succeeded"
1872
1873  
1874 =head2 writeFolderUsingSavedToken($userid, $repository, $targetFolder, $localFolder, $accessFolderOrToken)
1875  
1876 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.
1877  
1878 Parameter Description
1879 1 $userid Userid on GitHub
1880 2 $repository Repository name
1881 3 $targetFolder Target folder on github
1882 4 $localFolder Local folder name
1883 5 $accessFolderOrToken Location of access token.
1884  
1885 =head2 writeCommitUsingSavedToken($userid, $repository, $source, $accessFolderOrToken)
1886  
1887 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.
1888  
1889 Parameter Description
1890 1 $userid Userid on GitHub
1891 2 $repository Repository name
1892 3 $source Local folder on github
1893 4 $accessFolderOrToken Optionally: location of access token.
1894  
1895 =head2 deleteFileUsingSavedToken($userid, $repository, $target, $accessFolderOrToken)
1896  
1897 Delete a file on GitHub using a saved token
1898  
1899 Parameter Description
1900 1 $userid Userid on GitHub
1901 2 $repository Repository name
1902 3 $target File on Github
1903 4 $accessFolderOrToken Optional: the folder containing saved access tokens
1904  
1905 =head2 getRepositoryUsingSavedToken($userid, $repository, $accessFolderOrToken)
1906  
1907 Get repository details using a saved token
1908  
1909 Parameter Description
1910 1 $userid Userid on GitHub
1911 2 $repository Repository name
1912 3 $accessFolderOrToken Optionally: location of access token.
1913  
1914 B<Example:>
1915  
1916  
1917
1918 my $r = getRepositoryUsingSavedToken(q(philiprbrenan), q(aaa)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1919  
1920 success "Get repository using saved access token succeeded";
1921
1922  
1923 =head2 getRepositoryUpdatedAtUsingSavedToken($userid, $repository, $accessFolderOrToken)
1924  
1925 Get repository 'updated_at' using a saved token and return the time in number of seconds since the Unix epoch.
1926  
1927 Parameter Description
1928 1 $userid Userid on GitHub
1929 2 $repository Repository name
1930 3 $accessFolderOrToken Optionally: location of access token.
1931  
1932 B<Example:>
1933  
1934  
1935
1936 my $u = getRepositoryUpdatedAtUsingSavedToken(q(philiprbrenan), q(aaa)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1937  
1938 success "Get repository updated_at field succeeded";
1939
1940  
1941 =head1 Access tokens
1942  
1943 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.
1944  
1945 =head2 savePersonalAccessToken($gitHub)
1946  
1947 Save a L<GitHub|https://github.com/philiprbrenan> personal access token by userid in folder L<personalAccessTokenFolder|/personalAccessTokenFolder>.
1948  
1949 Parameter Description
1950 1 $gitHub GitHub object
1951  
1952 B<Example:>
1953  
1954  
1955 my $d = temporaryFolder;
1956 my $t = join '', 1..20;
1957
1958 my $g = gitHub
1959 (userid => q(philiprbrenan),
1960 personalAccessToken => $t,
1961 personalAccessTokenFolder => $d,
1962 );
1963
1964
1965 $g->savePersonalAccessToken; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1966  
1967 my $T = $g->loadPersonalAccessToken;
1968
1969 confess "Load/Save token FAILED" unless $t eq $T;
1970 success "Load/Save token succeeded"
1971
1972  
1973 =head2 loadPersonalAccessToken($gitHub)
1974  
1975 Load a personal access token by userid from folder L<personalAccessTokenFolder|/personalAccessTokenFolder>.
1976  
1977 Parameter Description
1978 1 $gitHub GitHub object
1979  
1980 B<Example:>
1981  
1982  
1983 my $d = temporaryFolder;
1984 my $t = join '', 1..20;
1985
1986 my $g = gitHub
1987 (userid => q(philiprbrenan),
1988 personalAccessToken => $t,
1989 personalAccessTokenFolder => $d,
1990 );
1991
1992 $g->savePersonalAccessToken;
1993
1994 my $T = $g->loadPersonalAccessToken; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1995  
1996
1997 confess "Load/Save token FAILED" unless $t eq $T;
1998 success "Load/Save token succeeded"
1999
2000  
2001  
2002 =head2 GitHub::Crud Definition
2003  
2004  
2005 Attributes describing the interface with L<GitHub|https://github.com/philiprbrenan>.
2006  
2007  
2008  
2009  
2010 =head3 Input fields
2011  
2012  
2013 =head4 body
2014  
2015 The body of an issue.
2016  
2017 =head4 branch
2018  
2019 Branch name (you should create this branch first) or omit it for the default branch which is usually 'master'.
2020  
2021 =head4 confessOnFailure
2022  
2023 Confess to any failures
2024  
2025 =head4 gitFile
2026  
2027 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.
2028  
2029 =head4 gitFolder
2030  
2031 Folder name on L<GitHub|https://github.com/philiprbrenan> - this name can contain '/'.
2032  
2033 =head4 message
2034  
2035 Optional commit message
2036  
2037 =head4 nonRecursive
2038  
2039 Fetch only one level of files with L<list|https://en.wikipedia.org/wiki/Linked_list>.
2040  
2041 =head4 personalAccessToken
2042  
2043 A personal access token with scope "public_repo" as generated on page: https://github.com/settings/tokens.
2044  
2045 =head4 personalAccessTokenFolder
2046  
2047 The folder into which to save personal access tokens. Set to q(/etc/GitHubCrudPersonalAccessToken) by default.
2048  
2049 =head4 private
2050  
2051 Whether the repository being created should be private or not.
2052  
2053 =head4 repository
2054  
2055 The name of the repository to be worked on minus the userid - you should create this repository first manually.
2056  
2057 =head4 secret
2058  
2059 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>,
2060  
2061 =head4 title
2062  
2063 The title of an issue.
2064  
2065 =head4 userid
2066  
2067 Userid on L<GitHub|https://github.com/philiprbrenan> of the repository to be worked on.
2068  
2069 =head4 webHookUrl
2070  
2071 The url for a web hook.
2072  
2073  
2074  
2075 =head3 Output fields
2076  
2077  
2078 =head4 failed
2079  
2080 Defined if the last request to L<GitHub|https://github.com/philiprbrenan> failed else B<undef>.
2081  
2082 =head4 fileList
2083  
2084 Reference to an array of files produced by L<list|/list>.
2085  
2086 =head4 readData
2087  
2088 Data produced by L<read|/read>.
2089  
2090 =head4 response
2091  
2092 A reference to L<GitHub|https://github.com/philiprbrenan>'s response to the latest request.
2093  
2094  
2095  
2096 =head2 GitHub::Crud::Response Definition
2097  
2098  
2099 Attributes describing a response from L<GitHub|https://github.com/philiprbrenan>.
2100  
2101  
2102  
2103  
2104 =head3 Output fields
2105  
2106  
2107 =head4 content
2108  
2109 The actual content of the file from L<GitHub|https://github.com/philiprbrenan>.
2110  
2111 =head4 data
2112  
2113 The data received from L<GitHub|https://github.com/philiprbrenan>, normally in L<Json|https://en.wikipedia.org/wiki/JSON> format.
2114  
2115 =head4 status
2116  
2117 Our version of Status.
2118  
2119  
2120  
2121 =head1 Index
2122  
2123  
2124 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.
2125  
2126 2 L<createIssue|/createIssue> - Create an issue on L<GitHub|https://github.com/philiprbrenan>.
2127  
2128 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>.
2129  
2130 4 L<createIssueInCurrentRepo|/createIssueInCurrentRepo> - Create an issue in the current GitHub repo if we are running on GitHub
2131  
2132 5 L<createPushWebHook|/createPushWebHook> - Create a web hook for your L<GitHub|https://github.com/philiprbrenan> userid.
2133  
2134 6 L<createRepository|/createRepository> - Create a repository on L<GitHub|https://github.com/philiprbrenan>.
2135  
2136 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>.
2137  
2138 8 L<currentRepo|/currentRepo> - Create a github object for the current repo if we are on github actions
2139  
2140 9 L<delete|/delete> - Delete a file from L<GitHub|https://github.com/philiprbrenan>.
2141  
2142 10 L<deleteFileUsingSavedToken|/deleteFileUsingSavedToken> - Delete a file on GitHub using a saved token
2143  
2144 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>.
2145  
2146 12 L<getRepository|/getRepository> - Get the overall details of a repository
2147  
2148 13 L<getRepositoryUpdatedAtUsingSavedToken|/getRepositoryUpdatedAtUsingSavedToken> - Get repository 'updated_at' using a saved token and return the time in number of seconds since the Unix epoch.
2149  
2150 14 L<getRepositoryUsingSavedToken|/getRepositoryUsingSavedToken> - Get repository details using a saved token
2151  
2152 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.
2153  
2154 16 L<listCommits|/listCommits> - List all the commits in a L<GitHub|https://github.com/philiprbrenan> repository.
2155  
2156 17 L<listCommitShas|/listCommitShas> - Create {commit name => sha} from the results of L<listCommits>.
2157  
2158 18 L<listRepositories|/listRepositories> - List the repositories accessible to a user on L<GitHub|https://github.com/philiprbrenan>.
2159  
2160 19 L<listWebHooks|/listWebHooks> - List web hooks associated with your L<GitHub|https://github.com/philiprbrenan> repository.
2161  
2162 20 L<loadPersonalAccessToken|/loadPersonalAccessToken> - Load a personal access token by userid from folder L<personalAccessTokenFolder|/personalAccessTokenFolder>.
2163  
2164 21 L<new|/new> - Create a new L<GitHub|https://github.com/philiprbrenan> object with attributes as described at: L<GitHub::Crud Definition>.
2165  
2166 22 L<read|/read> - Read data from a file on L<GitHub|https://github.com/philiprbrenan>.
2167  
2168 23 L<readBlob|/readBlob> - Read a L<blob|https://en.wikipedia.org/wiki/Binary_large_object> from L<GitHub|https://github.com/philiprbrenan>.
2169  
2170 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.
2171  
2172 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.
2173  
2174 26 L<savePersonalAccessToken|/savePersonalAccessToken> - Save a L<GitHub|https://github.com/philiprbrenan> personal access token by userid in folder L<personalAccessTokenFolder|/personalAccessTokenFolder>.
2175  
2176 27 L<specialFileData|/specialFileData> - Do not encode or decode data with a known file signature
2177  
2178 28 L<write|/write> - Write utf8 data into a L<GitHub|https://github.com/philiprbrenan> file.
2179  
2180 29 L<writeBinaryFileFromFileInCurrentRun|/writeBinaryFileFromFileInCurrentRun> - Upload a binary file from the current run into the repo.
2181  
2182 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.
2183  
2184 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.
2185  
2186 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.
2187  
2188 33 L<writeFileFromCurrentRun|/writeFileFromCurrentRun> - Write to a file into the repository from the current run
2189  
2190 34 L<writeFileFromFileFromCurrentRun|/writeFileFromFileFromCurrentRun> - Write a file into the repository from the current run
2191  
2192 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.
2193  
2194 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.
2195  
2196 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.
2197  
2198 =head1 Installation
2199  
2200 This module is written in 100% Pure Perl and, thus, it is easy to read,
2201 comprehend, use, modify and install via B<cpan>:
2202  
2203 sudo cpan install GitHub::Crud
2204  
2205 =head1 Author
2206  
2207 L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com>
2208  
2209 L<http://www.appaapps.com|http://www.appaapps.com>
2210  
2211 =head1 Copyright
2212  
2213 Copyright (c) 2016-2021 Philip R Brenan.
2214  
2215 This module is free software. It may be used, redistributed and/or modified
2216 under the same terms as Perl itself.
2217  
2218 =cut
2219  
2220  
2221  
2222 # Tests and documentation
2223  
2224 sub test
2225 1 ▶ {my $p = __PACKAGE__;
Subroutine test: called CC=5 SCAR=17.1
Pod undocumented
2226 1 binmode($_, ":utf8") for *STDOUT, *STDERR;
2227 1 ▶ return if eval "eof(${p}::DATA)";
Branch: 1 branch
BranchTrueFalseTotal
if eval "eof(${p}::DATA)" 0 1 1
2228 1 my $s = eval "join('', <${p}::DATA>)";
2229 1 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
2230 1 ▶ eval $s;
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine success: not called CC=1 SCAR=0.1
Pod undocumented
2231 1 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 1 0 1
2232 0 1
2233 }
2234  
2235 1 ▶test unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
2236  
2237 1 1;
2238 #podDocumentation
2239 __DATA__
↑ Summary Build »