forked from ubiquity-os-marketplace/command-start-stop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: user self assign message display
- Loading branch information
1 parent
30ca585
commit c8a018d
Showing
8 changed files
with
64 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Context, Env, SupportedEventsU } from "../types"; | ||
import { userSelfAssign, userStartStop } from "./user-start-stop"; | ||
|
||
export interface Result { | ||
status: "ok" | "failed" | "skipped"; | ||
content?: string; | ||
reason?: string; | ||
} | ||
|
||
const callbacks: { [k in SupportedEventsU]: (context: Context, env: Env) => Result | Promise<Result> } = { | ||
"issue.assigned": userSelfAssign, | ||
"issue_comment.created": userStartStop, | ||
}; | ||
|
||
export function proxyCallbacks({ logger }: Context) { | ||
return new Proxy(callbacks, { | ||
get(target, prop: SupportedEventsU) { | ||
if (!(prop in target)) { | ||
logger.error(`${prop} is not supported, skipping.`); | ||
return async () => ({ status: "skipped", reason: "unsupported_event" }); | ||
} | ||
return target[prop].bind(target); | ||
}, | ||
}); | ||
} |
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,19 +1,42 @@ | ||
import { Context } from "../types"; | ||
import { addCommentToIssue } from "../utils/issue"; | ||
import { calculateDurations } from "../utils/shared"; | ||
import { Result } from "./proxy"; | ||
import { options } from "./shared/generate-assignment-comment"; | ||
import { start } from "./shared/start"; | ||
import { stop } from "./shared/stop"; | ||
|
||
export async function userStartStop(context: Context): Promise<{ output: string | null }> { | ||
function getUser(payload: Context["payload"]) { | ||
const { comment, sender } = payload; | ||
return comment.user?.login ? { login: comment.user.login, id: comment.user.id } : { login: sender.login, id: sender.id }; | ||
} | ||
|
||
export async function userStartStop(context: Context): Promise<Result> { | ||
const { payload } = context; | ||
const { issue, comment, sender, repository } = payload; | ||
const { issue, comment, repository } = payload; | ||
const slashCommand = comment.body.split(" ")[0].replace("/", ""); | ||
|
||
const user = comment.user?.login ? { login: comment.user.login, id: comment.user.id } : { login: sender.login, id: sender.id }; | ||
const user = getUser(context.payload); | ||
|
||
if (slashCommand === "stop") { | ||
return await stop(context, issue, user, repository); | ||
} else if (slashCommand === "start") { | ||
return await start(context, issue, user); | ||
} | ||
|
||
return { output: null }; | ||
return { status: "skipped" }; | ||
} | ||
|
||
export async function userSelfAssign(context: Context): Promise<Result> { | ||
const { payload } = context; | ||
const { issue } = payload; | ||
const startTime = new Date().getTime(); | ||
const duration: number = calculateDurations(issue.labels).shift() ?? 0; | ||
const endTime = new Date(startTime + duration * 1000); | ||
const deadline = endTime.toLocaleString("en-US", options); | ||
|
||
const users = issue.assignees.map((user) => `@${user?.login}`).join(", "); | ||
|
||
await addCommentToIssue(context, `${users} the deadline is at ${deadline}`); | ||
return { status: "ok" }; | ||
} |
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