App-GitHubUtils

 view release on metacpan or  search on metacpan

lib/App/GitHubUtils.pm  view on Meta::CPAN


    my @tried_names;

    my ($chosen_user, $chosen_repo);
  SEARCH:
    for my $user (@users) {
        for my $repo (@repos) {
            push @tried_names, "$user/$repo.git";
            log_info "Trying $user/$repo.git ...";
            my $res = Perinci::CmdLine::Call::call_cli_script(
                script => 'github-cmd',
                argv   => ['repo-exists', '--repo', $repo, '--user', $user],
            );
            return [500, "Can't check if repo $repo exists: ".
                        "$res->[0] - $res->[1]"] unless $res->[0] == 200;
            if ($res->[2]) {
                $chosen_user = $user;
                $chosen_repo = $repo;
                last SEARCH;
            }
        }
    }

    return [412, "Can't find any existing repo (tried ".
                join(", ", @tried_names).")"]
        unless defined $chosen_user;

    system(
        "git", "clone", "git\@github.com:$chosen_user/$chosen_repo.git",
        (defined $args{directory} ? ($args{directory}) : ()),
    );

    if ($?) {
        [500, "git clone failed with exit code ".($? < 0 ? $? : $? >> 8)];
    } else {
        [200];
    }
}

$SPEC{this_repo_on_github} = {
    v => 1.1,
    args => {},
    deps => {
        prog => 'this-repo',
    },
    links => [
        {url=>'prog:this-repo'},
    ],
};
sub this_repo_on_github {
    require IPC::System::Options;
    require Browser::Open;

    my $repo = `this-repo`;
    return [412, "this-repo failed"] unless length $repo;
    chomp($repo);
    my $stdout;
    IPC::System::Options::system(
        {die=>1, log=>1, capture_stdout=>\$stdout},
        "git", "config", "-l");
    say $stdout;
    $stdout =~ m!.*=.*github\.com:?/?([^/]+)/(.+?)(?:\.git)?$!im
        or return [412, "Can't find github username and repository name from configuration"];
    Browser::Open::open_browser("https://github.com/$1/$2");
    [200];
}

1;
# ABSTRACT: Utilities related to GitHub

__END__

=pod

=encoding UTF-8

=head1 NAME

App::GitHubUtils - Utilities related to GitHub

=head1 VERSION

This document describes version 0.009 of App::GitHubUtils (from Perl distribution App-GitHubUtils), released on 2021-08-14.

=head1 DESCRIPTION

This distribution provides the following command-line utilities related to
GitHub:

=over

=item * L<create-this-repo-on-github>

=item * L<git-clone-from-github>

=item * L<this-repo-on-github>

=back

=head1 FUNCTIONS


=head2 create_this_repo_on_github

Usage:

 create_this_repo_on_github(%args) -> [$status_code, $reason, $payload, \%result_meta]

Create this repo on github.

This is a convenient no-argument-needed command to create GitHub repository of
the current ("this") repo. Will use L<github-cmd> from L<App::github::cmd>
to create the repository. To find out the repo name to be created, will first
check .git/config if it exists. Otherwise, will just use the name of the current
directory.

This function is not exported.

Arguments ('*' denotes required arguments):

=over 4



( run in 1.351 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )