Skip to content

Commit

Permalink
chore: teams test
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Jul 15, 2024
1 parent 24cc92e commit cd2817f
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/handlers/shared/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ export async function start(context: Context, issue: Context["payload"]["issue"]
const duration: number = calculateDurations(labels).shift() ?? 0;

const { id, login } = sender;
const logMessage = logger.info("Task assigned successfully", { duration, priceLabel, revision: commitHash?.substring(0, 7), teammate: teammates, assignee: login, issue: issue.number });
const logMessage = logger.info("Task assigned successfully", {
duration,
priceLabel,
revision: commitHash?.substring(0, 7),
teammate: teammates,
assignee: login,
issue: issue.number,
});

const assignmentComment = await generateAssignmentComment(context, issue.created_at, issue.number, id, duration);
const metadata = structuredMetadata.create("Assignment", logMessage);
Expand Down
5 changes: 4 additions & 1 deletion src/handlers/user-start-stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export async function userStartStop(context: Context): Promise<{ output: string
const { payload, config } = context;
const { issue, comment, sender, repository } = payload;
const slashCommand = comment.body.split(" ")[0].replace("/", "");
const teamMates = comment.body.split("@").slice(1).map((teamMate) => teamMate.split(" ")[0]);
const teamMates = comment.body
.split("@")
.slice(1)
.map((teamMate) => teamMate.split(" ")[0]);
const { isEnabled } = config;

if (!isEnabled) {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/get-linked-prs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export async function getLinkedPullRequests(context: Context, { owner, repositor
author: pr.user?.login,
state: pr.state,
};
}).filter((pr) => pr !== null)
})
.filter((pr) => pr !== null)
.filter((pr) => pr.state === "open") as GetLinkedResults[];
}
6 changes: 3 additions & 3 deletions src/utils/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,19 @@ export async function closePullRequestForAnIssue(context: Context, issueNumber:
}

const currentRepo = context.payload.repository;
let closed = false
let isClosed = false;

for await (const pr of linkedPullRequests) {
if (pr.author !== author || pr.organization !== currentRepo.owner.login || pr.repository !== currentRepo.name) {
continue;
} else {
await closePullRequest(context, pr);
comment += ` ${pr.href} `;
closed = true;
isClosed = true;
}
}

if (!closed) {
if (!isClosed) {
return logger.info(`No PRs were closed`);
}

Expand Down
1 change: 1 addition & 0 deletions tests/__mocks__/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export const db = factory({
issue: {
number: Number,
html_url: String,
state: String,
repository: {
full_name: String,
},
Expand Down
30 changes: 27 additions & 3 deletions tests/__mocks__/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,33 @@ export const handlers = [
return HttpResponse.json({ owner, repo, pull_number });
}),
// add assignee to an issue
http.post("https://api.github.com/repos/:owner/:repo/issues/:issue_number/assignees", ({ params: { owner, repo, issue_number } }) => {
return HttpResponse.json({ owner, repo, issue_number });
}),
http.post(
"https://api.github.com/repos/:owner/:repo/issues/:issue_number/assignees",
async ({ params: { owner, repo, issue_number }, request: { body } }) => {
const reader = body?.getReader();
if (!reader) {
return HttpResponse.json({ owner, repo, issue_number });
}
const { assignees } = await reader.read().then(({ value }) => {
return JSON.parse(new TextDecoder().decode(value));
});

const issue = db.issue.findFirst({
where: { owner: { equals: owner as string }, repo: { equals: repo as string }, number: { equals: Number(issue_number) } },
});

if (issue) {
db.issue.update({
where: { id: { equals: issue.id } },
data: {
assignees,
},
});
}

return HttpResponse.json({ owner, repo, issue_number, assignees });
}
),
// list all pull requests
http.get("https://api.github.com/repos/:owner/:repo/pulls", ({ params: { owner, repo } }) => {
return HttpResponse.json(db.pull.findMany({ where: { owner: { equals: owner as string }, repo: { equals: repo as string } } }));
Expand Down
1 change: 1 addition & 0 deletions tests/__mocks__/issue-template.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default {
assignees: [],
assignee: {
login: "",
avatar_url: "",
Expand Down
23 changes: 22 additions & 1 deletion tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ describe("User start/stop", () => {
expect(output).toEqual("Task assigned successfully");
});

test("User can start an issue with teammates", async () => {
const issue = db.issue.findFirst({ where: { id: { equals: 1 } } }) as unknown as Issue;
const sender = db.users.findFirst({ where: { id: { equals: 1 } } }) as unknown as Sender;

const context = createContext(issue, sender, "/start @user2");

context.adapters = createAdapters(getSupabase(), context as unknown as Context);

const { output } = await userStartStop(context as unknown as Context);

expect(output).toEqual("Task assigned successfully");

const issue2 = db.issue.findFirst({ where: { id: { equals: 1 } } }) as unknown as Issue;

expect(issue2.assignees).toHaveLength(2);

expect(issue2.assignees).toEqual(expect.arrayContaining(["ubiquity", "user2"]));
});

test("User can stop an issue", async () => {
// using the second issue
const issue = db.issue.findFirst({ where: { id: { equals: 2 } } }) as unknown as Issue;
Expand Down Expand Up @@ -422,7 +441,6 @@ async function setupTests() {
},
body: "Pull request body",
owner: "ubiquity",

repo: "test-repo",
state: "open",
closed_at: null,
Expand Down Expand Up @@ -458,6 +476,7 @@ async function setupTests() {
issue: {
number: 10,
html_url: "https://github.com/ubiquity/test-repo/pull/10",
state: "open",
repository: {
full_name: TEST_REPO,
},
Expand All @@ -483,6 +502,7 @@ async function setupTests() {
source: {
issue: {
number: 2,
state: "open",
html_url: "http://github.com/ubiquity/test-repo/pull/2",
repository: {
full_name: TEST_REPO,
Expand All @@ -509,6 +529,7 @@ async function setupTests() {
source: {
issue: {
number: 3,
state: "open",
html_url: "http://github.com/ubiquity/test-repo/pull/3",
repository: {
full_name: TEST_REPO,
Expand Down

0 comments on commit cd2817f

Please sign in to comment.