2024-10-17 20:31:18 +00:00
|
|
|
import { Logger } from "winston";
|
|
|
|
|
import { AppContext, Repository } from "./types.js";
|
2024-11-01 17:51:14 +00:00
|
|
|
import path from 'path';
|
2024-11-14 22:16:31 +00:00
|
|
|
import micromatch from "micromatch";
|
2024-10-17 20:31:18 +00:00
|
|
|
|
|
|
|
|
export const measure = async <T>(cb : () => Promise<T>) => {
|
|
|
|
|
const start = Date.now();
|
|
|
|
|
const data = await cb();
|
|
|
|
|
const durationMs = Date.now() - start;
|
|
|
|
|
return {
|
|
|
|
|
data,
|
|
|
|
|
durationMs
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const marshalBool = (value?: boolean) => {
|
|
|
|
|
return !!value ? '1' : '0';
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-01 17:51:14 +00:00
|
|
|
export const excludeForkedRepos = <T extends Repository>(repos: T[], logger?: Logger) => {
|
2024-10-17 20:31:18 +00:00
|
|
|
return repos.filter((repo) => {
|
2024-11-01 17:51:14 +00:00
|
|
|
if (!!repo.isFork) {
|
2024-12-11 22:17:57 +00:00
|
|
|
logger?.debug(`Excluding repo ${repo.id}. Reason: \`exclude.forks\` is true`);
|
2024-10-17 20:31:18 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-01 17:51:14 +00:00
|
|
|
export const excludeArchivedRepos = <T extends Repository>(repos: T[], logger?: Logger) => {
|
2024-10-17 20:31:18 +00:00
|
|
|
return repos.filter((repo) => {
|
2024-11-01 17:51:14 +00:00
|
|
|
if (!!repo.isArchived) {
|
2024-12-11 22:17:57 +00:00
|
|
|
logger?.debug(`Excluding repo ${repo.id}. Reason: \`exclude.archived\` is true`);
|
2024-10-17 20:31:18 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 22:16:31 +00:00
|
|
|
|
2024-11-01 17:51:14 +00:00
|
|
|
export const excludeReposByName = <T extends Repository>(repos: T[], excludedRepoNames: string[], logger?: Logger) => {
|
2024-10-17 20:31:18 +00:00
|
|
|
return repos.filter((repo) => {
|
2024-11-14 22:16:31 +00:00
|
|
|
if (micromatch.isMatch(repo.name, excludedRepoNames)) {
|
2024-12-11 22:17:57 +00:00
|
|
|
logger?.debug(`Excluding repo ${repo.id}. Reason: \`exclude.repos\` contains ${repo.name}`);
|
2024-10-17 20:31:18 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-03 00:07:02 +00:00
|
|
|
export const includeReposByName = <T extends Repository>(repos: T[], includedRepoNames: string[], logger?: Logger) => {
|
|
|
|
|
return repos.filter((repo) => {
|
|
|
|
|
if (micromatch.isMatch(repo.name, includedRepoNames)) {
|
2024-12-11 22:17:57 +00:00
|
|
|
logger?.debug(`Including repo ${repo.id}. Reason: \`repos\` contain ${repo.name}`);
|
2024-12-03 00:07:02 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-11 22:17:57 +00:00
|
|
|
export const includeReposByTopic = <T extends Repository>(repos: T[], includedRepoTopics: string[], logger?: Logger) => {
|
|
|
|
|
return repos.filter((repo) => {
|
|
|
|
|
const topics = repo.topics ?? [];
|
|
|
|
|
const matchingTopics = topics.filter((topic) => micromatch.isMatch(topic, includedRepoTopics));
|
|
|
|
|
|
|
|
|
|
if (matchingTopics.length > 0) {
|
|
|
|
|
|
|
|
|
|
logger?.debug(`Including repo ${repo.id}. Reason: \`topics\` matches the following topics: ${matchingTopics.join(', ')}`);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const excludeReposByTopic = <T extends Repository>(repos: T[], excludedRepoTopics: string[], logger?: Logger) => {
|
|
|
|
|
return repos.filter((repo) => {
|
|
|
|
|
const topics = repo.topics ?? [];
|
|
|
|
|
const matchingTopics = topics.filter((topic) => micromatch.isMatch(topic, excludedRepoTopics));
|
|
|
|
|
|
|
|
|
|
if (matchingTopics.length > 0) {
|
|
|
|
|
logger?.debug(`Excluding repo ${repo.id}. Reason: \`exclude.topics\` matches the following topics: ${matchingTopics.join(', ')}`);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 20:31:18 +00:00
|
|
|
export const getTokenFromConfig = (token: string | { env: string }, ctx: AppContext) => {
|
|
|
|
|
if (typeof token === 'string') {
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
const tokenValue = process.env[token.env];
|
|
|
|
|
if (!tokenValue) {
|
|
|
|
|
throw new Error(`The environment variable '${token.env}' was referenced in ${ctx.configPath}, but was not set.`);
|
|
|
|
|
}
|
|
|
|
|
return tokenValue;
|
2024-10-17 23:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const isRemotePath = (path: string) => {
|
|
|
|
|
return path.startsWith('https://') || path.startsWith('http://');
|
2024-11-01 17:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const resolvePathRelativeToConfig = (localPath: string, configPath: string) => {
|
|
|
|
|
let absolutePath = localPath;
|
|
|
|
|
if (!path.isAbsolute(absolutePath)) {
|
|
|
|
|
if (absolutePath.startsWith('~')) {
|
|
|
|
|
absolutePath = path.join(process.env.HOME ?? '', absolutePath.slice(1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
absolutePath = path.resolve(path.dirname(configPath), absolutePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return absolutePath;
|
2024-11-07 02:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-13 02:37:35 +00:00
|
|
|
export const arraysEqualShallow = <T>(a?: readonly T[], b?: readonly T[]) => {
|
2024-11-07 02:28:10 +00:00
|
|
|
if (a === b) return true;
|
|
|
|
|
if (a === undefined || b === undefined) return false;
|
|
|
|
|
if (a.length !== b.length) return false;
|
|
|
|
|
|
2024-11-13 02:37:35 +00:00
|
|
|
const aSorted = a.toSorted();
|
|
|
|
|
const bSorted = b.toSorted();
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < aSorted.length; i++) {
|
|
|
|
|
if (aSorted[i] !== bSorted[i]) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2024-11-14 22:16:31 +00:00
|
|
|
}
|