generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 20
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 #169 from gentlementlegen/feat/improvements
feat: improvements
- Loading branch information
Showing
11 changed files
with
102 additions
and
32 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Context } from "./context"; | ||
import { LogReturn } from "@ubiquity-os/ubiquity-os-logger"; | ||
import { sanitizeMetadata } from "./util"; | ||
|
||
const HEADER_NAME = "Ubiquity"; | ||
|
||
/** | ||
* Posts a comment on a GitHub issue if the issue exists in the context payload, embedding structured metadata to it. | ||
*/ | ||
export async function postComment(context: Context, message: LogReturn) { | ||
if ("issue" in context.payload && context.payload.repository?.owner?.login) { | ||
const metadata = createStructuredMetadata(message.metadata?.name, message); | ||
await context.octokit.rest.issues.createComment({ | ||
owner: context.payload.repository.owner.login, | ||
repo: context.payload.repository.name, | ||
issue_number: context.payload.issue.number, | ||
body: [message.logMessage.diff, metadata].join("\n"), | ||
}); | ||
} else { | ||
context.logger.info("Cannot post comment because issue is not found in the payload"); | ||
} | ||
} | ||
|
||
function createStructuredMetadata(className: string | undefined, logReturn: LogReturn) { | ||
const logMessage = logReturn.logMessage; | ||
const metadata = logReturn.metadata; | ||
|
||
const jsonPretty = sanitizeMetadata(metadata); | ||
const stack = logReturn.metadata?.stack; | ||
const stackLine = (Array.isArray(stack) ? stack.join("\n") : stack)?.split("\n")[2] ?? ""; | ||
const caller = stackLine.match(/at (\S+)/)?.[1] ?? ""; | ||
const ubiquityMetadataHeader = `<!-- ${HEADER_NAME} - ${className} - ${caller} - ${metadata?.revision}`; | ||
|
||
let metadataSerialized: string; | ||
const metadataSerializedVisible = ["```json", jsonPretty, "```"].join("\n"); | ||
const metadataSerializedHidden = [ubiquityMetadataHeader, jsonPretty, "-->"].join("\n"); | ||
|
||
if (logMessage?.type === "fatal") { | ||
// if the log message is fatal, then we want to show the metadata | ||
metadataSerialized = [metadataSerializedVisible, metadataSerializedHidden].join("\n"); | ||
} else { | ||
// otherwise we want to hide it | ||
metadataSerialized = metadataSerializedHidden; | ||
} | ||
|
||
// Add carriage returns to avoid any formatting issue | ||
return `\n${metadataSerialized}\n`; | ||
} |
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,4 +1,6 @@ | ||
export { createPlugin } from "./server"; | ||
export { createActionsPlugin } from "./actions"; | ||
export { postComment } from "./comment"; | ||
export type { Context } from "./context"; | ||
export * from "./constants"; | ||
export type { Manifest } from "../types/manifest"; |
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