Fetch stars from Gitlab

This commit is contained in:
Ambar Mutha
2021-09-07 16:17:36 +05:30
parent be73ba9f2a
commit 308158ec22
6 changed files with 278 additions and 331 deletions

View File

@@ -6,7 +6,7 @@
*/
import fs from 'fs';
import { graphql } from '@octokit/graphql';
import { request, gql } from 'graphql-request';
import prettier from 'prettier';
const files = [
@@ -20,62 +20,83 @@ if (!process.env.GH_TOKEN) {
process.exit(1);
}
const getGithubRepo = ({ url }) => {
const match = url.match(/github.com\/([^#/]+)\/([^#/]+)/);
const getRepo = ({ url }) => {
const match = url.match(/(github.com|gitlab.com)\/([^#/]+)\/([^#/]+)/);
if (match) {
const [, owner, name] = match;
return `${owner}/${name}`;
const [, site, owner, name] = match;
const id = `${site}/${owner}/${name}`;
return { site, owner, name, id };
}
};
const repos = files.flatMap(
const reposWithDuplicates = files.flatMap(
(file) =>
JSON.parse(fs.readFileSync(file))
.map(getGithubRepo)
.map(getRepo)
.filter((x) => x) // filter undefined
);
const repos = getUnique(reposWithDuplicates);
const uniqueRepos = [...new Set(repos)].map((repo) => {
const [owner, name] = repo.split('/');
return { owner, name };
});
const githubRepos = repos.filter((repo) => repo.site === 'github.com');
const gitlabRepos = repos.filter((repo) => repo.site === 'gitlab.com');
const repoQuery = (repo, id) => `
r${id}: repository(owner: "${repo.owner}", name: "${repo.name}") {
const gitlabRepoQuery = (repo, idx) => `
r${idx}: project(fullPath: "${repo.owner}/${repo.name}") {
...frag
}
`;
const response = await graphql(
/* GraphQL */ `
{
${uniqueRepos.map(repoQuery).join('')}
}
fragment frag on Repository {
owner {
login
}
name
stargazerCount
}
`,
const gitlabQuery = gql`
{
headers: {
authorization: `token ${process.env.GH_TOKEN}`
}
${gitlabRepos.map(gitlabRepoQuery).join('')}
}
fragment frag on Project {
starCount
fullPath
}
`;
const gitlabResponse = await request('https://gitlab.com/api/graphql', gitlabQuery);
const ghRepoQuery = (repo, idx) => `
r${idx}: repository(owner: "${repo.owner}", name: "${repo.name}") {
...frag
}
`;
const ghQuery = gql`
{
${githubRepos.map(ghRepoQuery).join('')}
}
fragment frag on Repository {
resourcePath
stargazerCount
}
`;
const ghResponse = await request(
'https://api.github.com/graphql',
ghQuery,
{},
{ authorization: `token ${process.env.GH_TOKEN}` }
);
const repoData = Object.fromEntries(
Object.values(response).map((repo) => [`${repo.owner.login}/${repo.name}`, repo])
);
const repoData = {};
for (const repo of Object.values(gitlabResponse)) {
repoData[`gitlab.com/${repo.fullPath}`] = { stars: repo.starCount };
}
for (const repo of Object.values(ghResponse)) {
repoData[`github.com${repo.resourcePath}`] = { stars: repo.stargazerCount };
}
for (const file of files) {
const data = JSON.parse(fs.readFileSync(file));
for (const item of data) {
const repo = getGithubRepo(item);
if (repo && repoData[repo]) {
item.stars = repoData[repo].stargazerCount;
const repo = getRepo(item);
if (repo && repoData[repo.id]) {
item.stars = repoData[repo.id].stars;
}
}
prettySave(file, JSON.stringify(data), 'json');
@@ -90,3 +111,11 @@ function prettySave(filePath, text, parser = 'babel') {
fs.writeFileSync(filePath, formatted);
});
}
function getUnique(repos) {
const urls = repos.map((repo) => `${repo.site}/${repo.owner}/${repo.name}`);
return [...new Set(urls)].map((url) => {
const [site, owner, name] = url.split('/');
return { site, owner, name };
});
}