git-clone-entity 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #!/usr/bin/env perl
  2. package Git::CloneEntity;
  3. use strict;
  4. use warnings;
  5. use FindBin::libs;
  6. use HTTP::Tiny;
  7. use Config::Simple;
  8. use Getopt::Long qw{GetOptionsFromArray};
  9. use Pod::Usage;
  10. use Pithub;
  11. use Gogs;
  12. use Git;
  13. use Term::ReadKey();
  14. use IO::Interactive::Tiny();
  15. =head1 DESCRIPTION
  16. It is a common pattern in organizations to have their own git resources, but mirror everything public on one of the big platforms with network effect.
  17. It is also a common pattern to need to clone basically everything for a given user/org when new development environments are instantiated.
  18. Alternatively, you may just want to keep your local development environment up to date for said users/projects.
  19. This program facilitiates cloning your repositories for given users/orgs from either a local gogs/github instance and configuring pushurls for both it and github, or any other github-api compatible mirror(s).
  20. By default it will configure your origin remote to fetch from the baseurl provided, and push to it and the mirror(s) provided, but you can specify whatever remotename ('all' seems popular) you desire.
  21. Regardless, remotes for the base and mirrors will also be set up in case individual pushes must be made.
  22. In the event that two different users/orgs have the same named repository (e.g. forks) it will .
  23. Will set up remotes named after the user/org in the event the repo is a fork, and set the 'upstream' name to be the parent repository.
  24. This will not recursively scan for the oldest ancestor as parent; most of the time that's a bad idea.
  25. In the event that all the copies of a repo happen to be a fork on the passed users/orgs,
  26. whatever the --primary_user or --primary_org will be preferred.
  27. It will warn you whenever a repository is missing from either, so you can make it go whirr appropriately.
  28. Using this you can easily migrate an organization from being entirely on github to using private resources or vice versa.
  29. =head1 IMPORTANT
  30. This assumes that the repo names between the base and mirrors is identical.
  31. =head1 CONFIG FILE
  32. You will notice below that the options of this tool can be quite involved.
  33. To simplify deploying this tool across your organization, you can place a configuration file (Config::Simple) in ~/.git/clone-entity.cfg. Example:
  34. baseurl=https://my-gogs-install.test/api/v1
  35. nossh=true
  36. mirrors=https://api.github.com,https://premise-install.github.local/api
  37. me=jane
  38. Ideally all your users have to do is specify which users/orgs to clone w/mirroring and you should be off to the races.
  39. The name of the setting will be pluralized for any option which may be passed multiple times below.
  40. =head1 USAGE
  41. git clone-entity --user $user1 --user $user2 --org $org1 --org $org2 --alias $user1:$mirror_domain:$mirrorUser1 --baseurl=https://my.local.install/ [--mirror https://github.com] [--nossh] [--insecure] [--help]
  42. =head1 OPTIONS
  43. =head3 me
  44. Your username on the baseurl. Relevant to token use, what is visible, etc.
  45. --me tarzan
  46. =head3 baseurl
  47. URI for your Git management solution. Currently github and gogs are supported.
  48. --baseurl https://api.github.com
  49. --baseurl https://gogs.mydomain.test/api/v1
  50. =head3 mirror
  51. URI for a git management solution you wish to use for mirroring the repos at the baseurl. May be passed multiple times.
  52. --mirror https://on-prem.github.local/api/
  53. =head3 token
  54. Token for a particular baseurl or mirror. Of the format domain:token.
  55. --token my.domain.test:DEADBEEF
  56. You can omit the auth token on gogs, as we can create them automatically (we will prompt for your password).
  57. =head3 primary_user, primary_org
  58. Primary entity to clone. Consider their repository to be the canonical one. One or the other must be passed. In the event both are, the org is preferred.
  59. In most organizations, you will have the org hold the primary copy of a repo, with developers forking copies.
  60. --primary_org 'BigHugsLLC'
  61. =head3 user
  62. Clone all of this user's repositories. May be passed multiple times.
  63. --user fred
  64. =head3 org
  65. Clone all of this organization's repositories. May be passed multiple times.
  66. --org 'Granite-Industries'
  67. =head3 alias
  68. Map a user/org on your baseurl to a mirror. Of the format base_user:mirror_domain:mirror_user.
  69. Obviously won't work if the mirror is on the same hostname as the baseurl; use a subdomain at the very least.
  70. --alias george:sprockets.spacely.local:gjetson
  71. =head3 nossh
  72. Don't use SSH clone URIs. Useful for read-only clones & deployments with no ssh-agent.
  73. --nossh
  74. =head3 remote
  75. Name of primary remote. By default will be 'origin', but 'all' is popular.
  76. In the event this is not origin, origin will be set to be the push/pull for the repo at the baseurl.
  77. --remote all
  78. =head1 CONSEQUENTIAL OPTIONS
  79. =head3 insecure
  80. Allow insecure mirrors or baseurls. This is just to prevent footgunning by passing auth over plaintext.
  81. --insecure
  82. =head3 create
  83. Automatically create a copy of the repo on the mirror if it doesn't exist.
  84. --create
  85. =head3 private
  86. If --create is passed, also mirror repositories marked as private, preserving privacy.
  87. =head3 sync
  88. Force push all refs onto the mirror(s).
  89. --sync
  90. =cut
  91. sub _help {
  92. my ($code, $msg, $cb) = @_;
  93. $code //= 0;
  94. $msg //= "";
  95. $cb->() if ref $cb eq 'CODE';
  96. return Pod::Usage::pod2usage( -message => $msg, -exitval => $code);
  97. }
  98. my $domainRipper = qr{^\w+://([\w|\.]+)};
  99. sub main {
  100. my @args = @_;
  101. my %options = (
  102. help => undef,
  103. users => [],
  104. orgs => [],
  105. aliases => [],
  106. tokens => [],
  107. mirrors => [],
  108. baseurl => "",
  109. me => undef,
  110. create => undef,
  111. sync => undef,
  112. insecure => undef,
  113. nossh => undef,
  114. remote => 'origin',
  115. primary_user => undef,
  116. primary_org => undef,
  117. );
  118. # Allow options to override configuration
  119. my $home = $ENV{HOME};
  120. mkdir "$home/.git" unless -d "$home/.git";
  121. my $config_file = "$home/.git/clone-entity.cfg";
  122. if (-f $config_file) {
  123. my $conf = Config::Simple->new($config_file);
  124. my %config;
  125. %config = %{$conf->param(-block => 'default')} if $conf;
  126. # Merge the configuration with the options
  127. foreach my $opt (keys(%options)) {
  128. if ( ref $options{$opt} eq 'ARRAY' ) {
  129. next unless exists $config{$opt};
  130. my @arrayed = ref $config{$opt} eq 'ARRAY' ? @{$config{$opt}} : ($config{$opt});
  131. push(@{$options{$opt}}, @arrayed);
  132. next;
  133. }
  134. $options{$opt} = $config{$opt} if exists $config{$opt};
  135. }
  136. }
  137. GetOptionsFromArray(\@args,
  138. 'me=s' => \$options{me},
  139. 'user=s@' => \$options{users},
  140. 'alias=s@' => \$options{aliases},
  141. 'token=s@' => \$options{tokens},
  142. 'org=s@' => \$options{orgs},
  143. 'baseurl=s' => \$options{baseurl},
  144. 'mirror=s@' => \$options{mirrors},
  145. 'insecure' => \$options{insecure},
  146. 'nossh' => \$options{nossh},
  147. 'help' => \$options{help},
  148. 'primary_user=s' => \$options{primary},
  149. 'primary_org=s' => \$options{primary_org},
  150. );
  151. # Tiebreaker vote in the event of conflicting forks
  152. push(@{$options{users}}, $options{primary_user}) if $options{primary_user};
  153. push(@{$options{orgs}}, $options{primary_org}) if $options{primary_org};
  154. my $prime_name = $options{primary_org} || $options{primary_user};
  155. return _help() if $options{help};
  156. return _help(1, "Must pass either primary_user or primary_org") unless $prime_name;
  157. return _help(1, "Must pass at least one of: user or org") unless (@{$options{users}} + @{$options{orgs}});
  158. return _help(2, "Must pass baseurl") unless $options{baseurl};
  159. return _help(3, "Must pass your username as --me") unless $options{me};
  160. # Parse Alias mappings
  161. my %alias_map;
  162. foreach my $arg (@{$options{aliases}}) {
  163. my ($actual, $domain, $alias) = split(/:/, $arg);
  164. return _help(3, "aliases must be of the form user:domain:alias") unless $actual && $domain && $alias;
  165. $alias_map{$domain}{$actual} = $alias;
  166. }
  167. # Parse tokens
  168. my %tokens;
  169. foreach my $tok (@{$options{tokens}}) {
  170. my ($domain, $token) = split(/:/, $tok);
  171. return _help(4, "tokens must be of the form domain:token") unless $domain && $token;
  172. $tokens{$domain} = $token;
  173. }
  174. # Simplify code below by making the primary just another mirror to fetch
  175. unshift(@{$options{mirrors}}, $options{baseurl});
  176. my $field_name = $options{nossh} ? 'clone_url' : 'ssh_url';
  177. my @repos;
  178. my (%passwords, %clients);
  179. my $cleanup = sub { _cleanup_tokens( \%clients, \%passwords, $options{insecure} ) if %passwords };
  180. foreach my $mirror_url (@{$options{mirrors}}) {
  181. my $server_is_github = _server_is_github($mirror_url);
  182. my ($mirror_domain) = $mirror_url =~ $domainRipper;
  183. my $muser = $options{me};
  184. $muser = $alias_map{$mirror_domain}{$muser} if exists $alias_map{$mirror_domain}{$muser};
  185. my %margs = (
  186. user => $muser,
  187. api_uri => $mirror_url,
  188. );
  189. $margs{token} = $tokens{$mirror_domain} if $tokens{$mirror_domain};
  190. my $mirror = $server_is_github ? Pithub->new(%margs) : Gogs->new(%margs);
  191. # Then it's gogs, and we can just make one.
  192. if (!$margs{token} && !$server_is_github) {
  193. _help(5, "Program must be run interactively to auto-create keys on Gogs installs.") unless IO::Interactive::Tiny::is_interactive();
  194. # Stash the password in case we gotta clean up
  195. $passwords{$mirror_domain} = _prompt("Please type in the password for ".$mirror->user.":");
  196. $tokens{$mirror_domain} = $mirror->get_token(
  197. name => "git-clone-entity",
  198. password => $passwords{$mirror_domain},
  199. insecure => $options{insecure},
  200. );
  201. _help(6, "Could not fetch token from gogs! Check that you supplied the correct username & password.") unless $tokens{$mirror_domain};
  202. $mirror->token($tokens{$mirror_domain});
  203. # Stash for later use by cleanup routines if needed
  204. $clients{$mirror_domain} = $mirror;
  205. }
  206. my @fetched = _fetch_all($mirror, $options{users}, $options{orgs}, \%alias_map, $field_name);
  207. _help(7, "The provided server ($mirror_url) could not list repos!", $cleanup ) unless @fetched;
  208. push(@repos, @fetched);
  209. }
  210. #TODO actually differentiate between the various clones, build username/org remotes
  211. # Build a map of names to clone URIs
  212. my %names2clone = map { $_->{name} => $_->{$field_name} } @repos;
  213. my %upstreams = map { $_->{name} => $_->{upstream_uri} } @repos;
  214. # Figure out which repos are private so we don't mirror them unless instructed to.
  215. my @private_repos = map { $_->{name} } grep { $_->{private} } @repos;
  216. $cleanup->();
  217. use Data::Dumper;
  218. die Dumper(\%names2clone, \%upstreams, \@private_repos);
  219. # Clean up
  220. $cleanup->();
  221. return 0;
  222. }
  223. sub _clone_repos {
  224. }
  225. sub _fetch_upstream_uri {
  226. my ($mirror, $field_name, $muser, $repo) = @_;
  227. my $upstream_uri;
  228. if ($repo->{fork}) {
  229. my $details = $mirror->repos->get( user => $muser, repo => $repo->{name});
  230. _help(9, "Could not fetch repository details for $repo->{name}") unless $details && $details->response->is_success();
  231. my $content = $details->content();
  232. $upstream_uri = $content->{parent}{$field_name};
  233. _help(10, "Could not discern upstream URI for forked repo $repo->{name}!") unless $upstream_uri;
  234. }
  235. return $upstream_uri;
  236. }
  237. sub _parse_remotes {
  238. my ($raw) = shift;
  239. my %parsed;
  240. foreach my $line (split(/\n/, $raw)) {
  241. my ($name, $uri, $type) = $line =~ m/^(.+)\s+(.+)\s+\((.+)\)$/;
  242. if ($type eq 'fetch') {
  243. $parsed{$name}{$type} = $uri;
  244. } else {
  245. $parsed{$name}{$type} //= [];
  246. push(@{$parsed{$name}{$type}}, $uri);
  247. }
  248. }
  249. return %parsed;
  250. }
  251. sub _cleanup_tokens {
  252. my ( $apis, $passwords, $insecure ) = @_;
  253. foreach my $domain (keys(%$apis)) {
  254. my $api = $apis->{$domain};
  255. my $result = $api->delete_token( sha1 => $api->token, password => $passwords->{$domain}, insecure => $insecure );
  256. die "Could not clean up token" unless $result && $result->response->is_success;
  257. }
  258. }
  259. sub _prompt {
  260. my ( $prompt ) = @_;
  261. $prompt ||= "";
  262. my $input = "";
  263. print $prompt;
  264. # We are readin a password
  265. Term::ReadKey::ReadMode('noecho');
  266. {
  267. local $SIG{'INT'} = sub { Term::ReadKey::ReadMode(0); exit 130; };
  268. $input = <STDIN>;
  269. chomp($input) if $input;
  270. }
  271. Term::ReadKey::ReadMode(0);
  272. print "\n";
  273. return $input;
  274. }
  275. sub _fetch_all {
  276. my ($api, $users, $orgs, $alias_map, $field_name) = @_;
  277. my ($domain) = $api->api_uri =~ $domainRipper;
  278. # TODO detect which repo among forks is the "primary" (if one of them is not a fork, use it)
  279. my @repos;
  280. foreach my $user (@$users) {
  281. $user = $alias_map->{$domain}{$user} if exists $alias_map->{$domain}{$user};
  282. my $result = $api->repos->list( user => $user );
  283. my @fetched = _array_content($result);
  284. @fetched = _augment_repos($api, $field_name, $user, $domain, @fetched);
  285. push(@repos, @fetched);
  286. }
  287. foreach my $org (@$orgs) {
  288. $org = $alias_map->{$domain}{$org} if exists $alias_map->{$domain}{$org};
  289. my $result = $api->repos->list( org => $org );
  290. my @fetched = _array_content($result);
  291. @fetched = _augment_repos($api, $field_name, $org, $domain, @fetched);
  292. push(@repos, @fetched);
  293. }
  294. return @repos;
  295. }
  296. sub _array_content {
  297. my ($result) = @_;
  298. return () unless $result && $result->response->is_success;
  299. return @{$result->content()} if ref $result->content() eq 'ARRAY';
  300. return ();
  301. }
  302. sub _augment_repos {
  303. my ($mirror, $field_name, $muser, $domain, @fetched) = @_;
  304. @fetched = map {
  305. my $subj = $_;
  306. $subj->{domain} = $domain;
  307. $subj->{upstream_uri} = _fetch_upstream_uri($mirror, $field_name, $muser, $subj);
  308. $subj
  309. } @fetched;
  310. return @fetched;
  311. }
  312. sub _server_is_github {
  313. my ($uri) = @_;
  314. my $ua = HTTP::Tiny->new();
  315. my $res = $ua->get($uri);
  316. # GOGS will 404 it's api baseurl, github will not
  317. return $res->{success};
  318. }
  319. exit main(@ARGV) unless caller;
  320. 1;