@simulacrum/github-api-simulator
Version:
Provides common functionality to frontend app and plugins.
396 lines • 18.8 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertInitialStateToStoreState = exports.convertToObj = exports.gitubInitialStoreSchema = exports.githubBlobSchema = exports.githubOrganizationSchema = exports.githubBranchSchema = exports.githubRepositorySchema = exports.githubAppInstallationSchema = exports.githubUserSchema = void 0;
const zod_1 = require("zod");
const faker_1 = require("@faker-js/faker");
exports.githubUserSchema = zod_1.z
.object({
id: zod_1.z.string().or(zod_1.z.number()).default(""),
login: zod_1.z.string(),
name: zod_1.z.string().optional(),
email: zod_1.z.string().optional(),
organizations: zod_1.z.array(zod_1.z.string()),
})
.transform((user) => {
user.id = user.login;
if (!user.name)
user.name = user.login;
if (!user.email)
user.email = faker_1.faker.internet.email({ firstName: user.name });
return user;
});
const githubEntityPermissionSchema = zod_1.z
.object({
admin: zod_1.z.boolean().optional().default(false),
push: zod_1.z.boolean().optional().default(false),
pull: zod_1.z.boolean().optional().default(true),
})
.optional()
.default({ admin: false, push: false, pull: true });
exports.githubAppInstallationSchema = zod_1.z
.object({
id: zod_1.z.number().optional(),
account: zod_1.z.string(),
repository_selection: zod_1.z.enum(["all", "selected"]).optional().default("all"),
app_id: zod_1.z.number().optional().default(1),
access_tokens_url: zod_1.z.string().optional(),
repositories_url: zod_1.z.string().optional(),
html_url: zod_1.z.string().optional(),
client_id: zod_1.z.string().optional().default("Iv1.ab1112223334445c"),
target_id: zod_1.z.number().optional(),
target_type: zod_1.z.enum(["Organization", "User"]).optional(),
permissions: githubEntityPermissionSchema,
events: zod_1.z.array(zod_1.z.any()).optional().default([]),
updated_at: zod_1.z
.string()
.optional()
.default(() => faker_1.faker.date.recent().toISOString()),
created_at: zod_1.z
.string()
.optional()
.default(() => faker_1.faker.date.recent().toISOString()),
single_file_name: zod_1.z.string().optional().default("config.yml"),
has_multiple_single_files: zod_1.z.boolean().optional().default(true),
single_file_paths: zod_1.z.array(zod_1.z.string()).optional().default([]),
app_slug: zod_1.z.string().optional().default("simulator-app"),
suspended_at: zod_1.z.nullable(zod_1.z.string()).optional().default(null),
suspended_by: zod_1.z.nullable(zod_1.z.string()).optional().default(null),
})
.transform((install) => {
install.id = faker_1.faker.number.int();
const host = "localhost:3300";
// api endpoint
install.access_tokens_url = `https://${host}/app/installations/1/access_tokens`;
install.repositories_url = `https://${host}/installation/repositories`;
// main site
install.html_url = `https://${host}/organizations/github/settings/installations/1`;
return install;
});
exports.githubRepositorySchema = zod_1.z
.object({
id: zod_1.z.number().optional(),
node_id: zod_1.z.string().optional(),
name: zod_1.z.string(),
description: zod_1.z
.string()
.optional()
.default("Generic repository description"),
owner: zod_1.z.string(),
full_name: zod_1.z.string().optional().default(""),
packages: zod_1.z.array(zod_1.z.string()).optional(),
pushed_at: zod_1.z
.string()
.optional()
.default(() => faker_1.faker.date.recent().toISOString()),
updated_at: zod_1.z
.string()
.optional()
.default(() => faker_1.faker.date.recent().toISOString()),
created_at: zod_1.z
.string()
.optional()
.default(() => faker_1.faker.date.recent().toISOString()),
url: zod_1.z.string().optional(),
html_url: zod_1.z.string().optional(),
archive_url: zod_1.z.string().optional(),
assignees_url: zod_1.z.string().optional(),
blobs_url: zod_1.z.string().optional(),
branches_url: zod_1.z.string().optional(),
collaborators_url: zod_1.z.string().optional(),
comments_url: zod_1.z.string().optional(),
commits_url: zod_1.z.string().optional(),
compare_url: zod_1.z.string().optional(),
contents_url: zod_1.z.string().optional(),
contributors_url: zod_1.z.string().optional(),
deployments_url: zod_1.z.string().optional(),
downloads_url: zod_1.z.string().optional(),
events_url: zod_1.z.string().optional(),
forks_url: zod_1.z.string().optional(),
git_commits_url: zod_1.z.string().optional(),
git_refs_url: zod_1.z.string().optional(),
git_tags_url: zod_1.z.string().optional(),
git_url: zod_1.z.string().optional(),
issue_comment_url: zod_1.z.string().optional(),
issue_events_url: zod_1.z.string().optional(),
issues_url: zod_1.z.string().optional(),
keys_url: zod_1.z.string().optional(),
labels_url: zod_1.z.string().optional(),
languages_url: zod_1.z.string().optional(),
merges_url: zod_1.z.string().optional(),
milestones_url: zod_1.z.string().optional(),
notifications_url: zod_1.z.string().optional(),
pulls_url: zod_1.z.string().optional(),
releases_url: zod_1.z.string().optional(),
ssh_url: zod_1.z.string().optional(),
stargazers_url: zod_1.z.string().optional(),
statuses_url: zod_1.z.string().optional(),
subscribers_url: zod_1.z.string().optional(),
subscription_url: zod_1.z.string().optional(),
tags_url: zod_1.z.string().optional(),
teams_url: zod_1.z.string().optional(),
trees_url: zod_1.z.string().optional(),
clone_url: zod_1.z.string().optional(),
mirror_url: zod_1.z.string().optional(),
hooks_url: zod_1.z.string().optional(),
svn_url: zod_1.z.string().optional(),
homepage: zod_1.z.string().optional(),
language: zod_1.z.nullable(zod_1.z.string()).optional().default(null),
default_branch: zod_1.z.string().optional().default("main"),
visibility: zod_1.z.enum(["public", "private"]).default("public"),
private: zod_1.z.boolean().optional().default(false),
license: zod_1.z.nullable(zod_1.z.record(zod_1.z.string(), zod_1.z.string())).default(null),
fork: zod_1.z.boolean().optional().default(false),
topics: zod_1.z.array(zod_1.z.string()).optional().default([]),
is_template: zod_1.z.boolean().optional().default(false),
has_issues: zod_1.z.boolean().optional().default(true),
has_projects: zod_1.z.boolean().optional().default(true),
has_wiki: zod_1.z.boolean().optional().default(true),
has_pages: zod_1.z.boolean().optional().default(false),
has_downloads: zod_1.z.boolean().optional().default(true),
has_discussions: zod_1.z.boolean().optional().default(false),
archived: zod_1.z.boolean().optional().default(false),
disabled: zod_1.z.boolean().optional().default(false),
forks_count: zod_1.z.number().optional().default(9001),
forks: zod_1.z.number().optional().default(9001),
stargazers_count: zod_1.z.number().optional().default(9001),
stargazers: zod_1.z.number().optional().default(9001),
watchers_count: zod_1.z.number().optional().default(9001),
watchers: zod_1.z.number().optional().default(9001),
size: zod_1.z.number().optional().default(9001),
open_issues_count: zod_1.z.number().optional().default(9001),
open_issues: zod_1.z.number().optional().default(9001),
permissions: githubEntityPermissionSchema,
security_and_analysis: zod_1.z
.object({
advanced_security: zod_1.z
.object({ status: zod_1.z.string() })
.optional()
.default({ status: "enabled" }),
secret_scanning: zod_1.z
.object({ status: zod_1.z.string() })
.optional()
.default({ status: "enabled" }),
secret_scanning_push_protection: zod_1.z
.object({ status: zod_1.z.string() })
.optional()
.default({ status: "enabled" }),
secret_scanning_non_provider_patterns: zod_1.z
.object({ status: zod_1.z.string() })
.optional()
.default({ status: "enabled" }),
})
.optional()
.default({
advanced_security: {
status: "enabled",
},
secret_scanning: {
status: "enabled",
},
secret_scanning_push_protection: {
status: "disabled",
},
secret_scanning_non_provider_patterns: {
status: "disabled",
},
}),
})
.transform((repo) => {
repo.id = faker_1.faker.number.int();
repo.node_id = repo.name;
repo.full_name = `${repo.owner}/${repo.name}`;
const host = "localhost:3300";
repo.url = `http://${host}/repos/octocat/Hello-World`;
repo.html_url = `http://${host}/repos/octocat/Hello-World`;
repo.archive_url = `http://${host}/repos/octocat/Hello-World/{archive_format}{/ref}`;
repo.assignees_url = `http://${host}/repos/octocat/Hello-World/assignees{/user}`;
repo.blobs_url = `http://${host}/repos/octocat/Hello-World/git/blobs{/sha}`;
repo.branches_url = `http://${host}/repos/octocat/Hello-World/branches{/branch}`;
repo.collaborators_url = `http://${host}/repos/octocat/Hello-World/collaborators{/collaborator}`;
repo.comments_url = `http://${host}/repos/octocat/Hello-World/comments{/number}`;
repo.commits_url = `http://${host}/repos/octocat/Hello-World/commits{/sha}`;
repo.compare_url = `http://${host}/repos/octocat/Hello-World/compare/{base}...{head}`;
repo.contents_url = `http://${host}/repos/octocat/Hello-World/contents/{+path}`;
repo.contributors_url = `http://${host}/repos/octocat/Hello-World/contributors`;
repo.deployments_url = `http://${host}/repos/octocat/Hello-World/deployments`;
repo.downloads_url = `http://${host}/repos/octocat/Hello-World/downloads`;
repo.events_url = `http://${host}/repos/octocat/Hello-World/events`;
repo.forks_url = `http://${host}/repos/octocat/Hello-World/forks`;
repo.git_commits_url = `http://${host}/repos/octocat/Hello-World/git/commits{/sha}`;
repo.git_refs_url = `http://${host}/repos/octocat/Hello-World/git/refs{/sha}`;
repo.git_tags_url = `http://${host}/repos/octocat/Hello-World/git/tags{/sha}`;
repo.git_url = `git:github.com/octocat/Hello-World.git`;
repo.issue_comment_url = `http://${host}/repos/octocat/Hello-World/issues/comments{/number}`;
repo.issue_events_url = `http://${host}/repos/octocat/Hello-World/issues/events{/number}`;
repo.issues_url = `http://${host}/repos/octocat/Hello-World/issues{/number}`;
repo.keys_url = `http://${host}/repos/octocat/Hello-World/keys{/key_id}`;
repo.labels_url = `http://${host}/repos/octocat/Hello-World/labels{/name}`;
repo.languages_url = `http://${host}/repos/octocat/Hello-World/languages`;
repo.merges_url = `http://${host}/repos/octocat/Hello-World/merges`;
repo.milestones_url = `http://${host}/repos/octocat/Hello-World/milestones{/number}`;
repo.notifications_url = `http://${host}/repos/octocat/Hello-World/notifications{?since,all,participating}`;
repo.pulls_url = `http://${host}/repos/octocat/Hello-World/pulls{/number}`;
repo.releases_url = `http://${host}/repos/octocat/Hello-World/releases{/id}`;
repo.ssh_url = `git@github.com:octocat/Hello-World.git`;
repo.stargazers_url = `http://${host}/repos/octocat/Hello-World/stargazers`;
repo.statuses_url = `http://${host}/repos/octocat/Hello-World/statuses/{sha}`;
repo.subscribers_url = `http://${host}/repos/octocat/Hello-World/subscribers`;
repo.subscription_url = `http://${host}/repos/octocat/Hello-World/subscription`;
repo.tags_url = `http://${host}/repos/octocat/Hello-World/tags`;
repo.teams_url = `http://${host}/repos/octocat/Hello-World/teams`;
repo.trees_url = `http://${host}/repos/octocat/Hello-World/git/trees{/sha}`;
repo.clone_url = `http://github.com/octocat/Hello-World.git`;
repo.mirror_url = `git:git.example.com/octocat/Hello-World`;
repo.hooks_url = `http://${host}/repos/octocat/Hello-World/hooks`;
repo.svn_url = `http://svn.github.com/octocat/Hello-World`;
repo.homepage = `http://${host}`;
repo.topics = ["octocat", "atom", "electron", "api"];
return repo;
});
exports.githubBranchSchema = zod_1.z.object({
name: zod_1.z.string().optional().default("main"),
commit: zod_1.z
.object({ sha: zod_1.z.string().optional(), url: zod_1.z.string().optional() })
.default({
sha: faker_1.faker.git.commitSha(),
// @ts-expect-error
url: `https://api.github.com/repos/octocat/Hello-World/commits/${this === null || this === void 0 ? void 0 : this.sha}`,
}),
protected: zod_1.z.boolean().optional().default(true),
protection: zod_1.z.any().optional(),
protection_url: zod_1.z
.string()
.optional()
.default("https://api.github.com/repos/octocat/hello-world/branches/master/protection"),
});
exports.githubOrganizationSchema = zod_1.z
.object({
id: zod_1.z.number().optional(),
login: zod_1.z.string(),
name: zod_1.z.string().optional(),
email: zod_1.z.string().optional(),
node_id: zod_1.z.string().optional(),
type: zod_1.z.enum(["User", "Organization"]).default("Organization"),
description: zod_1.z.string().optional().default("Generic org description"),
created_at: zod_1.z
.string()
.default(() => faker_1.faker.date.recent().toISOString())
.optional(),
teams: zod_1.z.union([zod_1.z.array(zod_1.z.string()), zod_1.z.undefined()]),
avatar_url: zod_1.z
.string()
.optional()
.default("https://github.com/images/error/octocat_happy.gif"),
gravatar_id: zod_1.z.string().optional().default(""),
site_admin: zod_1.z.boolean().optional().default(true),
url: zod_1.z.string().optional(),
html_url: zod_1.z.string().optional(),
followers_url: zod_1.z.string().optional(),
following_url: zod_1.z.string().optional(),
gists_url: zod_1.z.string().optional(),
starred_url: zod_1.z.string().optional(),
subscriptions_url: zod_1.z.string().optional(),
organizations_url: zod_1.z.string().optional(),
repos_url: zod_1.z.string().optional(),
events_url: zod_1.z.string().optional(),
received_events_url: zod_1.z.string().optional(),
hooks_url: zod_1.z.string().optional(),
issues_url: zod_1.z.string().optional(),
members_url: zod_1.z.string().optional(),
public_members_url: zod_1.z.string().optional(),
})
.transform((org) => {
org.id = faker_1.faker.number.int();
if (!(org === null || org === void 0 ? void 0 : org.name))
org.name = org.login;
if (!org.email)
org.email = faker_1.faker.internet.email({
firstName: "org",
lastName: org.login,
});
const host = "localhost:3300";
org.url = `https://${host}/orgs/${org.login}`;
org.html_url = `https://github.com/octocat`;
org.followers_url = `https://${host}/users/octocat/followers`;
org.following_url = `https://${host}/users/octocat/following{/other_user}`;
org.gists_url = `https://${host}/users/octocat/gists{/gist_id}`;
org.starred_url = `https://${host}/users/octocat/starred{/owner}{/repo}`;
org.subscriptions_url = `https://${host}/users/octocat/subscriptions`;
org.organizations_url = `https://${host}/users/octocat/orgs`;
org.repos_url = `https://${host}/users/octocat/repos`;
org.events_url = `https://${host}/users/octocat/events{/privacy}`;
org.received_events_url = `https://${host}/users/octocat/received_events`;
org.repos_url = `${org.url}/repos`;
org.events_url = `${org.url}/events`;
org.hooks_url = `${org.url}/hooks`;
org.issues_url = `${org.url}/issues`;
org.members_url = `${org.url}/members{/member}`;
org.public_members_url = `${org.url}/public_members{/member}`;
org.node_id = "MDQ6VXNlcjE=";
return org;
});
exports.githubBlobSchema = zod_1.z
.object({
id: zod_1.z.string().or(zod_1.z.number()).default(""),
content: zod_1.z.string().optional().default(faker_1.faker.lorem.paragraphs),
encoding: zod_1.z
.union([zod_1.z.literal("string"), zod_1.z.literal("base64")])
.default("string"),
owner: zod_1.z.string(),
repo: zod_1.z.string(),
// below we ensure that one of these is specified, but the other is then optional
path: zod_1.z.string(),
sha: zod_1.z.string(),
})
.transform((blob, ctx) => {
var _a, _b;
if (!blob.path && !blob.sha) {
ctx.addIssue({
code: zod_1.z.ZodIssueCode.custom,
message: "Specify the path or sha of the blob",
});
return zod_1.z.NEVER;
}
blob.id = `${blob.owner}/${blob.repo}/${(_b = (_a = blob === null || blob === void 0 ? void 0 : blob.path) !== null && _a !== void 0 ? _a : blob.sha) !== null && _b !== void 0 ? _b : "00000"}`;
return blob;
});
exports.gitubInitialStoreSchema = zod_1.z
.object({
users: zod_1.z.array(exports.githubUserSchema),
installations: zod_1.z.array(exports.githubAppInstallationSchema).optional().default([]),
organizations: zod_1.z.array(exports.githubOrganizationSchema),
repositories: zod_1.z.array(exports.githubRepositorySchema),
branches: zod_1.z.array(exports.githubBranchSchema),
blobs: zod_1.z.array(exports.githubBlobSchema),
})
.transform((initialStore) => {
initialStore.installations = initialStore.organizations.map((org) => {
return exports.githubAppInstallationSchema.parse({
account: org.login,
target_id: org.id,
target_type: org.type,
});
});
return initialStore;
});
const convertToObj = (arrayOfObjects, key = "id") => arrayOfObjects.reduce((final, obj) => {
final[obj[key]] = obj;
return final;
}, {});
exports.convertToObj = convertToObj;
const convertInitialStateToStoreState = (initialState) => {
if (!initialState)
return undefined;
// TODO try to make this generic?
const storeObject = {
users: (0, exports.convertToObj)(initialState.users, "login"),
installations: (0, exports.convertToObj)(initialState.installations, "id"),
repositories: (0, exports.convertToObj)(initialState.repositories, "name"),
branches: (0, exports.convertToObj)(initialState.branches, "name"),
organizations: (0, exports.convertToObj)(initialState.organizations, "login"),
blobs: (0, exports.convertToObj)(initialState.blobs),
};
return storeObject;
};
exports.convertInitialStateToStoreState = convertInitialStateToStoreState;
//# sourceMappingURL=entities.js.map
;