Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix set label to translation pages #2

Merged
merged 12 commits into from
Nov 24, 2021
Merged
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ jobs:
with:
fetch-depth: 0
- run: npm ci
- run: npm run test
- run: npm run build
4 changes: 4 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm test
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12.22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure we can't use anything more modern here? The latest LTS version is 16.x.....

Copy link
Contributor Author

@navarroaxel navarroaxel Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, the actions run in node12 only.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though this only applies for local environments, and does not change anything about the action environment itself, which is controlled in the action.yml file.

However, again, making it so that only people with node12 (or want to switch to it) seems rather heavy handed to ensure node12 compliance...

190 changes: 107 additions & 83 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8274,110 +8274,116 @@ function wrappy (fn, cb) {
/***/ }),

/***/ 8919:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
navarroaxel marked this conversation as resolved.
Show resolved Hide resolved
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.run = exports.main = void 0;
const core = __importStar(__nccwpck_require__(2186));
const github = __importStar(__nccwpck_require__(5438));
const getChangedFiles = async (client, prNumber) => {
const listFilesOptions = client.rest.pulls.listFiles.endpoint.merge({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
exports.run = exports.main = exports.getFileLabel = exports.LabelType = exports.FileStatus = void 0;
const core_1 = __nccwpck_require__(2186);
const github_1 = __nccwpck_require__(5438);
const util_1 = __nccwpck_require__(2629);
var FileStatus;
(function (FileStatus) {
FileStatus["added"] = "added";
FileStatus["modified"] = "modified";
FileStatus["removed"] = "removed";
FileStatus["renamed"] = "renamed";
})(FileStatus = exports.FileStatus || (exports.FileStatus = {}));
var LabelType;
(function (LabelType) {
LabelType["community"] = "community";
LabelType["documentation"] = "documentation";
LabelType["massChanges"] = "mass changes";
LabelType["newCommand"] = "new command";
LabelType["pageEdit"] = "page edit";
LabelType["tooling"] = "tooling";
LabelType["translation"] = "translation";
LabelType["waiting"] = "waiting";
})(LabelType = exports.LabelType || (exports.LabelType = {}));
const communityRegex = /^MAINTAINERS\.md$/;
const documentationRegex = /\.md$/i;
const mainPageRegex = /^pages\//;
const toolingRegex = /\.([jt]s|py|sh|yml)$/;
const translationPageRegex = /^pages\.[a-z_]+\//i;
const getChangedFiles = async (octokit, prNumber) => {
const listFilesOptions = octokit.rest.pulls.listFiles.endpoint.merge({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
pull_number: prNumber,
});
return client.paginate(listFilesOptions);
return octokit.paginate(listFilesOptions);
};
const getPrLabels = async (client, prNumber) => {
const getPrOptions = client.rest.pulls.get.endpoint.merge({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
const getPrLabels = async (octokit, prNumber) => {
const getPrOptions = octokit.rest.pulls.get.endpoint.merge({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
pull_number: prNumber,
});
const prResponse = await client.request(getPrOptions);
return new Set(prResponse.data.labels.map((label) => label.name));
const prResponse = await octokit.request(getPrOptions);
return (0, util_1.uniq)(prResponse.data.labels.map((label) => label.name));
};
const addLabels = async (client, prNumber, labels) => {
await client.rest.issues.addLabels({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
const addLabels = async (octokit, prNumber, labels) => {
await octokit.rest.issues.addLabels({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
issue_number: prNumber,
labels: [...labels],
labels,
});
};
const removeLabels = async (client, prNumber, labels) => {
await Promise.all([...labels].map((label) => client.rest.issues.removeLabel({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
const removeLabels = async (octokit, prNumber, labels) => {
await Promise.all(labels.map((name) => octokit.rest.issues.removeLabel({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
issue_number: prNumber,
name: label,
name,
})));
};
const getFileLabel = (file) => {
if (mainPageRegex.test(file.filename) || (file.previous_filename && mainPageRegex.test(file.previous_filename))) {
if (file.status === FileStatus.added) {
return LabelType.newCommand;
}
if ([FileStatus.modified, FileStatus.removed, FileStatus.renamed].includes(file.status)) {
return LabelType.pageEdit;
}
}
if (translationPageRegex.test(file.filename) || (file.previous_filename && translationPageRegex.test(file.previous_filename))) {
return LabelType.translation;
}
if (communityRegex.test(file.filename)) {
return LabelType.community;
}
if (documentationRegex.test(file.filename)) {
return LabelType.documentation;
}
if (toolingRegex.test(file.filename)) {
return LabelType.tooling;
}
return null;
};
exports.getFileLabel = getFileLabel;
const main = async () => {
var _a;
const token = core.getInput('token', { required: true });
const prNumber = (_a = github.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number;
const token = (0, core_1.getInput)('token', { required: true });
const prNumber = (_a = github_1.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number;
if (!prNumber) {
console.log('Could not determine PR number, skipping');
return;
}
const client = github.getOctokit(token);
const changedFiles = await getChangedFiles(client, prNumber);
const labels = new Set();
const translatedPagePattern = /pages\.[a-z_]+/i;
const pagePattern = /pages(?:\.[a-z_]+)?/i;
const documentationPattern = /[a-z\-]+\.md/i;
const toolingPattern = /.*\.([jt]s|py|sh|yml)/i;
for (const file of changedFiles) {
if (pagePattern.test(file.filename) || (file.previous_filename && pagePattern.test(file.previous_filename))) {
if (translatedPagePattern.test(file.filename) || (file.previous_filename && translatedPagePattern.test(file.previous_filename))) {
labels.add('translation');
}
if (file.status === 'added') {
labels.add('new command');
}
else if (['modified', 'renamed', 'removed'].includes(file.status)) {
labels.add('page edit');
}
}
else if (documentationPattern.test(file.filename)) {
labels.add('documentation');
}
else if (toolingPattern.test(file.filename)) {
labels.add('tooling');
}
}
const prLabels = await getPrLabels(client, prNumber);
const labelsToAdd = new Set([...labels].filter((label) => !prLabels.has(label)));
const labelsToRemove = new Set([...prLabels].filter((label) => !labels.has(label)));
if (labelsToAdd.size) {
console.log(`Labels to add: ${[...labelsToAdd].join(', ')}`);
await addLabels(client, prNumber, labelsToAdd);
const octokit = (0, github_1.getOctokit)(token);
const changedFiles = await getChangedFiles(octokit, prNumber);
const labels = (0, util_1.uniq)(changedFiles.map(file => (0, exports.getFileLabel)(file)).filter((label) => label !== null));
const prLabels = await getPrLabels(octokit, prNumber);
const labelsToAdd = labels.filter((label) => !prLabels.includes(label));
const extraPrLabels = prLabels.filter((label) => !labels.includes(label));
if (labelsToAdd.length) {
console.log(`Labels to add: ${labelsToAdd.join(', ')}`);
await addLabels(octokit, prNumber, labelsToAdd);
}
if (labelsToRemove.size) {
console.log(`Labels to remove: ${[...labelsToRemove].join(', ')}`);
await removeLabels(client, prNumber, labelsToRemove);
if (extraPrLabels.includes(LabelType.waiting)) {
console.log(`Labels to remove: ${LabelType.waiting}`);
await removeLabels(octokit, prNumber, [LabelType.waiting]);
}
};
exports.main = main;
Expand All @@ -8386,13 +8392,31 @@ const run = async () => {
await (0, exports.main)();
}
catch (err) {
core.error(err);
core.setFailed(err.message);
(0, core_1.error)(err);
(0, core_1.setFailed)(err.message);
}
};
exports.run = run;


/***/ }),

/***/ 2629:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.uniq = void 0;
/**
* Creates a duplicate-free version of an array.
* @param array {Array} The array to inspect.
* @returns {Array} The new duplicate free array.
*/
const uniq = (array) => Array.from(new Set(array));
exports.uniq = uniq;


/***/ }),

/***/ 2877:
Expand Down
Loading