Skip to content

Commit

Permalink
chore: fix tests, using timeline over comment if later than
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Sep 19, 2024
1 parent 960a7bf commit e05524d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 48 deletions.
48 changes: 28 additions & 20 deletions src/helpers/task-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,47 @@ export async function getTaskMetadata(
const assignmentRegex = /Ubiquity - Assignment - start -/gi;
const botAssignmentComments = botComments
.filter((o) => assignmentRegex.test(o?.body || ""))
.sort((a, b) => DateTime.fromISO(a.created_at).toMillis() - DateTime.fromISO(b.created_at).toMillis());
.sort((a, b) => DateTime.fromISO(b.created_at).toMillis() - DateTime.fromISO(a.created_at).toMillis());

// Has the bot previously reminded them?
const botFollowup = /<!-- Ubiquity - Followup - remindAssignees/gi;
const botFollowupComments = botComments
.filter((o) => botFollowup.test(o?.body || ""))
.sort((a, b) => DateTime.fromISO(a.created_at).toMillis() - DateTime.fromISO(b.created_at).toMillis());
.sort((a, b) => DateTime.fromISO(b.created_at).toMillis() - DateTime.fromISO(a.created_at).toMillis());

// `lastCheck` represents the last time the bot intervened in the issue, separate from the activity tracking of a user.
const lastCheckComment = botFollowupComments[0]?.created_at ? botFollowupComments[0] : botAssignmentComments[0];
let lastCheck = lastCheckComment?.created_at ? DateTime.fromISO(lastCheckComment.created_at) : null;

// if we don't have a lastCheck yet, use the assignment event
if (!lastCheck) {
logger.info("No last check found, using assignment event");
const assignmentEvents = await octokit.paginate(octokit.rest.issues.listEvents, {
owner: repo.owner.login,
repo: repo.name,
issue_number: issue.number,
});

const assignmentEvent = assignmentEvents.find((o) => o.event === "assigned");
if (assignmentEvent) {
lastCheck = DateTime.fromISO(assignmentEvent.created_at);
} else {
logger.error(`Failed to find last check for ${issue.html_url}`);
return false;
}
// incase their was self-assigning after the lastCheck
const assignmentEvents = await octokit.paginate(octokit.rest.issues.listEvents, {
owner: repo.owner.login,
repo: repo.name,
issue_number: issue.number,
});

const assignedEvents = assignmentEvents
.filter((o) => o.event === "assigned")
.sort((a, b) => DateTime.fromISO(b.created_at).toMillis() - DateTime.fromISO(a.created_at).toMillis());

const latestUserAssignment = assignedEvents.find((o) => o.actor?.type === "User");
const latestBotAssignment = assignedEvents.find((o) => o.actor?.type === "Bot");

let mostRecentAssignmentEvent = latestUserAssignment || latestBotAssignment;

if (latestUserAssignment && latestBotAssignment && DateTime.fromISO(latestUserAssignment.created_at) > DateTime.fromISO(latestBotAssignment.created_at)) {
mostRecentAssignmentEvent = latestUserAssignment;
} else {
mostRecentAssignmentEvent = latestBotAssignment;
}

if (mostRecentAssignmentEvent && (!lastCheck || DateTime.fromISO(mostRecentAssignmentEvent.created_at) > lastCheck)) {
lastCheck = DateTime.fromISO(mostRecentAssignmentEvent.created_at);
logger.debug(`Using assignment event`, { mostRecentAssignmentEvent });
}

if (!lastCheck) {
logger.error(`Failed to find last check for ${issue.html_url}`);
logger.error(`No last check found for ${issue.html_url}`);
return false;
}

Expand All @@ -64,7 +73,6 @@ export async function getTaskMetadata(

if (!metadata.taskAssignees?.length) {
logger.error(`Missing Assignees from ${issue.html_url}`);
return false;
}

const durationInMs = parseTimeLabel(issue.labels);
Expand Down
7 changes: 3 additions & 4 deletions tests/__mocks__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,21 @@ export function createIssue(id: number, assignees: { login: string; id: number }
});
}

export function createEvent() {
export function createEvent(id: number, created_at = new Date(Date.now() - ONE_DAY).toISOString()) {
db.event.create({
id: 1,
id,
actor: {
id: 1,
type: "User",
login: "ubiquity",
name: null,
},
owner: "ubiquity",
repo: "test-repo",
issue_number: 1,
event: "assigned",
commit_id: null,
commit_url: "https://github.com/ubiquity/test-repo/commit/1",
created_at: new Date(Date.now() - ONE_DAY).toISOString(),
created_at,
assignee: {
login: "ubiquity",
},
Expand Down
7 changes: 7 additions & 0 deletions tests/__mocks__/mock-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ export default [
{
id: 1,
login: "ubiquity",
type: "User",
},
{
id: 2,
login: "bot",
type: "Bot",
},
{
id: 100000000,
login: "user2",
type: "User",
},
];
44 changes: 20 additions & 24 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,31 @@ describe("User start/stop", () => {
it("Should process update for all repos except optOut", async () => {
const context = createContext(1, 1);
const infoSpy = jest.spyOn(context.logger, "info");
await runPlugin(context);
createComment(5, 3, STRINGS.BOT, "Bot", botAssignmentComment(2, daysPriorToNow(1)), daysPriorToNow(1));
createEvent(2, daysPriorToNow(1));

await expect(runPlugin(context)).resolves.toBe(true);

// The logs skipped just contain the timestamp infos: "last check was on...."

expect(infoSpy).toHaveBeenNthCalledWith(1, STRINGS.USING_ASSIGNMENT_EVENT);
expect(infoSpy).toHaveBeenNthCalledWith(3, `Nothing to do for ${getIssueHtmlUrl(1)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(4, STRINGS.USING_ASSIGNMENT_EVENT);
expect(infoSpy).toHaveBeenNthCalledWith(6, `Nothing to do for ${getIssueHtmlUrl(2)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(7, STRINGS.USING_ASSIGNMENT_EVENT);
expect(infoSpy).toHaveBeenNthCalledWith(9, `Nothing to do for ${getIssueHtmlUrl(3)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(10, STRINGS.USING_ASSIGNMENT_EVENT);
expect(infoSpy).toHaveBeenNthCalledWith(12, `Nothing to do for ${getIssueHtmlUrl(4)}, still within due-time.`);
expect(infoSpy).not.toHaveBeenNthCalledWith(14, `Nothing to do for https://github.com/ubiquity/private-repo/issues/5, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(2, `Nothing to do for ${getIssueHtmlUrl(1)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(4, `Nothing to do for ${getIssueHtmlUrl(2)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(6, `Nothing to do for ${getIssueHtmlUrl(3)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(8, `Nothing to do for ${getIssueHtmlUrl(4)}, still within due-time.`);
expect(infoSpy).not.toHaveBeenNthCalledWith(10, `Nothing to do for https://github.com/ubiquity/private-repo/issues/5, still within due-time.`);
});

it("Should include the previously excluded repo", async () => {
const context = createContext(1, 1, []);
const infoSpy = jest.spyOn(context.logger, "info");
await runPlugin(context);
createComment(5, 3, STRINGS.BOT, "Bot", botAssignmentComment(2, daysPriorToNow(1)), daysPriorToNow(1));
createEvent(2, daysPriorToNow(1));

await expect(runPlugin(context)).resolves.toBe(true);

expect(infoSpy).toHaveBeenNthCalledWith(1, STRINGS.USING_ASSIGNMENT_EVENT);
expect(infoSpy).toHaveBeenNthCalledWith(3, `Nothing to do for ${getIssueHtmlUrl(1)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(4, STRINGS.USING_ASSIGNMENT_EVENT);
expect(infoSpy).toHaveBeenNthCalledWith(6, `Nothing to do for ${getIssueHtmlUrl(2)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(7, STRINGS.USING_ASSIGNMENT_EVENT);
expect(infoSpy).toHaveBeenNthCalledWith(9, `Nothing to do for ${getIssueHtmlUrl(3)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(10, STRINGS.USING_ASSIGNMENT_EVENT);
expect(infoSpy).toHaveBeenNthCalledWith(12, `Nothing to do for ${getIssueHtmlUrl(4)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(13, STRINGS.USING_ASSIGNMENT_EVENT);
expect(infoSpy).toHaveBeenNthCalledWith(15, `Nothing to do for https://github.com/ubiquity/private-repo/issues/5, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(2, `Nothing to do for ${getIssueHtmlUrl(1)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(4, `Nothing to do for ${getIssueHtmlUrl(2)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(6, `Nothing to do for ${getIssueHtmlUrl(3)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(8, `Nothing to do for ${getIssueHtmlUrl(4)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(10, `Nothing to do for https://github.com/ubiquity/private-repo/issues/5, still within due-time.`);
});

it("Should eject the user after the disqualification period", async () => {
Expand All @@ -122,6 +117,7 @@ describe("User start/stop", () => {

const timestamp = daysPriorToNow(9);
createComment(3, 3, STRINGS.BOT, "Bot", botAssignmentComment(2, timestamp), timestamp);
createEvent(2, timestamp);

const issue = db.issue.findFirst({ where: { id: { equals: 4 } } });
expect(issue?.assignees).toEqual([{ login: STRINGS.USER, id: 2 }]);
Expand Down Expand Up @@ -229,7 +225,7 @@ async function setupTests() {
createComment(1, 1, STRINGS.UBIQUITY);
createComment(2, 2, STRINGS.UBIQUITY);

createEvent();
createEvent(1);
}

function daysPriorToNow(days: number) {
Expand Down

0 comments on commit e05524d

Please sign in to comment.