This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
114 lines (99 loc) · 3.39 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { Octokit } from '@octokit/rest';
import { Callback, Context, Handler } from 'aws-lambda';
import * as AWS from 'aws-sdk';
import { CodePipelineEvent } from './codepipeline';
import { fetchParameters, getParameter } from './ssm';
const JSON_INDENT = 2;
export const handler: Handler = async (
event: CodePipelineEvent,
_context: Context,
_callback: Callback | undefined,
) => {
const parameters = await fetchParameters();
const githubToken = getParameter(parameters, 'github_token');
if (githubToken === undefined) {
console.log('No github_token specified');
return;
}
const octokit = new Octokit({ auth: githubToken });
const codePipeline = new AWS.CodePipeline();
const execution = await codePipeline
.getPipelineExecution({
pipelineExecutionId: event.detail['execution-id'],
pipelineName: event.detail.pipeline,
})
.promise();
if (execution.pipelineExecution == null) {
console.log(
'Failed to fetch pipeline execution',
JSON.stringify(execution, null, JSON_INDENT),
);
return;
}
if (
execution.pipelineExecution.artifactRevisions == null ||
execution.pipelineExecution.artifactRevisions.length === 0
) {
console.log('No revision info found in pipeline execution');
console.log(JSON.stringify(execution, null, JSON_INDENT));
return;
}
const sha = execution.pipelineExecution.artifactRevisions[0].revisionId;
if (sha == null) {
console.log('No revisionId found in pipeline execution');
return;
}
const { revisionUrl } = execution.pipelineExecution.artifactRevisions[0];
if (revisionUrl == null) {
console.log(
'No revisionUrl found in pipeline execution',
JSON.stringify(execution, null, JSON_INDENT),
);
return;
}
const matches =
revisionUrl.match(/^https:\/\/github\.com\/([^/]+)\/([^/]+).*/) ||
revisionUrl.match(
/^https:\/\/.*\.console\.aws\.amazon\.com\/.*FullRepositoryId=([^/]+)\/([^&]+).*/,
);
if (matches == null) {
console.log(`Not a GitHub revisionUrl: ${revisionUrl}`);
return;
}
const owner = matches[1];
const repo = matches[2];
const state = ((
pipelineExecution: AWS.CodePipeline.PipelineExecution,
): 'error' | 'failure' | 'pending' | 'success' => {
switch (pipelineExecution.status) {
case 'InProgress':
return 'pending';
case 'Failed':
return 'failure';
case 'Succeeded':
return 'success';
default:
return 'error';
}
})(execution.pipelineExecution);
const description = ((): string => {
switch (event['detail-type']) {
case 'CodePipeline Pipeline Execution State Change':
return `${execution.pipelineExecution.status}: Execution ${event.detail['execution-id']}`;
case 'CodePipeline Action Execution State Change':
case 'CodePipeline Stage Execution State Change':
return `${execution.pipelineExecution.status} (${event.detail.stage}): Execution ${event.detail['execution-id']}`;
default:
throw new Error('Unknown event type');
}
})();
await octokit.repos.createCommitStatus({
description,
owner,
repo,
sha,
state,
context: `CodePipeline (${event.detail.pipeline})`,
target_url: `https://${event.region}.console.aws.amazon.com/codesuite/codepipeline/pipelines/${event.detail.pipeline}/executions/${event.detail['execution-id']}/visualization?region=${event.region}`,
});
};