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 #114 from ubiquity/development
Merge development into main
- Loading branch information
Showing
13 changed files
with
503 additions
and
95 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
name: Sync branch to template | ||
|
||
on: | ||
workflow_dispatch: | ||
schedule: | ||
- cron: '14 0 1 * *' | ||
|
||
jobs: | ||
sync: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: write | ||
pull-requests: write | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Get GitHub App token | ||
uses: tibdex/[email protected] | ||
id: get_installation_token | ||
with: | ||
app_id: ${{ secrets.APP_ID }} | ||
private_key: ${{ secrets.APP_PRIVATE_KEY }} | ||
|
||
- name: Sync branch to template | ||
env: | ||
GH_TOKEN: ${{ steps.get_installation_token.outputs.token }} | ||
IGNORE_FILES: "README.md another-file.txt" | ||
run: | | ||
branch_name=$(git rev-parse --abbrev-ref HEAD) | ||
original_remote=$(git remote get-url origin) | ||
pr_branch="sync-template/${branch_name}" | ||
git config --global user.email "github-actions[bot]@users.noreply.github.com" | ||
git config --global user.name "github-actions[bot]" | ||
git checkout -b "$pr_branch" | ||
git clone https://github.com/ubiquity/ts-template | ||
for file in $IGNORE_FILES; do | ||
rm -rf "ts-template/$file" | ||
done | ||
cp -rf ts-template/* . | ||
rm -rf ts-template/ | ||
git add . | ||
git commit -m "chore: sync template" | ||
git push "$original_remote" "$pr_branch" | ||
gh pr create --title "Sync branch to template" --body "This pull request merges changes from the template repository." --head "$pr_branch" --base "$branch_name" | ||
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import * as core from "@actions/core"; | ||
import * as github from "@actions/github"; | ||
import { Context } from "./context"; | ||
import { customOctokit } from "./octokit"; | ||
import { EmitterWebhookEventName as WebhookEventName } from "@octokit/webhooks"; | ||
import { Logs, LogLevel, LOG_LEVEL, LogReturn } from "@ubiquity-dao/ubiquibot-logger"; | ||
import { config } from "dotenv"; | ||
import { Type as T, TAnySchema } from "@sinclair/typebox"; | ||
import { Value } from "@sinclair/typebox/value"; | ||
import { sanitizeMetadata } from "./util"; | ||
|
||
config(); | ||
|
||
interface Options { | ||
logLevel?: LogLevel; | ||
postCommentOnError?: boolean; | ||
settingsSchema?: TAnySchema; | ||
envSchema?: TAnySchema; | ||
} | ||
|
||
const inputSchema = T.Object({ | ||
stateId: T.String(), | ||
eventName: T.String(), | ||
eventPayload: T.String(), | ||
authToken: T.String(), | ||
settings: T.String(), | ||
ref: T.String(), | ||
}); | ||
|
||
export async function createActionsPlugin<TConfig = unknown, TEnv = unknown, TSupportedEvents extends WebhookEventName = WebhookEventName>( | ||
handler: (context: Context<TConfig, TEnv, TSupportedEvents>) => Promise<Record<string, unknown> | undefined>, | ||
options?: Options | ||
) { | ||
const pluginOptions = { | ||
logLevel: options?.logLevel || LOG_LEVEL.INFO, | ||
postCommentOnError: options?.postCommentOnError || true, | ||
settingsSchema: options?.settingsSchema, | ||
envSchema: options?.envSchema, | ||
}; | ||
|
||
const inputs = Value.Decode(inputSchema, github.context.payload.inputs); | ||
|
||
let config: TConfig; | ||
if (pluginOptions.settingsSchema) { | ||
config = Value.Decode(pluginOptions.settingsSchema, JSON.parse(inputs.settings)); | ||
} else { | ||
config = JSON.parse(inputs.settings) as TConfig; | ||
} | ||
|
||
let env: TEnv; | ||
if (pluginOptions.envSchema) { | ||
env = Value.Decode(pluginOptions.envSchema, process.env); | ||
} else { | ||
env = process.env as TEnv; | ||
} | ||
|
||
const context: Context<TConfig, TEnv, TSupportedEvents> = { | ||
eventName: inputs.eventName as TSupportedEvents, | ||
payload: JSON.parse(inputs.eventPayload), | ||
octokit: new customOctokit({ auth: inputs.authToken }), | ||
config: config, | ||
env: env, | ||
logger: new Logs(pluginOptions.logLevel), | ||
}; | ||
|
||
try { | ||
const result = await handler(context); | ||
core.setOutput("result", result); | ||
await returnDataToKernel(inputs.authToken, inputs.stateId, result); | ||
} catch (error) { | ||
console.error(error); | ||
|
||
let loggerError: LogReturn | null; | ||
if (error instanceof Error) { | ||
core.setFailed(error); | ||
loggerError = context.logger.error(`Error: ${error}`, { error: error }); | ||
} else if (error instanceof LogReturn) { | ||
core.setFailed(error.logMessage.raw); | ||
loggerError = error; | ||
} else { | ||
core.setFailed(`Error: ${error}`); | ||
loggerError = context.logger.error(`Error: ${error}`); | ||
} | ||
|
||
if (pluginOptions.postCommentOnError && loggerError) { | ||
await postComment(context, loggerError); | ||
} | ||
} | ||
} | ||
|
||
async function postComment(context: Context, error: LogReturn) { | ||
if ("issue" in context.payload && context.payload.repository?.owner?.login) { | ||
await context.octokit.rest.issues.createComment({ | ||
owner: context.payload.repository.owner.login, | ||
repo: context.payload.repository.name, | ||
issue_number: context.payload.issue.number, | ||
body: `${error.logMessage.diff}\n<!--\n${getGithubWorkflowRunUrl()}\n${sanitizeMetadata(error.metadata)}\n-->`, | ||
}); | ||
} else { | ||
context.logger.info("Cannot post comment because issue is not found in the payload"); | ||
} | ||
} | ||
|
||
function getGithubWorkflowRunUrl() { | ||
return `${github.context.payload.repository?.html_url}/actions/runs/${github.context.runId}`; | ||
} | ||
|
||
async function returnDataToKernel(repoToken: string, stateId: string, output: object | undefined) { | ||
const octokit = new customOctokit({ auth: repoToken }); | ||
await octokit.rest.repos.createDispatchEvent({ | ||
owner: github.context.repo.owner, | ||
repo: github.context.repo.repo, | ||
event_type: "return_data_to_ubiquibot_kernel", | ||
client_payload: { | ||
state_id: stateId, | ||
output: output ? JSON.stringify(output) : 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
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,2 +1,4 @@ | ||
export { createPlugin } from "./server"; | ||
export { createActionsPlugin } from "./actions"; | ||
export type { Context } from "./context"; | ||
export * from "./constants"; |
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,5 @@ | ||
import { LogReturn } from "@ubiquity-dao/ubiquibot-logger"; | ||
|
||
export function sanitizeMetadata(obj: LogReturn["metadata"]): string { | ||
return JSON.stringify(obj, null, 2).replace(/</g, "<").replace(/>/g, ">").replace(/--/g, "--"); | ||
} |
Oops, something went wrong.