Skip to content

Commit 735eaaa

Browse files
committed
Add pull requests
1 parent 83f34a3 commit 735eaaa

3 files changed

Lines changed: 59 additions & 45 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# GitHub Toolkit
22

3-
A [GitHub App](https://docs.github.com/en/apps/creating-github-apps/about-creating-github-apps/about-creating-github-apps) that could do a variety of things, but currently adds all new issues created in the GitHub organization to some specific project.
3+
A [GitHub App](https://docs.github.com/en/apps/creating-github-apps/about-creating-github-apps/about-creating-github-apps) that could do a variety of things, but currently adds all new issues and pull requests created in the GitHub organization to some specific project.

src/webhook-endpoint/index.mjs

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,65 @@ import { App } from "octokit";
33

44
const OK_RESPONSE = { statusCode: 200 };
55

6-
async function handleIssue(payload) {
7-
console.log(JSON.stringify(payload));
8-
if (payload.action === "opened" && payload.issue.state === "open") {
9-
const app = new App({
10-
appId: 311508,
11-
// If the envar is added directly to the Lambda function it will
12-
// probably look like:
13-
// -----BEGIN RSA PRIVATE KEY-----\nMIIEo…
14-
// with the newlines replaced with literal "\n".
15-
// Those will be replaced with real newlines below.
16-
// If the envar is added via CloudFormation or AWS SAM, it will
17-
// maintain actual newlines and the `replace` will be a no-op.
18-
// When generating a private key for a GitHub app, it will download a
19-
// .pem file. The contents of that file is the private key.
20-
privateKey: process.env.GITHUB_APP_PRIVATE_KEY.replace(/\\n/g, "\n"),
21-
});
6+
async function addToProject(installationId, contentId) {
7+
const app = new App({
8+
appId: 311508,
9+
// If the envar is added directly to the Lambda function it will
10+
// probably look like:
11+
// -----BEGIN RSA PRIVATE KEY-----\nMIIEo…
12+
// with the newlines replaced with literal "\n".
13+
// Those will be replaced with real newlines below.
14+
// If the envar is added via CloudFormation or AWS SAM, it will
15+
// maintain actual newlines and the `replace` will be a no-op.
16+
// When generating a private key for a GitHub app, it will download a
17+
// .pem file. The contents of that file is the private key.
18+
privateKey: process.env.GITHUB_APP_PRIVATE_KEY.replace(/\\n/g, "\n"),
19+
});
2220

23-
const octokit = await app.getInstallationOctokit(payload.installation.id);
21+
const octokit = await app.getInstallationOctokit(installationId);
2422

25-
const projectOwnerName = "PRX";
26-
const projectNumber = 3;
27-
const ownerType = "organization";
23+
const projectOwnerName = "PRX";
24+
const projectNumber = 3;
25+
const ownerType = "organization";
2826

29-
const idResp = await octokit.graphql(
30-
`query getProject($projectOwnerName: String!, $projectNumber: Int!) {
31-
${ownerType}(login: $projectOwnerName) {
32-
projectV2(number: $projectNumber) {
33-
id
34-
}
27+
const idResp = await octokit.graphql(
28+
`query getProject($projectOwnerName: String!, $projectNumber: Int!) {
29+
${ownerType}(login: $projectOwnerName) {
30+
projectV2(number: $projectNumber) {
31+
id
3532
}
36-
}`,
37-
{
38-
projectOwnerName,
39-
projectNumber,
40-
},
41-
);
33+
}
34+
}`,
35+
{
36+
projectOwnerName,
37+
projectNumber,
38+
},
39+
);
4240

43-
const projectId = idResp[ownerType]?.projectV2.id;
44-
const contentId = payload.issue.node_id;
41+
const projectId = idResp[ownerType]?.projectV2.id;
4542

46-
await octokit.graphql(
47-
`mutation addIssueToProject {
48-
addProjectV2ItemById(
49-
input: {projectId: "${projectId}", contentId: "${contentId}"}
50-
) {
51-
clientMutationId
52-
}
53-
}`,
54-
);
43+
await octokit.graphql(
44+
`mutation addItemToProject {
45+
addProjectV2ItemById(
46+
input: {projectId: "${projectId}", contentId: "${contentId}"}
47+
) {
48+
clientMutationId
49+
}
50+
}`,
51+
);
52+
}
53+
54+
async function handleIssue(payload) {
55+
console.log(JSON.stringify(payload));
56+
if (payload.action === "opened" && payload.issue.state === "open") {
57+
await addToProject(payload.installation.id, payload.issue.node_id);
58+
}
59+
}
60+
61+
async function handlePullRequest(payload) {
62+
console.log(JSON.stringify(payload));
63+
if (payload.action === "opened" && payload.pull_request.state === "open") {
64+
await addToProject(payload.installation.id, payload.pull_request.node_id);
5565
}
5666
}
5767

@@ -78,6 +88,9 @@ export const handler = async (event) => {
7888
case "issues":
7989
await handleIssue(JSON.parse(body));
8090
break;
91+
case "pull_request":
92+
await handlePullRequest(JSON.parse(body));
93+
break;
8194
default:
8295
break;
8396
}

template.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ Resources:
3535
Architectures: [arm64]
3636
CodeUri: src/webhook-endpoint
3737
Description: >-
38-
Receives GitHub webhooks for issues and performs some actions
38+
Receives GitHub webhooks for issues and pull requests and performs
39+
some actions
3940
Environment:
4041
Variables:
4142
GITHUB_WEBHOOK_SECRET: !Ref GitHubWebhookSecret

0 commit comments

Comments
 (0)