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.
chore: set up basic example of configuration loading from plugin
- Loading branch information
Showing
10 changed files
with
201 additions
and
56 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
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
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 |
---|---|---|
@@ -1,15 +1,59 @@ | ||
import { GitHubContext } from "../../github-context"; | ||
import { UbiquiBotConfig, getUbiquiBotConfig } from "../../ubiquibot-config"; | ||
import { generateHelpMenu } from "./help/help"; | ||
|
||
export const userCommands: IssueCommentCreatedCommand[] = [{ id: "/help", description: "List all available commands.", example: "/help", handler: generateHelpMenu }]; | ||
// fetch the ubiquibot-config.yml from the current repository, from the current organization, then merge (priority being the current repository.) | ||
// ubiquibot-config.yml is always meant to live at .github/ubiquibot-config.yml | ||
export async function issueCommentCreated(event: GitHubContext<"issue_comment.created">) { | ||
if (event.payload.comment.user.type === "Bot") { | ||
console.log("Skipping bot comment"); | ||
return null; | ||
const configuration = await getUbiquiBotConfig(event); | ||
const command = commentParser(event.payload.comment.body); | ||
if (!command) { | ||
return; | ||
} | ||
const commandHandler = userCommands.find((cmd) => cmd.id === command); | ||
if (!commandHandler) { | ||
return; | ||
} else { | ||
const result = await commandHandler.handler(event, configuration, event.payload.comment.body); | ||
if (typeof result === "string") { | ||
// Extract issue number and repository details from the event payload | ||
const issueNumber = event.payload.issue.number; | ||
const repo = event.payload.repository.name; | ||
const owner = event.payload.repository.owner.login; | ||
|
||
await event.octokit.issues.createComment({ | ||
owner: event.payload.repository.owner.login, | ||
repo: event.payload.repository.name, | ||
issue_number: event.payload.issue.number, | ||
body: "Hello from the worker!", | ||
}); | ||
// Create a new comment on the issue | ||
await event.octokit.rest.issues.createComment({ | ||
owner, | ||
repo, | ||
issue_number: issueNumber, | ||
body: result, | ||
}); | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
// Parses the comment body and figure out the command name a user wants | ||
function commentParser(body: string): null | string { | ||
const userCommandIds = userCommands.map((cmd) => cmd.id); | ||
const regex = new RegExp(`^(${userCommandIds.join("|")})\\b`); // Regex pattern to match any command at the beginning of the body | ||
const matches = regex.exec(body); | ||
if (matches) { | ||
const command = matches[0] as string; | ||
if (userCommandIds.includes(command)) { | ||
return command; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
type IssueCommentCreatedCommand = { | ||
id: string; | ||
description: string; | ||
example: string; | ||
handler: IssueCommentCreatedHandler; | ||
}; | ||
|
||
type IssueCommentCreatedHandler = (context: GitHubContext<"issue_comment.created">, configuration: UbiquiBotConfig, body: string) => Promise<string | 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { GitHubContext } from "../../../github-context"; | ||
import { UbiquiBotConfig } from "../../../ubiquibot-config"; | ||
import { userCommands } from "../created"; | ||
|
||
export async function generateHelpMenu(context: GitHubContext<"issue_comment.created">, configuration: UbiquiBotConfig) { | ||
const disabledCommands = configuration.disabledCommands; | ||
const isStartDisabled = configuration.disabledCommands.some((command) => command === "start"); | ||
let helpMenu = "### Available Commands\n\n| Command | Description | Example |\n| --- | --- | --- |\n"; | ||
// const commands = userCommands(configuration.miscellaneous.registerWalletWithVerification); | ||
|
||
userCommands | ||
.filter((command) => !disabledCommands.includes(command.id)) | ||
.map( | ||
(command) => | ||
(helpMenu += `| \`${command.id}\` | ${breakSentences(command.description) || ""} | ${(command.example && breakLongString(command.example)) || ""} |\n`) // add to help menu | ||
); | ||
|
||
if (isStartDisabled) { | ||
helpMenu += "\n\n**To assign yourself to an issue, please open a draft pull request that is linked to it.**"; | ||
} | ||
return helpMenu; | ||
} | ||
|
||
function breakLongString(str: string, maxLen = 24) { | ||
const newStr = [] as string[]; | ||
let spaceIndex = str.indexOf(" ", maxLen); // Find the first space after maxLen | ||
|
||
while (str.length > maxLen && spaceIndex !== -1) { | ||
newStr.push(str.slice(0, spaceIndex)); | ||
str = str.slice(spaceIndex + 1); | ||
spaceIndex = str.indexOf(" ", maxLen); | ||
} | ||
|
||
newStr.push(str); // Push the remaining part of the string | ||
|
||
return newStr.join("<br>"); | ||
} | ||
|
||
function breakSentences(str: string) { | ||
const sentences = str.endsWith(".") ? str.slice(0, -1).split(". ") : str.split(". "); | ||
if (sentences.length <= 1) { | ||
return str; | ||
} | ||
return sentences.join(".<br><br>"); | ||
} |
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,76 @@ | ||
import yaml from "js-yaml"; | ||
import { GitHubContext } from "./github-context"; | ||
|
||
export type UbiquiBotConfig = { | ||
keys: { | ||
evmPrivateEncrypted: string; | ||
openAi: string; | ||
}; | ||
features: { | ||
assistivePricing: boolean; | ||
publicAccessControl: unknown; | ||
}; | ||
payments: { | ||
evmNetworkId: 1 | 100; | ||
basePriceMultiplier: number; | ||
issueCreatorMultiplier: number; | ||
maxPermitPrice: number; | ||
}; | ||
timers: { | ||
reviewDelayTolerance: string; | ||
taskStaleTimeoutDuration: string; | ||
taskFollowUpDuration: string; | ||
taskDisqualifyDuration: string; | ||
}; | ||
miscellaneous: { | ||
promotionComment: string; | ||
maxConcurrentTasks: number; | ||
registerWalletWithVerification: boolean; | ||
}; | ||
disabledCommands: string[]; | ||
incentives: { comment: unknown }; | ||
labels: { time: string[]; priority: string[] }; | ||
}; | ||
|
||
export async function getUbiquiBotConfig(event: GitHubContext<"issue_comment.created">): Promise<UbiquiBotConfig> { | ||
const responses = { | ||
repositoryConfig: null as UbiquiBotConfig | null, | ||
organizationConfig: null as UbiquiBotConfig | null, | ||
}; | ||
|
||
try { | ||
responses.repositoryConfig = await fetchConfig(event, event.payload.repository.name); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
|
||
try { | ||
responses.organizationConfig = await fetchConfig(event, `.ubiquibot-config`); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
|
||
// Merge the two configs | ||
return { | ||
...(responses.organizationConfig || {}), | ||
...(responses.repositoryConfig || {}), | ||
} as UbiquiBotConfig; | ||
} | ||
|
||
async function fetchConfig(event: GitHubContext<"issue_comment.created">, repo: string): Promise<UbiquiBotConfig | null> { | ||
const response = await event.octokit.rest.repos.getContent({ | ||
owner: event.payload.repository.owner.login, | ||
repo, | ||
path: ".github/ubiquibot-config.yml", | ||
}); | ||
|
||
// Check if the response data is a file and has a content property | ||
if ("content" in response.data && typeof response.data.content === "string") { | ||
// Convert the content from Base64 to string and parse the YAML content | ||
const content = atob(response.data.content).toString(); | ||
return yaml.load(content) as UbiquiBotConfig; | ||
} else { | ||
return null; | ||
// throw new Error("Expected file content, but got something else"); | ||
} | ||
} |
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