blib/lib/App/GitHubUtils.pm

filter: Statementstmt 22.3% 21 / 94 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 57.1% 4 / 7 Podpod 100% 3 / 3 totaltotal 18.7% 28 / 149 CC 25 SCAR 52.0
CC
25
Cov
16%
git_clone_from_github
306.0
create_this_repo_on_github
56.0
this_repo_on_github
12.0
CRAP 181.7
↑ Summary
1 package App::GitHubUtils;
2  
3 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 1 our $DATE = '2021-08-14'; # DATE
5 1 our $DIST = 'App-GitHubUtils'; # DIST
6 1 our $VERSION = '0.009'; # VERSION
7  
8 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our %SPEC;
14  
15 1 $SPEC{':package'} = {
16 v => 1.1,
17 summary => 'Utilities related to GitHub',
18 };
19  
20 $SPEC{create_this_repo_on_github} = {
21 1 v => 1.1,
22 summary => 'Create this repo on github',
23 description => <<'_',
24  
25 This is a convenient no-argument-needed command to create GitHub repository of
26 the current ("this") repo. Will use <prog:github-cmd> from <pm:App::github::cmd>
27 to create the repository. To find out the repo name to be created, will first
28 check .git/config if it exists. Otherwise, will just use the name of the current
29 directory.
30  
31 _
32 args => {
33 github_cmd_config_profile=>{
34 schema => 'str*',
35 },
36 },
37 deps => {
38 prog => 'github-cmd',
39 },
40 };
41 0 ▶sub create_this_repo_on_github {
Subroutine create_this_repo_on_github: not called CC=7 SCAR=40.3
Pod documented
42 0 require App::GitUtils;
43 0 require Cwd;
44 0 require IPC::System::Options;
45  
46 0 my %args = @_;
47  
48 0 my $repo;
49 SET_REPO_NAME:
50 {
51 0 my $res = App::GitUtils::info();
52 0 ▶ if ($res->[0] == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] == 200) 0 0 0
53 0 my $content = do {
54 0 local $/;
55 0 my $path = "$res->[2]{git_dir}/config";
56 0 ▶ open my $fh, "<", $path or die "Can't open $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "Can't open ${path}: $!" 0 0 0
57 0 <$fh>;
58 };
59 0 ▶ if ($content =~ m!^\s*url\s*=\s*.+/([^/]+)\.git\s*$!m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[^\s*url\s*=\s*.+/([^/]+)\.git\s*$]m) 0 0 0
60 0 $repo = $1;
61 0 last;
62 }
63 }
64 0 $repo = Cwd::getcwd();
65 0 $repo =~ s!.+/!!;
66 }
67 0 log_info "Creating repo '%s' ...", $repo;
68  
69 0 my ($out, $err);
70 IPC::System::Options::system(
71 {log=>1, capture_stdout=>\$out, capture_stderr=>\$err},
72 "github-cmd",
73 0 ▶ defined($args{github_cmd_config_profile}) ? ("--config-profile", $args{github_cmd_config_profile}) : (),
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'github_cmd_config_profile'} ? : 0 0 0
74 "create-repo", $repo);
75 0 my $exit = $?;
76  
77 0 ▶ if ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) { } 0 0 0
78 0 ▶ if ($out =~ /name already exists/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out =~ /name already exists/) { } 0 0 0
79 0 return [412, "Failed: Repo already exists"];
80 } else {
81 0 return [500, "Failed: $out"];
82 }
83 } else {
84 0 return [200, "OK", undef, {'func.repo'=>$repo}];
85 }
86 }
87  
88 $SPEC{git_clone_from_github} = {
89 1 v => 1.1,
90 summary => 'git clone, with some conveniences',
91 description => <<'_',
92  
93 Instead of having to type:
94  
95 % git clone git@github.com:USER/PREFIX-NAME.git
96  
97 you can just type:
98  
99 % git-clone-from-github NAME
100  
101 The utility will try the `users` specified in config file, as well as
102 `prefixes` and clone the first repo that exists. You can put something like this
103 in `githubutils.conf`:
104  
105 [prog=git-clone-from-github]
106 users = ["perlancar", "perlancar2"]
107 prefixes = ["perl5-", "perl-"]
108 suffixes = ["-p5"]
109  
110 The utility will check whether repo in these URLs exist:
111  
112 git@github.com:perlancar/perl5-NAME.git
113 git@github.com:perlancar/perl-NAME.git
114 git@github.com:perlancar/NAME-p5.git
115 git@github.com:perlancar2/perl5-NAME.git
116 git@github.com:perlancar2/perl-NAME.git
117 git@github.com:perlancar2/NAME-p5.git
118  
119 _
120 args => {
121 name => {
122 schema => 'str*',
123 req => 1,
124 pos => 0,
125 },
126 users => {
127 schema => ['array*', of=>'str*'],
128 description => <<'_',
129  
130 If not specified, will use `login` from `github-cmd.conf` file.
131  
132 _
133 },
134 prefixes => {
135 schema => ['array*', of=>'str*'],
136 },
137 suffixes => {
138 schema => ['array*', of=>'str*'],
139 },
140 },
141 deps => {
142 all => [
143 {prog => 'github-cmd'},
144 {prog => 'git'},
145 ],
146 },
147 };
148 0 ▶sub git_clone_from_github {
Subroutine git_clone_from_github: not called CC=17 SCAR=57.2
Pod documented
149 0 require Perinci::CmdLine::Call;
150 0 require Perinci::CmdLine::Util::Config;
151  
152 0 my %args = @_;
153  
154 0 my @users;
155 0 ▶ if ($args{users} && @{ $args{users} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'users'} and @{$args{'users'};}) { } 0 0 0
Condition0%0/3
$args{"users"} and @{$args{"users"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"users"} and @{$args{"users"};}
$args{"users"} @{$args{"users"};}
Truth table0%0/3
A and B
A= $args{"users"}B= @{$args{"users"};}
ABresult
0X0
100
111
156 0 push @users, @{ $args{users} };
157 } else {
158 # get login from github-cmd.conf. XXX later we'll use
159 # PERINCI_CMDLINE_DUMP_CONFIG/PERINCI_CMDLINE_DUMP_ARGS
160 0 my $res = Perinci::CmdLine::Util::Config::read_config(
161 config_filename => 'github-cmd.conf',
162 );
163 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
164 return [412, "Cannot read 'login' from github-cmd.conf to use as users"]
165 0 ▶ unless defined $res->[2]{GLOBAL}{login};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res->[2]{"GLOBAL"}{"login"} 0 0 0
166 0 push @users, $res->[2]{GLOBAL}{login};
167 }
168  
169 0 my @repos;
170 0 push @repos, $args{name};
171 0 ▶ push @repos, "$_$args{name}" for @{ $args{prefixes} // [] };
Condition0%0/2
$args{"prefixes"} // []
l!l
00
MC/DC0%0/1
$args{"prefixes"} // []
$args{"prefixes"}
Truth table0%0/2
A
A= $args{"prefixes"}
Aresult
11
00
172 0 ▶ push @repos, "$args{name}$_" for @{ $args{suffixes} // [] };
Condition0%0/2
$args{"suffixes"} // []
l!l
00
MC/DC0%0/1
$args{"suffixes"} // []
$args{"suffixes"}
Truth table0%0/2
A
A= $args{"suffixes"}
Aresult
11
00
173  
174 0 my @tried_names;
175  
176 0 my ($chosen_user, $chosen_repo);
177 SEARCH:
178 0 for my $user (@users) {
179 0 for my $repo (@repos) {
180 0 push @tried_names, "$user/$repo.git";
181 0 log_info "Trying $user/$repo.git ...";
182 0 my $res = Perinci::CmdLine::Call::call_cli_script(
183 script => 'github-cmd',
184 argv => ['repo-exists', '--repo', $repo, '--user', $user],
185 );
186 0 ▶ return [500, "Can't check if repo $repo exists: ".
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
187 "$res->[0] - $res->[1]"] unless $res->[0] == 200;
188 0 ▶ if ($res->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[2]) 0 0 0
189 0 $chosen_user = $user;
190 0 $chosen_repo = $repo;
191 0 last SEARCH;
192 }
193 }
194 }
195  
196 0 ▶ return [412, "Can't find any existing repo (tried ".
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chosen_user 0 0 0
197 join(", ", @tried_names).")"]
198 unless defined $chosen_user;
199  
200 system(
201 "git", "clone", "git\@github.com:$chosen_user/$chosen_repo.git",
202 0 ▶ (defined $args{directory} ? ($args{directory}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'directory'} ? : 0 0 0
203 );
204  
205 0 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) { } 0 0 0
206 0 ▶ [500, "git clone failed with exit code ".($? < 0 ? $? : $? >> 8)];
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
207 } else {
208 0 [200];
209 }
210 }
211  
212 $SPEC{this_repo_on_github} = {
213 1 v => 1.1,
214 args => {},
215 deps => {
216 prog => 'this-repo',
217 },
218 links => [
219 {url=>'prog:this-repo'},
220 ],
221 };
222 0 ▶sub this_repo_on_github {
Subroutine this_repo_on_github: not called CC=3 SCAR=24.8
Pod documented
223 0 require IPC::System::Options;
224 0 require Browser::Open;
225  
226 0 my $repo = `this-repo`;
227 0 ▶ return [412, "this-repo failed"] unless length $repo;
Branch: 1 branch
BranchTrueFalseTotal
unless length $repo 0 0 0
228 0 chomp($repo);
229 0 my $stdout;
230 0 IPC::System::Options::system(
231 {die=>1, log=>1, capture_stdout=>\$stdout},
232 "git", "config", "-l");
233 0 say $stdout;
234 0 ▶ $stdout =~ m!.*=.*github\.com:?/?([^/]+)/(.+?)(?:\.git)?$!im
Branch: 1 branch
BranchTrueFalseTotal
$stdout =~ m[.*=.*github\.com:?/?([^/]+)/(.+?)(?:\.git)?$]im or return [412, "Can't find github username and repository name from configuration"] 0 0 0
235 or return [412, "Can't find github username and repository name from configuration"];
236 0 Browser::Open::open_browser("https://github.com/$1/$2");
237 0 [200];
238 }
239  
240 1 1;
241 # ABSTRACT: Utilities related to GitHub
242  
243 __END__
↑ Summary