Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Separate label function files #14

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
56 changes: 22 additions & 34 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,8 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import { isLabelInPR } from "./utils/isLabelInPR";

type Payload = {
owner: string;
repo: string;
issueNumber: number;
};

const prepareOctokit = async ({ owner, repo, issueNumber }: Payload) => (
// TODO: any type for now. fix later
label: { labels: [string] } | { name: string },
fn: any
): Promise<void> =>
fn({
owner,
repo,
issue_number: issueNumber,
...label,
});
import { addLabels } from "./utils/addLabels";
import { removeLabel } from "./utils/removeLabel";

const get = (name: string): string => core.getInput(name, { required: true });

Expand All @@ -35,12 +19,6 @@ const main = async (): Promise<void> => {

const { owner, repo } = context.repo;

const labelFuncs = await prepareOctokit({
owner,
repo,
issueNumber,
});

const {
data: { draft },
} = await octokit.pulls.get({
Expand All @@ -49,14 +27,6 @@ const main = async (): Promise<void> => {
pull_number: issueNumber,
});

const add = (label: string, fn = octokit.issues.addLabels): Promise<void> =>
labelFuncs({ labels: [label] }, fn);

const remove = (
label: string,
fn = octokit.issues.removeLabel
): Promise<void> => labelFuncs({ name: label }, fn);

// check the labels in pr event on event
const { data: labels } = await octokit.issues.listLabelsOnIssue({
...context.repo,
Expand All @@ -72,9 +42,27 @@ const main = async (): Promise<void> => {

return draft
? // to draft
add(inProgressLabel) && remove(readyForReviewLabel)
addLabels(octokit.issues.addLabels, inProgressLabel, {
owner,
repo,
issue_number: issueNumber,
}) &&
removeLabel(octokit.issues.removeLabel, readyForReviewLabel, {
owner,
repo,
issue_number: issueNumber,
})
: // to ready for review
add(readyForReviewLabel) && remove(inProgressLabel);
addLabels(octokit.issues.addLabels, readyForReviewLabel, {
owner,
repo,
issue_number: issueNumber,
}) &&
removeLabel(octokit.issues.removeLabel, inProgressLabel, {
owner,
repo,
issue_number: issueNumber,
});
} catch (error) {
core.debug(`message: ${error}`);
}
Expand Down
21 changes: 21 additions & 0 deletions src/utils/addLabels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* addLabels: {(
* params?: Octokit.RequestOptions &
* Octokit.IssuesAddLabelsParamsDeprecatedNumber
* ): Promise<Octokit.Response<Octokit.IssuesAddLabelsResponse>>;
* (
* params?: Octokit.RequestOptions & Octokit.IssuesAddLabelsParams
* ): Promise<Octokit.Response<Octokit.IssuesAddLabelsResponse>>;
* endpoint: Octokit.Endpoint;
* };
*/
type AddLabels = (
_addLabels: any,
label: string,
requestOptions: { owner: string; repo: string; issue_number: number }
) => Promise<any>;
export const addLabels: AddLabels = async (_addLabels, label, requestOptions) =>
_addLabels({
...requestOptions,
labels: [label],
});
15 changes: 15 additions & 0 deletions src/utils/removeLabel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type RemoveLabel = (
_removeLabel: any,
label: string,
requestOptions: { owner: string; repo: string; issue_number: number }
) => Promise<any>;
export const removeLabel: RemoveLabel = async (
_removeLabel,
label,
requestOptions
) => {
_removeLabel({
...requestOptions,
name: label,
});
};