Skip to content

Commit

Permalink
chore: eslint, cspell
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Jul 27, 2024
1 parent 9a0037b commit f34b8b5
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 202 deletions.
19 changes: 18 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@
"ignorePaths": ["**/*.json", "**/*.css", "node_modules", "**/*.log", "./src/adapters/supabase/**/**.ts"],
"useGitignore": true,
"language": "en",
"words": ["Nektos", "dataurl", "devpool", "outdir", "servedir", "Supabase", "SUPABASE", "typebox", "ubiquibot", "Smee"],
"words": [
"Nektos",
"dataurl",
"devpool",
"outdir",
"servedir",
"Supabase",
"SUPABASE",
"typebox",
"ubiquibot",
"Smee",
"typeguards",
"mswjs",
"Typeguards",
"sonarjs",
"knip",
"mischeck"
],
"dictionaries": ["typescript", "node", "software-terms"],
"import": ["@cspell/dict-typescript/cspell-ext.json", "@cspell/dict-node/cspell-ext.json", "@cspell/dict-software-terms"],
"ignoreRegExpList": ["[0-9a-fA-F]{6}"]
Expand Down
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
```yml
plugins:
"issue_comment.created":
- name: hello-world
id: hello-world
type: github
description: "A simple hello world plugin" # small description of what the plugin does
command: "\/hello" # if you are creating a plugin with a slash command
example: "/hello" # how to invoke the slash command
uses:
# - plugin: <plugin-org/owner>/<plugin-repo-name>:compute.yml@development
- plugin: http://localhost:4000
with:
# Define configurables here and the kernel will pass these to the plugin.
configurableResponse: "Hello, is it me you are looking for?"
- name: hello-world
id: hello-world
type: github
description: "A simple hello world plugin" # small description of what the plugin does
command: "\/hello" # if you are creating a plugin with a slash command
example: "/hello" # how to invoke the slash command
uses:
# - plugin: <plugin-org/owner>/<plugin-repo-name>:compute.yml@development
- plugin: http://localhost:4000
with:
# Define configurable items here and the kernel will pass these to the plugin.
configurableResponse: "Hello, is it me you are looking for?"
```
###### At this stage, your plugin will fire on your defined events with the required settings passed in from the kernel. You can now start writing your plugin's logic.
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ts-template",
"description": "ts-template for Ubiquibot plugins.",
"ubiquity:listeners": [ "issue_comment.created" ],
"ubiquity:listeners": ["issue_comment.created"],
"commands": {
"command1": {
"ubiquity:example": "/command1 argument",
Expand Down
81 changes: 43 additions & 38 deletions src/handlers/hello-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,58 @@ import { Context } from "../types";

/**
* NOTICE: Remove this file or use it as a template for your own plugins.
*
*
* This encapsulates the logic for a plugin if the only thing it does is say "Hello, world!".
*
*
* Try it out by running your local kernel worker and running the `yarn worker` command.
* Comment on an issue in a repository where your GitHub App is installed and see the magic happen!
*
*
* Logger examples are provided to show how to log different types of data.
*/
export async function helloWorld(context: Context) {
const { logger, payload, octokit, config: { configurableResponse } } = context;
const {
logger,
payload,
octokit,
config: { configurableResponse },
} = context;

const sender = payload.comment.user?.login;
const repo = payload.repository.name;
const issueNumber = payload.issue.number;
const owner = payload.repository.owner.login;
const body = payload.comment.body;
const sender = payload.comment.user?.login;
const repo = payload.repository.name;
const issueNumber = payload.issue.number;
const owner = payload.repository.owner.login;
const body = payload.comment.body;

if (!body.match(/hello/i)) {
logger.error(`Invalid use of slash command, use "/hello".`, { body });
return;
}
if (!body.match(/hello/i)) {
logger.error(`Invalid use of slash command, use "/hello".`, { body });
return;
}

logger.info("Hello, world!");
logger.debug(`Executing helloWorld:`, { sender, repo, issueNumber, owner });
logger.info("Hello, world!");
logger.debug(`Executing helloWorld:`, { sender, repo, issueNumber, owner });

try {
await octokit.issues.createComment({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.issue.number,
body: configurableResponse
});
} catch (error) {
/**
* logger.fatal should not be used in 9/10 cases. Use logger.error instead.
*
* Below are examples of passing error objects to the logger, only one is needed.
*/
if (error instanceof Error) {
logger.error(`Error creating comment:`, { error: error, stack: error.stack });
throw error;
} else {
logger.error(`Error creating comment:`, { err: error, error: new Error() });
throw error;
}
try {
await octokit.issues.createComment({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.issue.number,
body: configurableResponse,
});
} catch (error) {
/**
* logger.fatal should not be used in 9/10 cases. Use logger.error instead.
*
* Below are examples of passing error objects to the logger, only one is needed.
*/
if (error instanceof Error) {
logger.error(`Error creating comment:`, { error: error, stack: error.stack });
throw error;
} else {
logger.error(`Error creating comment:`, { err: error, error: new Error() });
throw error;
}
}

logger.ok(`Successfully created comment!`);
logger.verbose(`Exiting helloWorld`);
}
logger.ok(`Successfully created comment!`);
logger.verbose(`Exiting helloWorld`);
}
6 changes: 3 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Octokit } from "@octokit/rest";
import { createClient } from "@supabase/supabase-js";
// import { createClient } from "@supabase/supabase-js";
import { createAdapters } from "./adapters";
import { Env, PluginInputs } from "./types";
import { Context } from "./types";
Expand Down Expand Up @@ -38,8 +38,8 @@ export async function plugin(inputs: PluginInputs, env: Env) {
};

// consider non-database storage solutions unless necessary
// TODO: deprecate adapters/supabase from context.
// TODO: deprecate adapters/supabase from context.
// context.adapters = createAdapters(supabase, context);

return runPlugin(context);
}
}
9 changes: 6 additions & 3 deletions src/types/plugin-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ export interface PluginInputs<T extends SupportedEventsU = SupportedEventsU, TU
* The kernel will extract those and pass them to the plugin,
* which are built into the context object from setup().
*/
export const pluginSettingsSchema = T.Object({
configurableResponse: T.String()
}, { default: { configurableResponse: "Hello, world!" } });
export const pluginSettingsSchema = T.Object(
{
configurableResponse: T.String(),
},
{ default: { configurableResponse: "Hello, world!" } }
);

export const pluginSettingsValidator = new StandardValidator(pluginSettingsSchema);

Expand Down
4 changes: 2 additions & 2 deletions src/types/typeguards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import { Context } from "./context";
* Restricts the scope of `context` to the `issue_comment.created` payload.
*/
export function isIssueCommentEvent(context: Context): context is Context<"issue_comment.created"> {
return context.eventName === "issue_comment.created";
}
return context.eventName === "issue_comment.created";
}
2 changes: 1 addition & 1 deletion tests/__mocks__/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ export const db = factory({
id: Number,
},
},
});
});
13 changes: 7 additions & 6 deletions tests/__mocks__/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { http, HttpResponse } from "msw";
import { db } from "./db";
import issueTemplate from "./issue-template";
import { get } from "http";
/**
* Intercepts the routes and returns a custom payload
*/
Expand All @@ -15,8 +14,10 @@ export const handlers = [
HttpResponse.json(db.issue.findMany({ where: { owner: { equals: owner as string }, repo: { equals: repo as string } } }))
),
// get issue
http.get("https://api.github.com/repos/:owner/:repo/issues/:issue_number", ({ params: { owner, repo, issue_number } }) =>
HttpResponse.json(db.issue.findFirst({ where: { owner: { equals: owner as string }, repo: { equals: repo as string }, number: { equals: Number(issue_number) } } }))
http.get("https://api.github.com/repos/:owner/:repo/issues/:issue_number", ({ params: { owner, repo, issue_number: issueNumber } }) =>
HttpResponse.json(
db.issue.findFirst({ where: { owner: { equals: owner as string }, repo: { equals: repo as string }, number: { equals: Number(issueNumber) } } })
)
),
// get user
http.get("https://api.github.com/users/:username", ({ params: { username } }) =>
Expand All @@ -38,10 +39,10 @@ export const handlers = [
return HttpResponse.json(newItem);
}),
// create comment
http.post("https://api.github.com/repos/:owner/:repo/issues/:issue_number/comments", async ({ params: { owner, repo, issue_number }, request }) => {
http.post("https://api.github.com/repos/:owner/:repo/issues/:issue_number/comments", async ({ params: { issue_number: issueNumber }, request }) => {
const { body } = await getValue(request.body);
const id = db.issueComments.count() + 1;
const newItem = { id, body, issue_number: Number(issue_number), user: db.users.getAll()[0] };
const newItem = { id, body, issue_number: Number(issueNumber), user: db.users.getAll()[0] };
db.issueComments.create(newItem);
return HttpResponse.json(newItem);
}),
Expand All @@ -60,4 +61,4 @@ async function getValue(body: ReadableStream<Uint8Array> | null) {
}
}
}
}
}
104 changes: 52 additions & 52 deletions tests/__mocks__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,70 @@ import usersGet from "./users-get.json";

/**
* Helper function to setup tests.
*
*
* This function populates the mock database with the external API
* data you'd expect to find in a real-world scenario.
*
*
* Here is where you create issues, commits, pull requests, etc.
*/
export async function setupTests() {
for (const item of usersGet) {
db.users.create(item);
}
for (const item of usersGet) {
db.users.create(item);
}

db.repo.create({
id: 1,
name: STRINGS.TEST_REPO,
owner: {
login: STRINGS.USER_1,
id: 1,
},
issues: [],
});
db.repo.create({
id: 1,
name: STRINGS.TEST_REPO,
owner: {
login: STRINGS.USER_1,
id: 1,
},
issues: [],
});

db.issue.create({
...issueTemplate,
});
db.issue.create({
...issueTemplate,
});

db.issue.create({
...issueTemplate,
id: 2,
number: 2,
labels: [],
});
db.issue.create({
...issueTemplate,
id: 2,
number: 2,
labels: [],
});

createComment("/Hello", 1);
createComment("/Hello", 1);
}

export function createComment(comment: string, commentId: number) {
const isComment = db.issueComments.findFirst({
where: {
id: {
equals: commentId,
},
const isComment = db.issueComments.findFirst({
where: {
id: {
equals: commentId,
},
},
});

if (isComment) {
db.issueComments.update({
where: {
id: {
equals: commentId,
},
},
data: {
body: comment,
},
});

if (isComment) {
db.issueComments.update({
where: {
id: {
equals: commentId,
},
},
data: {
body: comment,
},
});
} else {
db.issueComments.create({
id: commentId,
body: comment,
issue_number: 1,
user: {
login: STRINGS.USER_1,
id: 1,
},
});
}
} else {
db.issueComments.create({
id: commentId,
body: comment,
issue_number: 1,
user: {
login: STRINGS.USER_1,
id: 1,
},
});
}
}
Loading

0 comments on commit f34b8b5

Please sign in to comment.