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 #40 from ubiquibot/development
Merge development into main
- Loading branch information
Showing
24 changed files
with
567 additions
and
251 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
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,10 @@ | ||
export enum HttpStatusCode { | ||
OK = 200, | ||
NOT_MODIFIED = 304, | ||
} | ||
|
||
export interface Result { | ||
status: HttpStatusCode; | ||
content?: string; | ||
reason?: string; | ||
} |
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,92 @@ | ||
import { Context } from "../../types"; | ||
import { getOwnerRepoFromHtmlUrl } from "../../utils/issue"; | ||
|
||
async function getUserStopComments(context: Context, username: string): Promise<number> { | ||
const { payload, octokit, logger } = context; | ||
const { number, html_url } = payload.issue; | ||
const { owner, repo } = getOwnerRepoFromHtmlUrl(html_url); | ||
|
||
try { | ||
const comments = await octokit.paginate(octokit.issues.listComments, { | ||
owner, | ||
repo, | ||
issue_number: number, | ||
}); | ||
|
||
return comments.filter((comment) => comment.body?.includes("/stop") && comment.user?.login.toLowerCase() === username.toLowerCase()).length; | ||
} catch (error) { | ||
throw new Error(logger.error("Error while getting user stop comments", { error: error as Error }).logMessage.raw); | ||
} | ||
} | ||
|
||
export async function hasUserBeenUnassigned(context: Context, username: string): Promise<boolean> { | ||
const { | ||
env: { APP_ID }, | ||
} = context; | ||
const events = await getAssignmentEvents(context); | ||
const userAssignments = events.filter((event) => event.assignee === username); | ||
|
||
if (userAssignments.length === 0) { | ||
return false; | ||
} | ||
|
||
const unassignedEvents = userAssignments.filter((event) => event.event === "unassigned"); | ||
// all bot unassignments (/stop, disqualification, etc) | ||
// TODO: task-xp-guard: will also prevent future assignments so we need to add a comment tracker we can use here | ||
const botUnassigned = unassignedEvents.filter((event) => event.actorId === APP_ID); | ||
// UI assignment | ||
const adminUnassigned = unassignedEvents.filter((event) => event.actor !== username && event.actorId !== APP_ID); | ||
// UI assignment | ||
const userUnassigned = unassignedEvents.filter((event) => event.actor === username); | ||
const userStopComments = await getUserStopComments(context, username); | ||
/** | ||
* Basically the bot will be the actor in most cases but if we | ||
* remove the /stop usage which does not trigger future disqualification | ||
* then any other bot unassignment will be considered valid | ||
*/ | ||
|
||
const botMinusUserStopCommands = Math.max(0, botUnassigned.length - userStopComments); | ||
const userUiMinusUserStopCommands = Math.max(0, userUnassigned.length - userStopComments); | ||
|
||
return botMinusUserStopCommands > 0 || userUiMinusUserStopCommands > 0 || adminUnassigned.length > 0; | ||
} | ||
|
||
async function getAssignmentEvents(context: Context) { | ||
const { repository, issue } = context.payload; | ||
try { | ||
const data = await context.octokit.paginate(context.octokit.issues.listEventsForTimeline, { | ||
owner: repository.owner.login, | ||
repo: repository.name, | ||
issue_number: issue.number, | ||
}); | ||
|
||
const events = data | ||
.filter((event) => event.event === "assigned" || event.event === "unassigned") | ||
.map((event) => { | ||
let actor, assignee, createdAt, actorId; | ||
|
||
if ((event.event === "unassigned" || event.event === "assigned") && "actor" in event && event.actor && "assignee" in event && event.assignee) { | ||
actor = event.actor.login; | ||
assignee = event.assignee.login; | ||
createdAt = event.created_at; | ||
actorId = event.actor.id; | ||
} | ||
|
||
return { | ||
event: event.event, | ||
actor, | ||
actorId, | ||
assignee, | ||
createdAt, | ||
}; | ||
}); | ||
|
||
return events | ||
.filter((event) => event !== undefined) | ||
.sort((a, b) => { | ||
return new Date(a.createdAt || "").getTime() - new Date(b.createdAt || "").getTime(); | ||
}); | ||
} catch (error) { | ||
throw new Error(context.logger.error("Error while getting assignment events", { error: error as Error }).logMessage.raw); | ||
} | ||
} |
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.