Skip to content

Commit d5307f2

Browse files
committed
Update label management for GitHub
1 parent 389ef91 commit d5307f2

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

src/reporter/github/index.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,28 @@ export default class GitHub {
6363
}
6464

6565
const updatedExistingLabels = labelsToRemove.length ? await this.getRepositoryLabels() : existingLabels; // Refresh labels after deletion, only if needed
66-
const existingLabelsNames = updatedExistingLabels.map(label => label.name);
66+
const managedLabelsNames = this.MANAGED_LABELS.map(label => label.name);
67+
68+
// Remove managed labels that are no longer in the MANAGED_LABELS list
69+
const obsoleteManagedLabels = updatedExistingLabels.filter(label =>
70+
label.description
71+
&& label.description.includes(MANAGED_BY_OTA_MARKER)
72+
&& !managedLabelsNames.includes(label.name));
73+
74+
if (obsoleteManagedLabels.length) {
75+
logger.info(`Removing obsolete managed labels: ${obsoleteManagedLabels.map(label => `"${label.name}"`).join(', ')}`);
76+
77+
for (const label of obsoleteManagedLabels) {
78+
await this.deleteLabel(label.name); /* eslint-disable-line no-await-in-loop */
79+
}
80+
}
81+
82+
// Refresh labels after obsolete removal
83+
const finalExistingLabels = obsoleteManagedLabels.length ? await this.getRepositoryLabels() : updatedExistingLabels;
84+
const existingLabelsNames = finalExistingLabels.map(label => label.name);
85+
const existingLabelsMap = new Map(finalExistingLabels.map(label => [ label.name, label ]));
86+
87+
// Find labels that need to be created
6788
const missingLabels = this.MANAGED_LABELS.filter(label => !existingLabelsNames.includes(label.name));
6889

6990
if (missingLabels.length) {
@@ -77,6 +98,31 @@ export default class GitHub {
7798
});
7899
}
79100
}
101+
102+
// Update existing labels if description changed
103+
const labelsToUpdate = this.MANAGED_LABELS.filter(label => {
104+
const existingLabel = existingLabelsMap.get(label.name);
105+
106+
if (!existingLabel) {
107+
return false;
108+
}
109+
110+
const expectedDescription = `${label.description} ${MANAGED_BY_OTA_MARKER}`;
111+
112+
return existingLabel.description !== expectedDescription || existingLabel.color !== label.color;
113+
});
114+
115+
if (labelsToUpdate.length) {
116+
logger.info(`Updating labels with changed descriptions: ${labelsToUpdate.map(label => `"${label.name}"`).join(', ')}`);
117+
118+
for (const label of labelsToUpdate) {
119+
await this.updateLabel({ /* eslint-disable-line no-await-in-loop */
120+
name: label.name,
121+
color: label.color,
122+
description: `${label.description} ${MANAGED_BY_OTA_MARKER}`,
123+
});
124+
}
125+
}
80126
} catch (error) {
81127
logger.error(`Failed to handle repository labels: ${error.message}`);
82128
}
@@ -134,6 +180,15 @@ export default class GitHub {
134180
});
135181
}
136182

183+
async updateLabel({ name, color, description }) {
184+
await this.octokit.request('PATCH /repos/{owner}/{repo}/labels/{name}', {
185+
...this.commonParams,
186+
name,
187+
color,
188+
description,
189+
});
190+
}
191+
137192
async getIssue(title) {
138193
return (await this.issues).get(title);
139194
}

0 commit comments

Comments
 (0)