Skip to content

Commit b559d9d

Browse files
authored
feat: add main branch head commit sha to deployment payload (#2)
1 parent 3ee05a2 commit b559d9d

File tree

6 files changed

+67
-9
lines changed

6 files changed

+67
-9
lines changed

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ inputs:
2626
environment_url:
2727
required: false
2828
description: link for the deployment (e.g. http://staging.your.app/status)
29+
main_branch:
30+
required: false
31+
description: name of main branch of the repo, to get and store sha of the head commit and track diff being deployed
2932
runs:
3033
using: 'node12'
3134
main: 'dist/index.js'

dist/index.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3542,7 +3542,7 @@ function getInput(name, options) {
35423542
return result;
35433543
}
35443544
function run() {
3545-
var _a, _b, _c, _d, _e, _f, _g;
3545+
var _a, _b, _c, _d, _e, _f, _g, _h;
35463546
return __awaiter(this, void 0, void 0, function* () {
35473547
let token;
35483548
let type;
@@ -3552,6 +3552,7 @@ function run() {
35523552
let environment;
35533553
let environmentUrl;
35543554
let deploymentId;
3555+
let mainBranch;
35553556
const { actor, ref } = github.context;
35563557
console.log('### context ###');
35573558
console.log(`actor: ${actor}`);
@@ -3573,8 +3574,10 @@ function run() {
35733574
console.log(`environment: ${environment}`);
35743575
environmentUrl = (_f = getInput('environment_url')) !== null && _f !== void 0 ? _f : '';
35753576
console.log(`environmentUrl: ${environmentUrl}`);
3577+
mainBranch = (_g = getInput('main_branch')) !== null && _g !== void 0 ? _g : 'master';
3578+
console.log(`main branch: ${mainBranch}`);
35763579
const shouldRequireDeploymentId = type === 'finish' || type === 'delete';
3577-
deploymentId = (_g = getInput('deployment_id', { required: shouldRequireDeploymentId })) !== null && _g !== void 0 ? _g : '0';
3580+
deploymentId = (_h = getInput('deployment_id', { required: shouldRequireDeploymentId })) !== null && _h !== void 0 ? _h : '0';
35783581
console.log(`deploymentId: ${deploymentId}`);
35793582
}
35803583
catch (error) {
@@ -3588,7 +3591,7 @@ function run() {
35883591
switch (type) {
35893592
case 'create':
35903593
try {
3591-
deploymentId = yield create_1.create(client, logsUrl, description, status, environment, environmentUrl);
3594+
deploymentId = yield create_1.create(client, logsUrl, description, status, environment, environmentUrl, mainBranch);
35923595
console.log(`setOutput::deployment_id: ${deploymentId}`);
35933596
core.setOutput('deployment_id', deploymentId);
35943597
}
@@ -8670,10 +8673,28 @@ function invalidatePreviousDeployments(client, environment) {
86708673
})));
86718674
});
86728675
}
8673-
function create(client, logUrl, description, initialStatus, environment, environmentUrl) {
8676+
function getMainSha(client, branch) {
8677+
return __awaiter(this, void 0, void 0, function* () {
8678+
try {
8679+
const response = yield client.repos.getBranch(Object.assign(Object.assign({}, github_1.context.repo), { branch }));
8680+
const sha = response.data.commit.sha;
8681+
console.log(`${branch} branch sha: ${sha}`);
8682+
return sha;
8683+
}
8684+
catch (error) {
8685+
console.error(error.message);
8686+
return `no_${branch}`;
8687+
}
8688+
});
8689+
}
8690+
function create(client, logUrl, description, initialStatus, environment, environmentUrl, mainBranch) {
86748691
return __awaiter(this, void 0, void 0, function* () {
86758692
yield invalidatePreviousDeployments(client, environment);
8676-
const deployment = yield client.repos.createDeployment(Object.assign(Object.assign({}, github_1.context.repo), { ref: github_1.context.ref, required_contexts: [], environment, transient_environment: true, auto_merge: false, description, payload: JSON.stringify({ actor: github_1.context.actor }) }));
8693+
// get main branch sha to store in payload
8694+
const mainBranchSha = yield getMainSha(client, mainBranch);
8695+
const payload = JSON.stringify({ actor: github_1.context.actor, main_sha: mainBranchSha });
8696+
const deployment = yield client.repos.createDeployment(Object.assign(Object.assign({}, github_1.context.repo), { ref: github_1.context.ref, required_contexts: [], environment, transient_environment: true, auto_merge: false, description,
8697+
payload }));
86778698
console.log(`created deployment: ${JSON.stringify(deployment.data, null, 2)}`);
86788699
const status = yield client.repos.createDeploymentStatus(Object.assign(Object.assign({}, github_1.context.repo), { deployment_id: deployment.data.id, state: initialStatus, log_url: logUrl, environment_url: environmentUrl }));
86798700
console.log(`created deployment status: ${JSON.stringify(status.data, null, 2)}`);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "action-deploy",
3-
"version": "0.0.1",
3+
"version": "1.0.1",
44
"private": true,
55
"description": "Action to manage GitHub deployments",
66
"main": "lib/main.js",

src/create.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,34 @@ async function invalidatePreviousDeployments (
4040
)
4141
}
4242

43+
async function getMainSha (client: GitHub, branch: string): Promise<string> {
44+
try {
45+
const response = await client.repos.getBranch({ ...context.repo, branch })
46+
const sha = response.data.commit.sha
47+
console.log(`${branch} branch sha: ${sha}`)
48+
return sha
49+
} catch (error) {
50+
console.error(error.message)
51+
return `no_${branch}`
52+
}
53+
}
54+
4355
export async function create (
4456
client: GitHub,
4557
logUrl: string,
4658
description: string,
4759
initialStatus: DeploymentStatus,
4860
environment: string,
49-
environmentUrl: string
61+
environmentUrl: string,
62+
mainBranch: string
5063
): Promise<string> {
5164
await invalidatePreviousDeployments(client, environment)
5265

66+
// get main branch sha to store in payload
67+
const mainBranchSha = await getMainSha(client, mainBranch)
68+
69+
const payload = JSON.stringify({ actor: context.actor, main_sha: mainBranchSha })
70+
5371
const deployment = await client.repos.createDeployment({
5472
...context.repo,
5573
ref: context.ref,
@@ -58,7 +76,7 @@ export async function create (
5876
transient_environment: true,
5977
auto_merge: false,
6078
description,
61-
payload: JSON.stringify({ actor: context.actor })
79+
payload
6280
})
6381

6482
console.log(`created deployment: ${JSON.stringify(deployment.data, null, 2)}`)

src/main.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export async function run (): Promise<void> {
2828
let environment: string
2929
let environmentUrl: string
3030
let deploymentId: string
31+
let mainBranch: string
3132

3233
const { actor, ref } = github.context
3334

@@ -59,6 +60,9 @@ export async function run (): Promise<void> {
5960
environmentUrl = getInput('environment_url') ?? ''
6061
console.log(`environmentUrl: ${environmentUrl}`)
6162

63+
mainBranch = getInput('main_branch') ?? 'master'
64+
console.log(`main branch: ${mainBranch}`)
65+
6266
const shouldRequireDeploymentId = type === 'finish' || type === 'delete'
6367
deploymentId = getInput('deployment_id', { required: shouldRequireDeploymentId }) ?? '0'
6468
console.log(`deploymentId: ${deploymentId}`)
@@ -82,7 +86,8 @@ export async function run (): Promise<void> {
8286
description,
8387
status,
8488
environment,
85-
environmentUrl
89+
environmentUrl,
90+
mainBranch
8691
)
8792
console.log(`setOutput::deployment_id: ${deploymentId}`)
8893
core.setOutput('deployment_id', deploymentId)

tests/main.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import nock from 'nock'
88
nock.disableNetConnect()
99

1010
const listDeploymentsReply = [] as any
11+
const getBranchReply = { commit: { sha: "fake-sha" } } as any
1112
const postDeploymentReply = { id: 42 } as any
1213
const postStatusReply = {} as any
1314

@@ -40,6 +41,10 @@ describe('create', () => {
4041
.get('/repos/owner/repo/deployments?ref=refs%2Fheads%2Fmaster&environment=master')
4142
.reply(200, listDeploymentsReply)
4243

44+
const getMainBranchSha = nock('https://api.github.com')
45+
.get('/repos/owner/repo/branches/master')
46+
.reply(200, getBranchReply)
47+
4348
const postDeployment = nock('https://api.github.com')
4449
.post('/repos/owner/repo/deployments')
4550
.reply(200, postDeploymentReply)
@@ -53,6 +58,7 @@ describe('create', () => {
5358

5459
// assert
5560
getListDeployments.done()
61+
getMainBranchSha.done()
5662
postDeployment.done()
5763
postStatus.done()
5864
})
@@ -73,6 +79,10 @@ describe('create', () => {
7379
.get('/repos/owner/repo/deployments?ref=refs%2Fheads%2Fmaster&environment=master')
7480
.reply(200, listDeploymentsReply)
7581

82+
const getMainBranchSha = nock('https://api.github.com')
83+
.get('/repos/owner/repo/branches/master')
84+
.reply(200, getBranchReply)
85+
7686
const postDeployment = nock('https://api.github.com')
7787
.post('/repos/owner/repo/deployments')
7888
.reply(400, {"resource":"DeploymentStatus","code":"custom","field":"environment_url","message":"environment_url must use http(s) scheme"})
@@ -88,6 +98,7 @@ describe('create', () => {
8898
// assert
8999
getListDeployments.done()
90100
postDeployment.done()
101+
getMainBranchSha.done()
91102
expect(setFailedSpy.mock.calls).toHaveLength(1)
92103
})
93104
})

0 commit comments

Comments
 (0)