Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Hotfix: check org and repo for linked issue #789

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions src/handlers/assign/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,45 @@ export const checkPullRequests = async () => {

// Loop through the pull requests and assign them to their respective issues if needed
for (const pull of pulls) {
const linkedIssue = await gitLinkedIssueParser({
const linkedIssues = await gitLinkedIssueParser({
owner: payload.repository.owner.login,
repo: payload.repository.name,
pull_number: pull.number,
});

// if pullRequestLinked is empty, continue
if (linkedIssue == "" || !pull.user || !linkedIssue) {
continue;
}
for (const linkedIssue of linkedIssues) {
// if pullRequestLinked is empty, continue
if (linkedIssue == "" || !pull.user || !linkedIssue) {
continue;
}

const connectedPull = await getPullByNumber(context, pull.number);
const connectedPull = await getPullByNumber(context, pull.number);

// Newly created PULL (draft or direct) pull does have same `created_at` and `updated_at`.
if (connectedPull?.created_at !== connectedPull?.updated_at) {
logger.debug("It's an updated Pull Request, reverting");
continue;
}
// The new PR, whether it's in draft or in direct form, it has identical `created_at` and `updated_at` timestamps.
if (connectedPull?.created_at !== connectedPull?.updated_at) {
logger.debug("Skipping because it's not a new PR");
continue;
}

const linkedIssueNumber = linkedIssue.substring(linkedIssue.lastIndexOf("/") + 1);
const linkedIssueNumber = linkedIssue.substring(linkedIssue.lastIndexOf("/") + 1);

// Check if the pull request opener is assigned to the issue
const opener = pull.user.login;
// Check if the pull request opener is assigned to the issue
const opener = pull.user.login;

const issue = await getIssueByNumber(context, +linkedIssueNumber);
if (!issue?.assignees) continue;
const issue = await getIssueByNumber(context, +linkedIssueNumber);
if (!issue?.assignees) continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's no assignee, we don't need to assign the opener of the linked PR to the issue?

Copy link
Contributor Author

@wannacfuture wannacfuture Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't mean there is no assinee.
It just wants to make sure if assinees field exists on the issue not if(issue?.assignees.length === 0)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like the same thing. No assignee means the assignees field doesn't exist, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No assignee means assignees field is empty array, not undefined.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this if statement will only execute if assignees is undefined or null, but not if its empty array. github should always send empty array if there's no assignees but if they don't send the field, this could possibly be a bug.

Copy link
Contributor Author

@wannacfuture wannacfuture Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. we need to go on, to assign the author of the linked PR to the issue if assignees is empty array.
If it is undefined or null, it should skip the loop.


// if issue is already assigned, continue
if (issue.assignees.length > 0) {
logger.debug(`Issue already assigned, ignoring...`);
continue;
}
// if issue is already assigned, continue
if (issue.assignees.length > 0) {
logger.debug(`Issue already assigned, ignoring...`);
continue;
}

const assignedUsernames = issue.assignees.map((assignee) => assignee.login);
if (!assignedUsernames.includes(opener)) {
await addAssignees(+linkedIssueNumber, [opener]);
logger.debug(`Assigned pull request #${pull.number} opener to issue ${linkedIssueNumber}.`);
const assignedUsernames = issue.assignees.map((assignee) => assignee.login);
if (!assignedUsernames.includes(opener)) {
await addAssignees(+linkedIssueNumber, [opener]);
logger.debug(`Assigned pull request #${pull.number} opener to issue ${linkedIssueNumber}.`);
}
}
}
};
28 changes: 22 additions & 6 deletions src/helpers/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,38 @@ export interface LinkedPR {

export const gitLinkedIssueParser = async ({ owner, repo, pull_number }: GitParser) => {
const logger = getLogger();
const linkedIssueUrls = [];
try {
const { data } = await axios.get(`https://github.com/${owner}/${repo}/pull/${pull_number}`);
const dom = parse(data);
const devForm = dom.querySelector("[data-target='create-branch.developmentForm']") as HTMLElement;
const linkedIssues = devForm.querySelectorAll(".my-1");

if (linkedIssues.length === 0) {
return null;
}
if (linkedIssues.length === 0) return [];

for (const linkedIssue of linkedIssues) {
const issueUrl = linkedIssue.querySelector("a")?.attrs?.href;

if (!issueUrl) continue;

const parts = issueUrl.split("/");

// check if array size is at least 4
if (parts.length < 4) continue;

// extract the organization name and repo name from the link:(e.g. "
const issueOrganization = parts[parts.length - 4];
const issueRepository = parts[parts.length - 3];

const issueUrl = linkedIssues[0].querySelector("a")?.attrs?.href || "";
return issueUrl;
if (`${issueOrganization}/${issueRepository}` !== `${owner}/${repo}`) continue;

linkedIssueUrls.push(issueUrl);
}
} catch (error) {
logger.error(`${JSON.stringify(error)}`);
return null;
}

return linkedIssueUrls;
};

export const gitLinkedPrParser = async ({ owner, repo, issue_number }: GitParser): Promise<LinkedPR[]> => {
Expand Down
Loading