Skip to content

Commit 7249355

Browse files
authored
feat: new clear-labels argument (#60)
* update test data * update test data * add rate limit * build code * lower sleep time to 30 seconds * remove duplicated code * fix delete endpoint * create all labels if clear argument exists
1 parent 202ea05 commit 7249355

414 files changed

Lines changed: 9095 additions & 1982 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/labels.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
with:
2121
ws-dir: 'test-data'
2222
- name: Bit Pull Request
23-
uses: bit-tasks/pull-request@main
23+
uses: bit-tasks/pull-request@update-test-data-4
2424
with:
2525
version-labels: true
26+
clear-labels: true

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ inputs:
2222
description: "Specify the hex color code for automatically created patch version labels."
2323
required: false
2424
default: "c2e0c6"
25+
clear-labels:
26+
description: "Remove all Bit labels from the Pull Request"
27+
required: false
28+
default: "false"
2529
runs:
2630
using: 'node20'
2731
main: 'dist/index.js'

dist/index.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11088,7 +11088,7 @@ function paginatedRequest(opts) {
1108811088
return results;
1108911089
});
1109011090
}
11091-
const createVersionLabels = (githubToken, repo, owner, prNumber, status, versionLabelsColors) => __awaiter(void 0, void 0, void 0, function* () {
11091+
const createVersionLabels = (githubToken, repo, owner, prNumber, status, versionLabelsColors, clearLabels) => __awaiter(void 0, void 0, void 0, function* () {
1109211092
var _a, e_1, _b, _c;
1109311093
core.info("Creating version labels for new and modified components");
1109411094
const versionLabels = [
@@ -11123,6 +11123,22 @@ const createVersionLabels = (githubToken, repo, owner, prNumber, status, version
1112311123
repo,
1112411124
params: {},
1112511125
});
11126+
if (clearLabels) {
11127+
core.info("Clearing all Bit labels from the Pull Request");
11128+
// Remove all Bit labels from the Pull Request
11129+
for (const label of repoLabels) {
11130+
if (label.name.endsWith("@patch") ||
11131+
label.name.endsWith("@major") ||
11132+
label.name.endsWith("@minor")) {
11133+
core.info(`Removing Bit label: ${label.name}`);
11134+
yield octokit.request("DELETE /repos/{owner}/{repo}/labels/{name}", {
11135+
owner,
11136+
repo,
11137+
name: label.name,
11138+
});
11139+
}
11140+
}
11141+
}
1112611142
// Define the version pattern
1112711143
const componentVersionPattern = /@(major|minor|patch)$/;
1112811144
// Identify labels to remove
@@ -11145,8 +11161,11 @@ const createVersionLabels = (githubToken, repo, owner, prNumber, status, version
1114511161
}
1114611162
}
1114711163
// Determine which labels need to be created in the repository
11148-
const newLabelsToCreate = versionLabels.filter(({ name }) => !repoLabels.some((label) => label.name === name) // Labels not in the repository
11149-
);
11164+
const newLabelsToCreate = clearLabels
11165+
? // if clearing labels, create all labels again
11166+
versionLabels
11167+
: versionLabels.filter(({ name }) => !repoLabels.some((label) => label.name === name) // Labels not in the repository
11168+
);
1115011169
core.info(`Creating ${newLabelsToCreate.length} new labels in the repository`);
1115111170
try {
1115211171
// Create GitHub labels if they do not exist
@@ -11169,6 +11188,22 @@ const createVersionLabels = (githubToken, repo, owner, prNumber, status, version
1116911188
});
1117011189
}
1117111190
catch (error) {
11191+
// Handle rate limit errors
11192+
if (error.message.includes("rate limit")) {
11193+
core.info(`Waiting 30 seconds before retrying to create label ${name}: ${error.message}`);
11194+
yield new Promise((resolve) => setTimeout(resolve, 30000));
11195+
yield octokit.request("POST /repos/{owner}/{repo}/labels", {
11196+
owner,
11197+
repo,
11198+
name: name,
11199+
color: color,
11200+
description: description,
11201+
headers: {
11202+
"X-GitHub-Api-Version": "2022-11-28",
11203+
},
11204+
});
11205+
continue;
11206+
}
1117211207
// Handle unexpected errors
1117311208
core.info(`Skipped creating label ${name}: ${error.message}`);
1117411209
}
@@ -11213,7 +11248,7 @@ function run(githubToken, repo, owner, prNumber, laneName, versionLabel, version
1121311248
}); // Avoid log param, since output is parsed for next steps
1121411249
const status = JSON.parse(statusRaw.trim());
1121511250
if (versionLabel) {
11216-
yield createVersionLabels(githubToken, repo, owner, prNumber, status, versionLabelsColors);
11251+
yield createVersionLabels(githubToken, repo, owner, prNumber, status, versionLabelsColors, core.getBooleanInput("clear-labels"));
1121711252
}
1121811253
yield (0, exec_1.exec)("bit", ["lane", "create", laneName, ...args], { cwd: wsDir });
1121911254
const snapMessageText = yield createSnapMessageText(githubToken, repo, owner, prNumber);

scripts/pull-request.ts

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ const createVersionLabels = async (
153153
owner: string,
154154
prNumber: number,
155155
status: any,
156-
versionLabelsColors: { patch: string; minor: string; major: string }
156+
versionLabelsColors: { patch: string; minor: string; major: string },
157+
clearLabels: boolean
157158
) => {
158159
core.info("Creating version labels for new and modified components");
159160

@@ -198,6 +199,26 @@ const createVersionLabels = async (
198199
params: {},
199200
});
200201

202+
if (clearLabels) {
203+
core.info("Clearing all Bit labels from the Pull Request");
204+
205+
// Remove all Bit labels from the Pull Request
206+
for (const label of repoLabels) {
207+
if (
208+
label.name.endsWith("@patch") ||
209+
label.name.endsWith("@major") ||
210+
label.name.endsWith("@minor")
211+
) {
212+
core.info(`Removing Bit label: ${label.name}`);
213+
await octokit.request("DELETE /repos/{owner}/{repo}/labels/{name}", {
214+
owner,
215+
repo,
216+
name: label.name,
217+
});
218+
}
219+
}
220+
}
221+
201222
// Define the version pattern
202223
const componentVersionPattern = /@(major|minor|patch)$/;
203224
// Identify labels to remove
@@ -229,9 +250,12 @@ const createVersionLabels = async (
229250
}
230251

231252
// Determine which labels need to be created in the repository
232-
const newLabelsToCreate = versionLabels.filter(
233-
({ name }) => !repoLabels.some((label) => label.name === name) // Labels not in the repository
234-
);
253+
const newLabelsToCreate = clearLabels
254+
? // if clearing labels, create all labels again
255+
versionLabels
256+
: versionLabels.filter(
257+
({ name }) => !repoLabels.some((label) => label.name === name) // Labels not in the repository
258+
);
235259

236260
core.info(
237261
`Creating ${newLabelsToCreate.length} new labels in the repository`
@@ -254,6 +278,25 @@ const createVersionLabels = async (
254278
},
255279
});
256280
} catch (error: any) {
281+
// Handle rate limit errors
282+
if (error.message.includes("rate limit")) {
283+
core.info(
284+
`Waiting 30 seconds before retrying to create label ${name}: ${error.message}`
285+
);
286+
await new Promise((resolve) => setTimeout(resolve, 30000));
287+
await octokit.request("POST /repos/{owner}/{repo}/labels", {
288+
owner,
289+
repo,
290+
name: name,
291+
color: color,
292+
description: description,
293+
headers: {
294+
"X-GitHub-Api-Version": "2022-11-28",
295+
},
296+
});
297+
continue;
298+
}
299+
257300
// Handle unexpected errors
258301
core.info(`Skipped creating label ${name}: ${error.message}`);
259302
}
@@ -305,7 +348,8 @@ export default async function run(
305348
owner,
306349
prNumber,
307350
status,
308-
versionLabelsColors
351+
versionLabelsColors,
352+
core.getBooleanInput("clear-labels")
309353
);
310354
}
311355

0 commit comments

Comments
 (0)