Skip to content

Commit b2a1566

Browse files
committed
Handle error and logging to higher-level functions
Prevent duplicate GitHub issues by stopping on getIssue failures
1 parent 51d710e commit b2a1566

File tree

1 file changed

+51
-35
lines changed

1 file changed

+51
-35
lines changed

src/reporter/github.js

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,24 @@ export default class GitHub {
3232

3333
async initialize() {
3434
this.MANAGED_LABELS = require('./labels.json');
35-
36-
const existingLabels = await this.getRepositoryLabels();
37-
const existingLabelsNames = existingLabels.map(label => label.name);
38-
const missingLabels = this.MANAGED_LABELS.filter(label => !existingLabelsNames.includes(label.name));
39-
40-
if (missingLabels.length) {
41-
logger.info(`🤖 Following required labels are not present on the repository: ${missingLabels.map(label => `"${label.name}"`).join(', ')}. Creating them…`);
42-
43-
for (const label of missingLabels) {
44-
await this.createLabel({ /* eslint-disable-line no-await-in-loop */
45-
name: label.name,
46-
color: label.color,
47-
description: `${label.description} ${MANAGED_BY_OTA_MARKER}`,
48-
});
35+
try {
36+
const existingLabels = await this.getRepositoryLabels();
37+
const existingLabelsNames = existingLabels.map(label => label.name);
38+
const missingLabels = this.MANAGED_LABELS.filter(label => !existingLabelsNames.includes(label.name));
39+
40+
if (missingLabels.length) {
41+
logger.info(`🤖 Following required labels are not present on the repository: ${missingLabels.map(label => `"${label.name}"`).join(', ')}. Creating them…`);
42+
43+
for (const label of missingLabels) {
44+
await this.createLabel({ /* eslint-disable-line no-await-in-loop */
45+
name: label.name,
46+
color: label.color,
47+
description: `${label.description} ${MANAGED_BY_OTA_MARKER}`,
48+
});
49+
}
4950
}
51+
} catch (error) {
52+
logger.error(`🤖 Failed to handle repository labels: ${error.message}`);
5053
}
5154
}
5255

@@ -123,38 +126,51 @@ export default class GitHub {
123126
}
124127

125128
async closeIssueWithCommentIfExists({ title, comment }) {
126-
const openedIssue = await this.getIssue({ title, state: GitHub.ISSUE_STATE_OPEN });
129+
try {
130+
const openedIssue = await this.getIssue({ title, state: GitHub.ISSUE_STATE_OPEN });
127131

128-
if (!openedIssue) {
129-
return;
130-
}
132+
if (!openedIssue) {
133+
return;
134+
}
131135

132-
await this.addCommentToIssue({ issue: openedIssue, comment });
136+
await this.addCommentToIssue({ issue: openedIssue, comment });
137+
await this.closeIssue(openedIssue);
133138

134-
return this.closeIssue(openedIssue);
139+
return logger.info(`🤖 Closed issue #${openedIssue.number}: ${openedIssue.html_url}`);
140+
} catch (error) {
141+
logger.error(`🤖 Failed to handle issue "${title}": ${error.message}`);
142+
}
135143
}
136144

137145
async createOrUpdateIssue({ title, description, label }) {
138-
const issue = await this.getIssue({ title, state: GitHub.ISSUE_STATE_ALL });
146+
try {
147+
const issue = await this.getIssue({ title, state: GitHub.ISSUE_STATE_ALL });
139148

140-
if (!issue) {
141-
return this.createIssue({ title, description, labels: [label] });
142-
}
149+
if (!issue) {
150+
const createdIssue = await this.createIssue({ title, description, labels: [label] });
143151

144-
if (issue.state == GitHub.ISSUE_STATE_CLOSED) {
145-
await this.openIssue(issue);
146-
}
152+
return logger.info(`🤖 Created GitHub issue #${createdIssue.number} "${title}": ${createdIssue.html_url}`);
153+
}
147154

148-
const managedLabelsNames = this.MANAGED_LABELS.map(label => label.name);
149-
const [managedLabel] = issue.labels.filter(label => managedLabelsNames.includes(label.name)); // it is assumed that only one specific reason for failure is possible at a time, making managed labels mutually exclusive
155+
if (issue.state == GitHub.ISSUE_STATE_CLOSED) {
156+
await this.openIssue(issue);
157+
logger.info(`🤖 Reopen issue #${issue.number}: ${issue.html_url}`);
158+
}
150159

151-
if (managedLabel?.name == label) { // if the label is already assigned to the issue, the error is redundant with the one already reported and no further action is necessary
152-
return;
153-
}
160+
const managedLabelsNames = this.MANAGED_LABELS.map(label => label.name);
161+
const [managedLabel] = issue.labels.filter(label => managedLabelsNames.includes(label.name)); // it is assumed that only one specific reason for failure is possible at a time, making managed labels mutually exclusive
154162

155-
const labelsNotManagedToKeep = issue.labels.map(label => label.name).filter(label => !managedLabelsNames.includes(label));
163+
if (managedLabel?.name == label) { // if the label is already assigned to the issue, the error is redundant with the one already reported and no further action is necessary
164+
return;
165+
}
166+
167+
const labelsNotManagedToKeep = issue.labels.map(label => label.name).filter(label => !managedLabelsNames.includes(label));
156168

157-
await this.setIssueLabels({ issue, labels: [ label, ...labelsNotManagedToKeep ] });
158-
await this.addCommentToIssue({ issue, comment: description });
169+
await this.setIssueLabels({ issue, labels: [ label, ...labelsNotManagedToKeep ] });
170+
await this.addCommentToIssue({ issue, comment: description });
171+
logger.info(`🤖 Updated issue #${issue.number}: ${issue.html_url}`);
172+
} catch (error) {
173+
logger.error(`🤖 Failed to handle issue "${title}": ${error.message}`);
174+
}
159175
}
160176
}

0 commit comments

Comments
 (0)