Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Improve tests and typing & package d.ts files. #270

Merged
merged 1 commit into from May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions .c8rc.json
@@ -1,6 +1,6 @@
{
"statements": "78",
"branches": "78",
"functions": "82",
"lines": "78"
"statements": "80",
"branches": "84",
"functions": "88",
"lines": "80"
}
10 changes: 3 additions & 7 deletions lib/allowlist.ts
@@ -1,5 +1,5 @@
import type { GitHubAdvisoryId } from "audit-types";
import { isGitHubAdvisoryId } from "./common";
import { deduplicate, isGitHubAdvisoryId } from "./common";
import type { AuditCiPreprocessedConfig } from "./config";

class Allowlist {
Expand Down Expand Up @@ -37,12 +37,8 @@ class Allowlist {
config: Pick<AuditCiPreprocessedConfig, "allowlist">
) {
const { allowlist } = config;
// It's possible someone duplicated the inputs.
// The solution is to merge into one array, change to set, and back to array.
// This will remove duplicates.
const set = new Set(allowlist || []);
const input = [...set];
const allowlistObject = new Allowlist(input);
const deduplicatedAllowlist = deduplicate(allowlist || []);
const allowlistObject = new Allowlist(deduplicatedAllowlist);
return allowlistObject;
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/audit.ts
Expand Up @@ -15,7 +15,9 @@ const PARTIAL_RETRY_ERROR_MSG = {
yarn: "503 Service Unavailable",
};

function getAuditor(packageManager: "npm" | "yarn" | "pnpm") {
function getAuditor(
packageManager: "npm" | "yarn" | "pnpm"
): typeof yarnAuditer | typeof npmAuditer | typeof pnpmAuditer {
switch (packageManager) {
case "yarn":
return yarnAuditer;
Expand Down
8 changes: 6 additions & 2 deletions lib/common.ts
Expand Up @@ -186,8 +186,8 @@ export function matchString(template: string, string_: string) {
: template === string_;
}

export function isGitHubAdvisoryId(id: string): id is GitHubAdvisoryId {
return id.startsWith("GHSA");
export function isGitHubAdvisoryId(id: unknown): id is GitHubAdvisoryId {
return typeof id === "string" && id.startsWith("GHSA");
}

export function gitHubAdvisoryUrlToAdvisoryId(url: string): GitHubAdvisoryId {
Expand All @@ -199,3 +199,7 @@ export function gitHubAdvisoryIdToUrl<T extends string>(
): `https://github.com/advisories/${T}` {
return `https://github.com/advisories/${id}`;
}

export function deduplicate(array: readonly string[]) {
return [...new Set(array)];
}