Skip to content

Commit 26263bd

Browse files
committed
make a PR comment
1 parent 5bb15c7 commit 26263bd

File tree

4 files changed

+396
-28
lines changed

4 files changed

+396
-28
lines changed

action.yml

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ branding:
44
icon: 'arrow-up-circle'
55
color: 'blue'
66
inputs:
7+
token:
8+
description: 'The GitHub Token to use for deployment'
9+
required: false
10+
default: ${{ github.token }}
711
region:
812
description: 'The AWS Region to deploy to'
913
required: false

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"@aws-sdk/client-sts": "^3.533.0",
3737
"axios": "^1.6.7",
3838
"boolean": "^3.2.0",
39-
"jose": "^5.2.3"
39+
"jose": "^5.2.3",
40+
"octokit": "^3.1.2"
4041
},
4142
"devDependencies": {
4243
"@babel/core": "^7.16.0",
@@ -65,4 +66,4 @@
6566
"webpack-cli": "^4.9.1",
6667
"webpack-node-externals": "^3.0.0"
6768
}
68-
}
69+
}

src/action.ts

+86-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
import { debug, notice, getIDToken, exportVariable, info, setOutput } from '@actions/core';
1+
import {
2+
debug,
3+
notice,
4+
getIDToken,
5+
exportVariable,
6+
info,
7+
setOutput,
8+
saveState,
9+
getState,
10+
} from '@actions/core';
211
import { getInput } from '@actions/core';
3-
import { context } from '@actions/github';
12+
import { context, getOctokit } from '@actions/github';
413
import { warn } from 'console';
514
import fs from 'fs';
615
import path from 'path';
@@ -30,23 +39,8 @@ export class Action {
3039
const region = getInput('region') || 'us-east-1';
3140
const role = getInput('role');
3241
const [owner, repo] = GITHUB_REPOSITORY?.split('/') || [];
33-
const [, branchType, branchId] = GITHUB_REF?.split('/') || [];
34-
35-
if (!branchId) {
36-
debug(`GITHUB_REF: ${GITHUB_REF}`);
37-
debug(`branchType: ${branchType}`);
38-
debug(`branchId: ${branchId}`);
39-
throw new Error('Unable to determine branch from GITHUB_REF');
40-
}
4142

42-
let deploymentStage = branchId;
43-
if (branchType === 'pull') {
44-
if (!GITHUB_BASE_REF) {
45-
throw new Error('Unable to determine base ref from GITHUB_BASE_REF');
46-
}
47-
deploymentStage = `${GITHUB_BASE_REF}-pr-${branchId}`;
48-
}
49-
setOutput('stage', deploymentStage);
43+
setOutput('stage', this.stage);
5044

5145
let deploy = true;
5246
let destroy = false;
@@ -59,6 +53,7 @@ export class Action {
5953
destroy = true;
6054
}
6155
setOutput('deploy', deploy);
56+
saveState('destroy', destroy);
6257
setOutput('destroy', destroy);
6358

6459
let idToken: string | undefined = undefined;
@@ -115,11 +110,82 @@ export class Action {
115110
}
116111
}
117112

113+
get stage(): string {
114+
const [, branchType, branchId] = GITHUB_REF?.split('/') || [];
115+
116+
if (!branchId) {
117+
debug(`GITHUB_REF: ${GITHUB_REF}`);
118+
debug(`branchType: ${branchType}`);
119+
debug(`branchId: ${branchId}`);
120+
throw new Error('Unable to determine branch from GITHUB_REF');
121+
}
122+
123+
let deploymentStage = branchId;
124+
if (branchType === 'pull') {
125+
if (!GITHUB_BASE_REF) {
126+
throw new Error('Unable to determine base ref from GITHUB_BASE_REF');
127+
}
128+
deploymentStage = `${GITHUB_BASE_REF}-pr-${branchId}`;
129+
}
130+
131+
return deploymentStage;
132+
}
133+
134+
get token(): string {
135+
const token = getInput('token');
136+
if (!token) {
137+
throw new Error('Missing GITHUB_TOKEN');
138+
}
139+
return token;
140+
}
141+
142+
get prComment(): string {
143+
return `
144+
[${this.commitSha}](https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${this.commitSha} has been deployed!
145+
- **Commit:** \`${this.commitSha}\`
146+
- **Stage:** \`${this.stage}\`
147+
- **URL:** [${this.httpApiUrl}](${this.httpApiUrl})
148+
`;
149+
}
150+
151+
get commitSha(): string {
152+
if (context.eventName === 'pull_request') {
153+
return `${context.payload.pull_request?.head.sha}`.substring(0, 7);
154+
}
155+
return context.sha.substring(0, 7);
156+
}
157+
158+
get prNumber(): number | undefined {
159+
if (context.eventName === 'pull_request') {
160+
return context.payload.pull_request?.number;
161+
}
162+
return undefined;
163+
}
164+
165+
async addPrComments(): Promise<void> {
166+
const destroy = boolean(getState('destroy'));
167+
if (destroy) {
168+
return;
169+
}
170+
171+
const { prNumber } = this;
172+
if (!prNumber) {
173+
return;
174+
}
175+
176+
const octokit = getOctokit(this.token);
177+
await octokit.rest.issues.createComment({
178+
body: this.prComment,
179+
owner: context.repo.owner,
180+
repo: context.repo.repo,
181+
issue_number: prNumber,
182+
});
183+
}
184+
118185
async post(): Promise<void> {
119186
const httpApiUrl = await this.httpApiUrl;
120187

121-
// TODO: Detect branch/PR delete/close and remove the stage
122-
188+
await this.addPrComments();
123189
notice(`HTTP API URL: ${httpApiUrl}`);
124190
}
125191

0 commit comments

Comments
 (0)