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

Commit

Permalink
fix: move helpers to helper file
Browse files Browse the repository at this point in the history
  • Loading branch information
seprintour committed Apr 28, 2023
1 parent 3c14120 commit 8de6773
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
28 changes: 3 additions & 25 deletions src/handlers/assign/auto.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
import { Context } from "probot";
import { getBotContext, getLogger } from "../../bindings";
import { addAssignees } from "../../helpers";
import { addAssignees, getIssueByNumber, getPullRequests } from "../../helpers";
import { gitLinkedIssueParser } from "../../helpers/parser";
import { Payload } from "../../types";

// Use `context.octokit.rest` to get the pull requests for the repository
export const getPullRequests = async (context: Context) => {
const logger = getLogger();
const payload = context.payload as Payload;
try {
const { data: pulls } = await context.octokit.rest.pulls.list({
owner: payload.repository.owner.login,
repo: payload.repository.name,
state: "open",
});
return pulls;
} catch (e: unknown) {
logger.debug(`Fetching pull requests failed!, reason: ${e}`);
return [];
}
};

// Check for pull requests linked to their respective issues but not assigned to them
export const checkPullRequests = async () => {
const context = getBotContext();
Expand Down Expand Up @@ -51,11 +33,8 @@ export const checkPullRequests = async () => {

// Check if the pull request opener is assigned to the issue
const opener = pull!.user!.login;
const { data: issue } = await context.octokit.rest.issues.get({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: +linkedIssueNumber,
});

let issue = await getIssueByNumber(context, +linkedIssueNumber);

// if issue is already assigned, continue
if (issue!.assignees!.length > 0) {
Expand All @@ -67,7 +46,6 @@ export const checkPullRequests = async () => {
if (!assignedUsernames.includes(opener)) {
await addAssignees(+linkedIssueNumber, [opener]);
logger.debug(`Assigned pull request #${pull.number} opener to issue ${linkedIssueNumber}.`);
console.log(`Assigned pull request #${pull.number} opener to issue ${linkedIssueNumber}.`);
}
}
};
35 changes: 35 additions & 0 deletions src/helpers/issue.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Context } from "probot";
import { getBotContext, getLogger } from "../bindings";
import { Comment, Payload } from "../types";
import { checkRateLimitGit } from "../utils";
Expand Down Expand Up @@ -187,3 +188,37 @@ export const deleteLabel = async (label: string): Promise<void> => {
logger.debug(`Label deletion failed!, reason: ${e}`);
}
};

// Use `context.octokit.rest` to get the pull requests for the repository
export const getPullRequests = async (context: Context, state: "open" | "closed" | "all" = "open") => {
const logger = getLogger();
const payload = context.payload as Payload;
try {
const { data: pulls } = await context.octokit.rest.pulls.list({
owner: payload.repository.owner.login,
repo: payload.repository.name,
state,
});
return pulls;
} catch (e: unknown) {
logger.debug(`Fetching pull requests failed!, reason: ${e}`);
return [];
}
};

// Get issues by issue number
export const getIssueByNumber = async (context: Context, issue_number: number) => {
const logger = getLogger();
const payload = context.payload as Payload;
try {
const { data: issue } = await context.octokit.rest.issues.get({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number,
});
return issue;
} catch (e: unknown) {
logger.debug(`Fetching issue failed!, reason: ${e}`);
return;
}
};

0 comments on commit 8de6773

Please sign in to comment.