-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
limit-pull-requests: migrate to JS and add test
As part of #549.
- Loading branch information
1 parent
f3fe7e5
commit 65e77dd
Showing
3 changed files
with
206 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import core from "@actions/core"; | ||
import github from "@actions/github"; | ||
|
||
async function main() { | ||
try { | ||
const eventName = github.context.eventName; | ||
if (!["pull_request", "pull_request_target"].includes(eventName)) { | ||
core.setFailed(`${eventName} is not a supported event. Only pull_request ` | ||
+ `and pull_request_target are supported.`); | ||
return; | ||
} | ||
|
||
const token = core.getInput("token", { required: true }); | ||
|
||
const exceptUsersInput = core.getInput("except-users"); | ||
const exceptUsers = exceptUsersInput ? exceptUsersInput.split(",") : []; | ||
const exceptAuthorAssocsInput = | ||
core.getInput("except-author-associations"); | ||
const exceptAuthorAssocs = | ||
exceptAuthorAssocsInput ? exceptAuthorAssocsInput.split(",") : []; | ||
|
||
const commentLimit = core.getInput("comment-limit"); | ||
const comment = core.getInput("comment"); | ||
const closeLimit = core.getInput("close-limit"); | ||
const close = core.getBooleanInput("close"); | ||
|
||
if (!comment && !close) { | ||
core.info("No action specified; exiting."); | ||
return; | ||
} | ||
if (exceptUsers.includes(github.context.actor)) { | ||
core.info(`@${github.context.actor} is exempted from the limit.`); | ||
return; | ||
} | ||
if (exceptAuthorAssocs.includes( | ||
github.context.payload.pull_request.author_association)) { | ||
core.info(`@${github.context.actor} is exempted from the limit.`); | ||
return; | ||
} | ||
|
||
const client = github.getOctokit(token); | ||
const openPrs = await client.paginate(client.rest.pulls.list, { | ||
...github.context.repo, | ||
state: "open", | ||
per_page: 100, | ||
}); | ||
const prCount = openPrs | ||
.filter(pr => pr.user.login === github.context.actor) | ||
.length; | ||
core.info(`@${github.context.actor} has ${prCount} open PR(s).`); | ||
|
||
if (comment && prCount >= commentLimit) { | ||
await client.rest.issues.createComment({ | ||
...github.context.repo, | ||
issue_number: github.context.payload.pull_request.number, | ||
body: comment, | ||
}); | ||
core.notice(`Soft limit reached; commented on PR.`); | ||
} | ||
|
||
if (close && prCount >= closeLimit) { | ||
await client.rest.pulls.update({ | ||
...github.context.repo, | ||
pull_number: github.context.payload.pull_request.number, | ||
state: "closed", | ||
}); | ||
core.notice(`Hard limit reached; closed PR.`); | ||
} | ||
} catch (error) { | ||
core.setFailed(error); | ||
} | ||
} | ||
|
||
await main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import fs from "node:fs"; | ||
import os from "node:os"; | ||
import path from "node:path"; | ||
import util from "node:util"; | ||
|
||
describe("limit-pull-requests", async () => { | ||
const token = "fake-token"; | ||
const commentLimit = 3; | ||
const comment = "Please don't open too many PRs!"; | ||
const closeLimit = 5; | ||
const close = true; | ||
const exemptedUser = "exempted-user"; | ||
|
||
const prNumber = 12345; | ||
|
||
const GITHUB_ACTOR = "fake-actor"; | ||
const GITHUB_EVENT_NAME = "pull_request_target"; | ||
|
||
let directory; | ||
let eventPath; | ||
|
||
before(async () => { | ||
process.env.GITHUB_ACTOR = GITHUB_ACTOR; | ||
process.env.GITHUB_EVENT_NAME = GITHUB_EVENT_NAME; | ||
|
||
directory = await fs.promises.mkdtemp(path.join(os.tmpdir(), "limit-pull-requests-")); | ||
eventPath = path.join(directory, "event.json"); | ||
await fs.promises.writeFile(eventPath, JSON.stringify({ | ||
pull_request: { | ||
number: prNumber, | ||
author_association: "CONTRIBUTOR", | ||
}, | ||
})); | ||
|
||
process.env.GITHUB_EVENT_PATH = eventPath; | ||
}); | ||
|
||
after(async () => { | ||
await fs.promises.rm(directory, { recursive: true }); | ||
}); | ||
|
||
beforeEach(async () => { | ||
mockInput("token", token); | ||
mockInput("comment-limit", commentLimit.toString()); | ||
mockInput("comment", comment); | ||
mockInput("close-limit", closeLimit.toString()); | ||
mockInput("close", close.toString()); | ||
mockInput("except-users", exemptedUser); | ||
}); | ||
|
||
it("does nothing if no limit is reached", async () => { | ||
const actor = GITHUB_ACTOR; | ||
const mockPool = githubMockPool(); | ||
|
||
mockPool.intercept({ | ||
method: "GET", | ||
path: `/repos/${GITHUB_REPOSITORY}/pulls?per_page=100&state=open`, | ||
headers: { | ||
Authorization: `token ${token}`, | ||
}, | ||
}).defaultReplyHeaders({ | ||
"Content-Type": "application/json", | ||
}).reply(200, [ | ||
{ user: { login: actor } }, | ||
{ user: { login: "some-other-actor-1" } }, | ||
{ user: { login: "some-other-actor-2" } }, | ||
{ user: { login: "some-other-actor-3" } }, | ||
]); | ||
|
||
await loadMain(); | ||
}); | ||
|
||
it("posts a comment and closes the PR if limits are reached", async () => { | ||
const actor = GITHUB_ACTOR; | ||
const mockPool = githubMockPool(); | ||
|
||
mockPool.intercept({ | ||
method: "GET", | ||
path: `/repos/${GITHUB_REPOSITORY}/pulls?per_page=100&state=open`, | ||
headers: { | ||
Authorization: `token ${token}`, | ||
}, | ||
}).defaultReplyHeaders({ | ||
"Content-Type": "application/json", | ||
}).reply(200, [ | ||
{ user: { login: actor } }, | ||
{ user: { login: actor } }, | ||
{ user: { login: actor } }, | ||
{ user: { login: actor } }, | ||
{ user: { login: actor } }, | ||
{ user: { login: actor } }, | ||
{ user: { login: "some-other-actor-1" } }, | ||
{ user: { login: "some-other-actor-2" } }, | ||
{ user: { login: "some-other-actor-3" } }, | ||
]); | ||
|
||
mockPool.intercept({ | ||
method: "POST", | ||
path: `/repos/${GITHUB_REPOSITORY}/issues/${prNumber}/comments`, | ||
headers: { | ||
Authorization: `token ${token}`, | ||
}, | ||
body: (body) => util.isDeepStrictEqual(JSON.parse(body), { | ||
body: comment, | ||
}), | ||
}).defaultReplyHeaders({ | ||
"Content-Type": "application/json", | ||
}).reply(200, {}); | ||
|
||
mockPool.intercept({ | ||
method: "PATCH", | ||
path: `/repos/${GITHUB_REPOSITORY}/pulls/${prNumber}`, | ||
headers: { | ||
Authorization: `token ${token}`, | ||
}, | ||
body: (body) => util.isDeepStrictEqual(JSON.parse(body), { | ||
state: "closed", | ||
}), | ||
}).defaultReplyHeaders({ | ||
"Content-Type": "application/json", | ||
}).reply(200, {}); | ||
|
||
await loadMain(); | ||
}); | ||
|
||
it("does nothing if limits are reached but user is exempted", async () => { | ||
process.env.GITHUB_ACTOR = exemptedUser; | ||
await loadMain(); | ||
}); | ||
}); |