Skip to content

Commit

Permalink
feat: Add support for ignoring specific repositories
Browse files Browse the repository at this point in the history
resolves #95
  • Loading branch information
stefanbuck committed Oct 23, 2022
1 parent c98b162 commit f2e0fa6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 4 deletions.
19 changes: 16 additions & 3 deletions lib/resolve-repositories.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
export async function resolveRepositories(state, repositories) {
const ignoreRepositories = repositories.reduce((memo, fullName) => {
if (fullName.startsWith('!')) {
memo.push(fullName.slice(1))
}
return memo;
}, []);

const repositoriesWithStars = repositories.filter((fullName) => {
return /^[a-z0-9_.-]+\/(\*|[a-z0-9_.-]+\*|\*[a-z0-9_.-]+|[a-z0-9_.-]+\*[a-z0-9_.-]+)$/i.test(
fullName
);
) && !fullName.startsWith('!');
});

const repositoriesWithoutStars = repositories.filter((fullName) => {
return !/\*/i.test(fullName);
return !/\*/i.test(fullName) && !fullName.startsWith('!');
});
const allRepositories = !!repositories.find((name) => name === "*");

Expand Down Expand Up @@ -79,6 +86,12 @@ export async function resolveRepositories(state, repositories) {

process.stdout.write("\n");

function includesRepository(list, repo) {
return list.some((x) => x.toLowerCase() === repo.full_name.toLowerCase())
}

// return array with unique repositories based by id (https://stackoverflow.com/a/56757215)
return resolvedRepositories.filter((repo, index, repoList) => repoList.findIndex(v2 => (v2.id === repo.id)) === index);
return resolvedRepositories
.filter((repo, index, repoList) => repoList.findIndex(v2 => (v2.id === repo.id)) === index)
.filter(repo => !includesRepository(ignoreRepositories, repo))
}
2 changes: 1 addition & 1 deletion lib/run-script-against-repositories.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function runScriptAgainstRepositories(state, octoherdRepos = []) {
const invalid = values.find((value) => {
if (value.trim() === "*") return;

if (/^[a-zA-Z0-9_.-]+\/[a-zA-Z0-9_.*-]+$/.test(value.trim())) {
if (/^!?[a-zA-Z0-9_.-]+\/[a-zA-Z0-9_.*-]+$/.test(value.trim())) {
return;
}

Expand Down
82 changes: 82 additions & 0 deletions tests/resolve-repositories.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,88 @@ withOrg("when requesting the same repository twice", async () => {
equal(resolvedRepos, [mockedResponse]);
});

withOrg("when requesting all repositories where one repository is ignored", async () => {
const octokit = new Octokit({ auth: "randomToken" });

const repositories = ['octoherd/*', '!octoherd/cli'];

const mockedResponse = [
{ id: 1, name: "cli", full_name: 'octoherd/cli' },
{ id: 2, name: "octokit", full_name: 'octoherd/octokit' },
{ id: 3, name: "octoherd", full_name: 'octoherd/octoherd' },
];

simple.mock(octokit, "request").resolveWith({ data: undefined });

simple.mock(octokit.paginate, "iterator").returnWith({
async *[Symbol.asyncIterator]() {
yield { data: mockedResponse };
},
});

const resolvedRepos = await resolveRepositories(
{
log: console,
octokit,
},
repositories
);

equal(resolvedRepos, [
{ id: 2, name: "octokit", full_name: 'octoherd/octokit' },
{ id: 3, name: "octoherd", full_name: 'octoherd/octoherd' },
]);
});

withOrg("when one of the requested repositories is ignored", async () => {
const octokit = new Octokit({ auth: "randomToken" });

const repositories = ['!octoherd/cli', 'octoherd/octokit'];

const mockedResponse = [
{ id: 1, name: "cli", full_name: 'octoherd/cli' },
{ id: 2, name: "octokit", full_name: 'octoherd/octokit' },
];

simple.mock(octokit, "request").resolveWith({ data: mockedResponse[0] });
simple.mock(octokit, "request").resolveWith({ data: mockedResponse[1] });

const resolvedRepos = await resolveRepositories(
{
log: console,
octokit,
},
repositories
);

equal(resolvedRepos, [
{ id: 2, name: "octokit", full_name: 'octoherd/octokit' },
]);
});

withOrg("when requested repository is ignored", async () => {
const org = "octoherd";
const repo = "cli";
const octokit = new Octokit({
auth: "randomToken",
});

const mockedResponse = { id: 1, name: repo };
const repositories = [`!${org}/${repo.toUpperCase()}`];

simple.mock(octokit, "request").resolveWith({ data: mockedResponse });

const resolvedRepos = await resolveRepositories(
{
log: console,
octokit,
},
repositories
);

equal(resolvedRepos, []);
});

withOrg("when requesting all the repositories", async () => {
const org = "octoherd";
const repo = "*";
Expand Down

0 comments on commit f2e0fa6

Please sign in to comment.