generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from gentlementlegen/feat/help-command-manifest
feat: help command manifest
- Loading branch information
Showing
13 changed files
with
198 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,4 @@ jobs: | |
- name: Build & Run test suite | ||
run: | | ||
bun i | ||
bun test --coverage | ||
bun jest:test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"preset": "ts-jest", | ||
"testEnvironment": "node", | ||
"roots": ["./tests"], | ||
"coveragePathIgnorePatterns": ["node_modules", "mocks"], | ||
"collectCoverage": true, | ||
"coverageReporters": ["json", "lcov", "text", "clover", "json-summary"], | ||
"reporters": ["default", "jest-junit", "jest-md-dashboard"], | ||
"coverageDirectory": "coverage", | ||
"setupFiles": ["dotenv/config"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { getConfig } from "../utils/config"; | ||
import { GithubPlugin, isGithubPlugin } from "../types/plugin-configuration"; | ||
import { GitHubContext } from "../github-context"; | ||
import { Manifest, manifestSchema, manifestValidator } from "../../types/manifest"; | ||
import { Value } from "@sinclair/typebox/value"; | ||
import { Buffer } from "node:buffer"; | ||
|
||
async function parseCommandsFromManifest(context: GitHubContext<"issue_comment.created">, plugin: string | GithubPlugin) { | ||
const commands: string[] = []; | ||
const manifest = await (isGithubPlugin(plugin) ? fetchActionManifest(context, plugin) : fetchWorkerManifest(plugin)); | ||
if (manifest) { | ||
Value.Default(manifestSchema, manifest); | ||
const errors = manifestValidator.testReturningErrors(manifest); | ||
if (errors !== null) { | ||
console.error(`Failed to load the manifest for ${JSON.stringify(plugin)}`); | ||
for (const error of errors) { | ||
console.error(error); | ||
} | ||
} else { | ||
if (manifest?.commands) { | ||
for (const [key, value] of Object.entries(manifest.commands)) { | ||
commands.push(`| \`/${getContent(key)}\` | ${getContent(value.description)} | \`${getContent(value["ubiquity:example"])}\` |`); | ||
} | ||
} | ||
} | ||
} | ||
return commands; | ||
} | ||
|
||
export async function postHelpCommand(context: GitHubContext<"issue_comment.created">) { | ||
const comments = [ | ||
"### Available Commands\n\n", | ||
"| Command | Description | Example |", | ||
"|---|---|---|", | ||
"| `/help` | List all available commands. | `/help` |", | ||
]; | ||
const commands: string[] = []; | ||
const configuration = await getConfig(context); | ||
for (const pluginArray of Object.values(configuration.plugins)) { | ||
for (const pluginElement of pluginArray) { | ||
const { plugin } = pluginElement.uses[0]; | ||
commands.push(...(await parseCommandsFromManifest(context, plugin))); | ||
} | ||
} | ||
await context.octokit.issues.createComment({ | ||
body: comments.concat(commands.sort()).join("\n"), | ||
issue_number: context.payload.issue.number, | ||
owner: context.payload.repository.owner.login, | ||
repo: context.payload.repository.name, | ||
}); | ||
} | ||
|
||
/** | ||
* Ensures that passed content does not break MD display within the table. | ||
*/ | ||
function getContent(content: string | undefined) { | ||
return content ? content.replace("|", "\\|") : "-"; | ||
} | ||
|
||
async function fetchActionManifest(context: GitHubContext<"issue_comment.created">, { owner, repo }: GithubPlugin): Promise<Manifest | null> { | ||
try { | ||
const { data } = await context.octokit.repos.getContent({ | ||
owner, | ||
repo, | ||
path: "manifest.json", | ||
}); | ||
if ("content" in data) { | ||
const content = Buffer.from(data.content, "base64").toString(); | ||
return JSON.parse(content); | ||
} | ||
} catch (e) { | ||
console.warn(`Could not find a manifest for ${owner}/${repo}: ${e}`); | ||
} | ||
return null; | ||
} | ||
|
||
async function fetchWorkerManifest(url: string): Promise<Manifest | null> { | ||
const manifestUrl = `${url}/manifest.json`; | ||
try { | ||
const result = await fetch(manifestUrl); | ||
return (await result.json()) as Manifest; | ||
} catch (e) { | ||
console.warn(`Could not find a manifest for ${manifestUrl}: ${e}`); | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,9 @@ | ||
import { GitHubContext } from "../github-context"; | ||
import { getConfig } from "../utils/config"; | ||
import { postHelpCommand } from "./help-command"; | ||
|
||
export default async function issueCommentCreated(context: GitHubContext<"issue_comment.created">) { | ||
const body = context.payload.comment.body.trim(); | ||
if (/^\/help$/.test(body)) { | ||
const comments = [ | ||
"### Available Commands\n\n", | ||
"| Command | Description | Example |", | ||
"|---|---|---|", | ||
"| `/help` | List all available commands. | `/help` |", | ||
]; | ||
const configuration = await getConfig(context); | ||
for (const pluginArray of Object.values(configuration.plugins)) { | ||
for (const plugin of pluginArray) { | ||
// Only show plugins that have commands available for the user | ||
if (plugin.command) { | ||
comments.push(`| \`${getContent(plugin.command)}\` | ${getContent(plugin.description)} | \`${getContent(plugin.example)}\` |`); | ||
} | ||
} | ||
} | ||
await context.octokit.issues.createComment({ | ||
body: comments.join("\n"), | ||
issue_number: context.payload.issue.number, | ||
owner: context.payload.repository.owner.login, | ||
repo: context.payload.repository.name, | ||
}); | ||
await postHelpCommand(context); | ||
} | ||
} | ||
|
||
/** | ||
* Ensures that passed content does not break MD display within the table. | ||
*/ | ||
function getContent(content: string | undefined) { | ||
return content ? content.replace("|", "\\|") : "-"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { type Static, Type as T } from "@sinclair/typebox"; | ||
import { StandardValidator } from "typebox-validators"; | ||
|
||
export const commandSchema = T.Object({ | ||
description: T.String({ minLength: 1 }), | ||
"ubiquity:example": T.String({ minLength: 1 }), | ||
}); | ||
|
||
export const manifestSchema = T.Object({ | ||
name: T.String({ minLength: 1 }), | ||
description: T.String({ minLength: 1 }), | ||
commands: T.Record(T.String(), commandSchema), | ||
}); | ||
|
||
export const manifestValidator = new StandardValidator(manifestSchema); | ||
|
||
export type Manifest = Static<typeof manifestSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.