generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
121 lines (95 loc) · 3.82 KB
/
index.js
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
115
116
117
118
119
120
121
const core = require('@actions/core');
const github = require('@actions/github');
const http = require('@actions/http-client');
const auth = require('@actions/http-client/lib/auth');
async function run() {
try {
const token = core.getInput('repo-token', { required: true });
const username = core.getInput('username', { required: true });
const password = core.getInput('password', { required: true });
const systemId = core.getInput('system-id', { required: true });
const tableName = core.getInput('table-name', { required: false });
const instanceName = core.getInput('instance-name', { required: false });
core.setSecret(token);
core.setSecret(username);
core.setSecret(password);
core.setSecret(systemId);
core.setSecret(tableName);
core.setSecret(instanceName);
const httpClient = new http.HttpClient(
'service-now-work-notes-github-action',
[new auth.BasicCredentialHandler(username, password)],
);
const url = `https://${instanceName}.servicenowservices.com/api/now/table/${tableName}/${systemId}`;
const octokit = github.getOctokit(token);
core.startGroup('🔔 Querying for run approvals');
const owner =
github.context.payload.repository.organization ??
github.context.payload.repository.owner.login;
const repo = github.context.payload.repository.name;
const runId = github.context.runId;
core.info(`Action run Id: ${github.context.runId}`);
core.info(
`API request: GET /repos/${owner}/${repo}/actions/runs/${runId}/approvals`,
);
const { data } = await octokit.request(
`GET /repos/${owner}/${repo}/actions/runs/${runId}/approvals`,
{
owner,
repo,
run_id: runId,
},
);
core.info(`Approvals: ${data.length}`);
if (data.length === 0) {
core.setFailed(
`No approvals found for runId: ${runId}. Does this environment require approvals? Check your environments in settings: https://github.com/${owner}/${repo}/settings/environments`,
);
return;
}
const lastAttempt = data[0];
const approver = lastAttempt.user.login;
const comments = lastAttempt.comment;
core.info(
`Approver: ${lastAttempt.user.login}, comment: ${lastAttempt.comment}`,
);
core.endGroup();
core.startGroup('🔔 Sending work note to Service Now');
let notes = `[code]<h2>🚀🚀🚀 New Deployment 🚀🚀🚀</h2>[/code]
[code]This item has been deployed using the <strong>${lastAttempt.environments[0].name}</strong> environment via a <a href="https://github.com/${owner}/${repo}/actions/runs/${runId}">GitHub Action</a> pipeline.[/code]
[code]It was <strong>${lastAttempt.state}</strong> by the GitHub user <a href="${lastAttempt.user.html_url}">${approver}</a>.[/code]`;
if (comments.length > 0) {
notes += `
The following comment was added with the approval
[code]<pre>${comments}</pre>[/code]`;
}
if (
github.context.eventName === 'release' &&
github.context.action === 'published'
) {
notes += `
The following changelog is associated with this release:
[code]<pre>${github.context.payload.release.body}</pre>[/code]
`;
}
core.info(`Sending: ${notes} to ${url}`);
const today = new Date();
const formattedDate = today.toISOString().split('T')[0];
const response = await httpClient.patchJson(url, {
work_notes: notes,
life_cycle_stage: 'Operational',
life_cycle_stage_status: 'In Use',
last_change_date: formattedDate,
});
core.info(`Service Now api response: ${response.statusCode}`);
if (response.statusCode != 200) {
core.setFailed(
`request failed:${response.statusCode}, ${response.result}`,
);
}
core.endGroup();
} catch (error) {
core.setFailed(error.message);
}
}
run();