Repository navigation
Expand file tree
/
Copy pathcsat.js
More file actions
95 lines (80 loc) · 3.4 KB
/
Copy pathcsat.js
File metadata and controls
95 lines (80 loc) · 3.4 KB
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
/*
Copyright 2026 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const CONSTANT_VALUES = require('./constant');
/**
* Invoked from csat.yml workflow file to post survey link
* in closed issues.
* @param {!Object.<string,!Object>} github contains pre defined functions.
* context Information about the workflow run.
* @return {null}
*/
module.exports = async ({ github, context }) => {
if (context.payload?.issue?.state_reason && context.payload.issue.state_reason !== 'completed') {
return;
}
// Ignore stale auto-closures and inactivity closures.
const isStale = context.payload?.issue?.labels?.some(label => {
const name = (typeof label === 'string' ? label : label?.name)?.toLowerCase();
return name === 'stale';
});
const senderLogin = context.payload?.sender?.login;
const closedByLogin = context.payload?.issue?.closed_by?.login;
const isClosedByBot =
senderLogin === 'adk-bot' ||
senderLogin?.endsWith('[bot]') ||
closedByLogin === 'adk-bot' ||
closedByLogin?.endsWith('[bot]');
if (isStale || isClosedByBot) {
return;
}
const issue = context.payload.issue.html_url;
// Check if any label matches (case-insensitive) the supported CSAT labels.
const supportedLabels = Object.values(CONSTANT_VALUES.GLOBALS.LABELS);
const hasMatchingLabel = context.payload.issue.labels.some(label => {
const name = label.name.toLowerCase();
return supportedLabels.some(supportedLabel => name.includes(supportedLabel));
});
if (hasMatchingLabel) {
const issueNumber = context.issue?.number ?? context.payload.issue.number;
// Check if CSAT survey was already posted.
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
per_page: 100,
});
const hasCsatComment = comments.some(
c => c.body && c.body.includes(CONSTANT_VALUES.MODULE.CSAT.MSG)
);
if (hasCsatComment) {
console.log(`CSAT survey already posted for issue #${issueNumber}`);
return;
}
console.log(`Posting CSAT survey for issue =${issue}`);
const baseUrl = CONSTANT_VALUES.MODULE.CSAT.BASE_URL;
const yesCsat = `<a href="${baseUrl + CONSTANT_VALUES.MODULE.CSAT.SATISFACTION_PARAM +
CONSTANT_VALUES.MODULE.CSAT.YES +
CONSTANT_VALUES.MODULE.CSAT.ISSUEID_PARAM + encodeURIComponent(issue)}"> ${CONSTANT_VALUES.MODULE.CSAT.YES}</a>`;
const noCsat = `<a href="${baseUrl + CONSTANT_VALUES.MODULE.CSAT.SATISFACTION_PARAM +
CONSTANT_VALUES.MODULE.CSAT.NO +
CONSTANT_VALUES.MODULE.CSAT.ISSUEID_PARAM + encodeURIComponent(issue)}"> ${CONSTANT_VALUES.MODULE.CSAT.NO}</a>`;
const comment = CONSTANT_VALUES.MODULE.CSAT.MSG + '\n' + yesCsat + '\n' +
noCsat + '\n';
await github.rest.issues.createComment({
issue_number: issueNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
}
};