Skip to content

Commit

Permalink
removing extra functions that now live in classes
Browse files Browse the repository at this point in the history
  • Loading branch information
gagoar committed Jan 21, 2021
1 parent f785c5d commit 96acace
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 76 deletions.
56 changes: 24 additions & 32 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 29 additions & 44 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,6 @@ const isValidRawRule = (content: unknown): content is RawRule => {
return hasValidActionValues && hasActors && matchers;
};

const loadRules = (rulesLocation: string): Rule[] => {
const matches = sync(rulesLocation, {
onlyFiles: true,
cwd: env.GITHUB_WORKSPACE,
absolute: true,
});

debug('files found:', matches);
const rules = matches.reduce((memo, filePath) => {
try {
const rule = loadJSONFile(filePath);

return isValidRawRule(rule) ? [...memo, { name: basename(filePath), ...sanitize(rule), path: filePath }] : memo;
} catch (e) {
console.error(`${filePath} can't be parsed, it will be ignored`);
return memo;
}
}, [] as Rule[]);

return rules;
};

export type MatchingRule = Rule & {
matched: boolean;
};
Expand Down Expand Up @@ -256,31 +234,29 @@ const isMatch: Matcher = (rule, options) => {
return matches.length ? matches.every((match) => match === true) : false;
};

export const getMatchingRules = (
rules: Rule[],
files: RuleFile[],
event: Event,
patchContent: string[] = []
): MatchingRule[] => {
const fileNames = files.map(({ filename }) => filename);

const matchingRules = rules.reduce((memo, rule) => {
if (isMatch(rule, { event, patch: patchContent, fileNames })) {
return [...memo, { ...rule, matched: true }];
} else {
return memo;
}
}, [] as MatchingRule[]);

return matchingRules;
};

export class Rules extends Array<Rule> {
public constructor(...items: Rule[]) {
super(...items);
}
static loadFromLocation(location: string): Rules {
const rules = loadRules(location);
const matches = sync(location, {
onlyFiles: true,
cwd: env.GITHUB_WORKSPACE,
absolute: true,
});

debug('files found:', matches);
const rules = matches.reduce((memo, filePath) => {
try {
const rule = loadJSONFile(filePath);

return isValidRawRule(rule) ? [...memo, { name: basename(filePath), ...sanitize(rule), path: filePath }] : memo;
} catch (e) {
console.error(`${filePath} can't be parsed, it will be ignored`);
return memo;
}
}, [] as Rule[]);

return new Rules(...rules);
}
getMatchingRules(files: RuleFile[], event: Event, patchContent?: string[]): MatchingRules {
Expand All @@ -302,8 +278,17 @@ class MatchingRules extends Array<MatchingRule> {
return grouped || [];
}

static load(rules: Rules, files: RuleFile[], event: Event, patchContent?: string[]): MatchingRules {
const matchingRules = getMatchingRules(rules, files, event, patchContent);
static load(rules: Rules, files: RuleFile[], event: Event, patchContent: string[] = []): MatchingRules {
const fileNames = files.map(({ filename }) => filename);

const matchingRules = rules.reduce((memo, rule) => {
if (isMatch(rule, { event, patch: patchContent, fileNames })) {
return [...memo, { ...rule, matched: true }];
} else {
return memo;
}
}, [] as MatchingRule[]);

return new MatchingRules(...matchingRules);
}
}

0 comments on commit 96acace

Please sign in to comment.