Skip to content

Instantly share code, notes, and snippets.

@JRaspass
Created February 3, 2021 18:23
Show Gist options
  • Select an option

  • Save JRaspass/650254a80c9e1a2903c932d9a686eaa3 to your computer and use it in GitHub Desktop.

Select an option

Save JRaspass/650254a80c9e1a2903c932d9a686eaa3 to your computer and use it in GitHub Desktop.
# Update stars.
my %queries;
for $db.query(q:to/SQL/).arrays {
SELECT DISTINCT url FROM dists WHERE url ~ '^https://git(hub|lab)\.com/'
SQL
my ( $, $, $domain, $owner, $repo ) = .split("/");
my $id = 'i' ~ ++$;
%queries{$domain}.push: $domain eq 'github.com'
?? Q:s{$id: repository(owner: "$owner" name: "$repo") { ...r }}
!! Q:s{$id: project(fullPath: "$owner/$repo") { ...p }};
}
my $query = Q:h{
fragment p on Project { fullPath, starCount } { %queries<gitlab.com>[] }
};
%res = $ua.post(
'https://gitlab.com/api/graphql',
content => to-json({ :$query }),
headers => :content-type<application/json>,
)<content>.decode.&from-json;
die %res<errors>».<message> if %res<errors>;
my %stars = %res<data>.values.grep(*.defined).map: {
'https://gitlab.com/' ~ .<fullPath> => .<starCount>;
};
with %*ENV<GITHUB_TOKEN> {
my $query = Q:h{
fragment r on Repository { nameWithOwner, stargazerCount }
{ %queries<github.com>[] }
};
my %res = $ua.post(
'https://api.github.com/graphql',
content => to-json({ :$query }),
headers => {
authorization => "Bearer $_",
content-type => 'application/json',
},
)<content>.decode.&from-json;
die %res<errors>».<message> if %res<errors>;
%stars{ 'https://github.com/' ~ .<nameWithOwner> } = .<stargazerCount>
for %res<data>.values.grep: *.defined;
}
my $update-stars = $db.prepare: 'UPDATE dists SET stars = $1 WHERE url = $2';
$update-stars.execute: .value, .key for %stars;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment