Skip to content

Commit

Permalink
feat: add option for ignoring collaborators of repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Anmol-Baranwal committed Nov 16, 2023
1 parent 19e4855 commit cd5e454
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 68 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ inputs:
description: 'Specify usernames that should be ignored while running this workflow'
default: 'false'
required: false
ignoreCollaborators:
description: 'Ignore all the collaborators in the repository while running this workflow'
default: 'false'
required: false
runs:
using: 'node16'
main: 'dist/index.js'
Expand Down
37 changes: 28 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,50 @@ async function HandleMultipleIssues() {
const context = github.context;
core.notice("step 1.");
// Retrieve custom inputs
const labels = core.getInput("label").split(",").map(label => label.trim());
const labels = core
.getInput("label")
.split(",")
.map((label) => label.trim());
const assign = core.getInput("assign") === "true" || false;
const issueNumber = core.getInput("issueNumber") === "true";
const comment = core.getInput("comment");
const close = core.getInput("close") === "true" || false;
const ignoreUsers = core.getInput("ignore-users").split(",").map(user => user.trim());
const ignoreUsers = core
.getInput("ignore-users")
.split(",")
.map((user) => user.trim());
const ignoreCollaboratorsInput = core.getInput("ignoreCollaborators") === "true" || false;
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;
if (ignoreUsers.includes(author)) {
core.notice(`User: ${author} is on the ignore list. Ignoring the workflow for this user.`);
return; // No need to continue.
}
const collaboratorUsernames = ignoreCollaboratorsInput
? (await octokit.rest.repos.listCollaborators({
owner: context.repo.owner,
repo: context.repo.repo
})).data.map((collaborator) => collaborator.login)
: [];
if (collaboratorUsernames.includes(author)) {
core.notice(`User ${author} is a collaborator. Ignoring the issue for collaborators.`);
return; // No need to continue.
}
core.notice("step 2.");
const { data: authorIssues } = await octokit.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
creator: author,
state: "open",
state: "open"
});
const filteredIssues = assign
? authorIssues.filter((issue) => issue.assignees.some((assignee) => assignee.login === author))
: authorIssues;
if (filteredIssues.length === 0) {
core.notice(`No existing ${assign === true ? "issues created by and assigned to" : "open issues for"} this author.`);
core.notice(`No existing ${assign === true
? "issues created by and assigned to"
: "open issues for"} this author.`);
return; // No need to continue.
}
core.notice("step 3.");
Expand All @@ -91,7 +110,7 @@ async function HandleMultipleIssues() {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumberToLabel,
labels: [lbl],
labels: [lbl]
});
}
}
Expand All @@ -101,7 +120,7 @@ async function HandleMultipleIssues() {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumberToLabel,
labels: [labels],
labels: [labels]
});
}
core.notice("Labels added to issue #" + issueNumberToLabel);
Expand All @@ -124,7 +143,7 @@ async function HandleMultipleIssues() {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumberToLabel,
body: commentText,
body: commentText
});
core.notice("Comment added to issue #" + issueNumberToLabel);
}
Expand All @@ -134,7 +153,7 @@ async function HandleMultipleIssues() {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumberToLabel,
body: comment,
body: comment
});
core.notice("Comment added to issue #" + issueNumberToLabel);
}
Expand All @@ -144,7 +163,7 @@ async function HandleMultipleIssues() {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumberToLabel,
state: "closed",
state: "closed"
});
core.notice("Issue #" + issueNumberToLabel + " closed");
}
Expand Down
147 changes: 88 additions & 59 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,107 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import * as core from "@actions/core"
import * as github from "@actions/github"

async function HandleMultipleIssues() {
try {
const token = core.getInput("gh-token");
const token = core.getInput("gh-token")

if (!token) core.debug(token + "");
else core.debug(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;
)
return
}

const octokit = github.getOctokit(token);
const context = github.context;
const octokit = github.getOctokit(token)
const context = github.context

core.notice("step 1.");
core.notice("step 1.")

// Retrieve custom inputs
const labels = core.getInput("label").split(",").map(label => label.trim());
const assign = core.getInput("assign") === "true" || false;
const issueNumber = core.getInput("issueNumber") === "true";
const comment = core.getInput("comment");
const close = core.getInput("close") === "true" || false;
const ignoreUsers = core.getInput("ignore-users").split(",").map(user => user.trim());

const checkComment = comment.trim() !== "";
const labels = core
.getInput("label")
.split(",")
.map((label) => label.trim())
const assign = core.getInput("assign") === "true" || false
const issueNumber = core.getInput("issueNumber") === "true"
const comment = core.getInput("comment")
const close = core.getInput("close") === "true" || false
const ignoreUsers = core
.getInput("ignore-users")
.split(",")
.map((user) => user.trim())
const ignoreCollaboratorsInput =
core.getInput("ignoreCollaborators") === "true" || false

const checkComment = comment.trim() !== ""

// Check if the same author has open issues
const author = context.payload.issue?.user.login;
const author = context.payload.issue?.user.login

if (ignoreUsers.includes(author)) {
core.notice(`User: ${author} is on the ignore list. Ignoring the workflow for this user.`);
return; // No need to continue.
core.notice(
`User: ${author} is on the ignore list. Ignoring the workflow for this user.`
)
return // No need to continue.
}

const collaboratorUsernames = ignoreCollaboratorsInput
? (
await octokit.rest.repos.listCollaborators({
owner: context.repo.owner,
repo: context.repo.repo
})
).data.map((collaborator) => collaborator.login)
: []

if (collaboratorUsernames.includes(author)) {
core.notice(
`User ${author} is a collaborator. Ignoring the issue for collaborators.`
)
return // No need to continue.
}

core.notice("step 2.");
core.notice("step 2.")

const { data: authorIssues } = await octokit.rest.issues.listForRepo({
const {data: authorIssues} = await octokit.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
creator: author,
state: "open",
});
state: "open"
})

const filteredIssues = assign
? authorIssues.filter((issue: any) =>
issue.assignees.some((assignee: any) => assignee.login === author)
)
: authorIssues

if (filteredIssues.length === 0) {
core.notice(
`No existing ${
assign === true ? "issues created by and assigned to" : "open issues for"
} this author.`
)
return // No need to continue.
}
if (filteredIssues.length === 0) {
core.notice(
`No existing ${
assign === true
? "issues created by and assigned to"
: "open issues for"
} this author.`
)
return // No need to continue.
}

core.notice("step 3.");
core.notice("step 3.")

const previousIssueNumbers = filteredIssues
.filter((issue: { number: any }) => issue.number !== context.issue.number) // Exclude the current issue
.map((issue: { number: any }) => issue.number);
.filter((issue: {number: any}) => issue.number !== context.issue.number) // Exclude the current issue
.map((issue: {number: any}) => issue.number)

if (previousIssueNumbers.length > 0) {
const issueNumberToLabel = context.issue.number;
const issueNumberToLabel = context.issue.number

const issueLinks = previousIssueNumbers
.map((issueNumber: any) => `#${issueNumber}`)
.join(", ");
.join(", ")

// Check if label is an array and add multiple labels if needed
if (Array.isArray(labels)) {
Expand All @@ -82,55 +110,56 @@ async function HandleMultipleIssues() {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumberToLabel,
labels: [lbl],
});
labels: [lbl]
})
}
} else {
// Add a single label
await octokit.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumberToLabel,
labels: [labels],
});
labels: [labels]
})
}

core.notice("Labels added to issue #" + issueNumberToLabel);
core.notice("Labels added to issue #" + issueNumberToLabel)

// Add comments based on conditions
if (issueNumber) {
// const issueLink = `#${issueNumberToLabel}`;
let commentText: string = "";
let commentText: string = ""

if (!checkComment) {
// Condition 1: issueNumber is true, comment is false

if(assign) commentText = `${issueLinks} has been opened by you and is also assigned to you.`;
else commentText = `${issueLinks} is already opened by you.`;
if (assign)
commentText = `${issueLinks} has been opened by you and is also assigned to you.`
else commentText = `${issueLinks} is already opened by you.`
} else if (checkComment) {
// Condition 2: issueNumber is true, comment is true
commentText = `${issueLinks} ${comment}`;
commentText = `${issueLinks} ${comment}`
}

await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumberToLabel,
body: commentText,
});
body: commentText
})

core.notice("Comment added to issue #" + issueNumberToLabel);
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,
});
body: comment
})

core.notice("Comment added to issue #" + issueNumberToLabel);
core.notice("Comment added to issue #" + issueNumberToLabel)
}

// Close the current issue if close is true
Expand All @@ -139,16 +168,16 @@ async function HandleMultipleIssues() {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumberToLabel,
state: "closed",
});
state: "closed"
})

core.notice("Issue #" + issueNumberToLabel + " closed");
core.notice("Issue #" + issueNumberToLabel + " closed")
}
}
} catch (error: any) {
core.notice("No Issue found!");
core.notice("Workflow failed: " + error.message);
core.notice("No Issue found!")
core.notice("Workflow failed: " + error.message)
}
}

HandleMultipleIssues();
HandleMultipleIssues()

0 comments on commit cd5e454

Please sign in to comment.