Skip to content

Commit

Permalink
chore: add file
Browse files Browse the repository at this point in the history
  • Loading branch information
Anmol-Baranwal committed Oct 16, 2023
1 parent 391b6de commit afc6148
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"use strict";
// import * as core from "@actions/core";
// import * as github from "@actions/github";
Object.defineProperty(exports, "__esModule", { value: true });
const core = require("@actions/core");
const github = require("@actions/github");
async function HandleMultipleIssues() {
var _a;
try {
const token = process.env.GITHUB_TOKEN;
if (!token)
core.debug(token + "");
else
core.debug(token);
if (!token) {
core.setFailed("GitHub token is missing. Make sure to set the GITHUB_TOKEN secret.");
return;
}
const octokit = github.getOctokit(token);
const context = github.context;
core.notice("step 1.");
// Retrieve custom inputs
const label = core.getInput("label") || "up for grabs"; // Set default label
const issueNumber = core.getInput("issueNumber") === "true"; // converts to boolean
const comment = core.getInput("comment");
const close = core.getInput("close") === "true";
const checkComment = comment.trim() !== "";
// Check if the same author has open issues
const author = (_a = context.payload.issue) === null || _a === void 0 ? void 0 : _a.user.login;
core.notice("step 2.");
const { data: authorIssues } = await octokit.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
creator: author,
state: "open",
});
if (authorIssues.length === 0) {
core.notice("No existing open issues for this author.");
return; // No need to continue.
}
core.notice("step 3.");
const previousIssueNumbers = authorIssues
.filter((issue) => issue.number !== context.issue.number) // Exclude the current issue
.map((issue) => issue.number);
if (previousIssueNumbers.length > 0) {
const issueNumberToLabel = context.issue.number;
const issueLinks = previousIssueNumbers
.map((issueNumber) => `#${issueNumber}`)
.join(", ");
// Check if label is an array and add multiple labels if needed
if (Array.isArray(label)) {
for (const lbl of label) {
await octokit.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumberToLabel,
labels: [lbl],
});
}
}
else {
// Add a single label
await octokit.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumberToLabel,
labels: [label],
});
}
core.notice("Labels added to issue #" + issueNumberToLabel);
// Add comments based on conditions
if (issueNumber) {
// const issueLink = `#${issueNumberToLabel}`;
let commentText = "";
if (!checkComment) {
// Condition 1: issueNumber is true, comment is false
commentText = `${issueLinks} is already opened by you.`;
}
else if (checkComment) {
// Condition 2: issueNumber is true, comment is true
commentText = `${issueLinks} ${comment}`;
}
await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumberToLabel,
body: commentText,
});
core.notice("Comment added to issue #" + issueNumberToLabel);
}
else if (!issueNumber && checkComment) {
// Condition 3: issueNumber is false, comment is true
await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumberToLabel,
body: comment,
});
core.notice("Comment added to issue #" + issueNumberToLabel);
}
// Close the current issue if close is true
if (close && issueNumberToLabel === context.issue.number) {
await octokit.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumberToLabel,
state: "closed",
});
core.notice("Issue #" + issueNumberToLabel + " closed");
}
}
}
catch (error) {
core.notice("No Issue found!");
core.notice("Workflow failed: " + error.message);
}
}
HandleMultipleIssues();

0 comments on commit afc6148

Please sign in to comment.