From 321fdd1e7344d54ab8eef2b23304f79efbc45263 Mon Sep 17 00:00:00 2001 From: Peter Marathas Date: Fri, 8 Mar 2024 16:18:29 -0500 Subject: [PATCH] Allow specifying colors --- action.yml | 3 ++ dist/index.js | 124 +++++++++++++++++++++++++++++++------------------- src/main.ts | 31 ++++++++++++- 3 files changed, 109 insertions(+), 49 deletions(-) diff --git a/action.yml b/action.yml index 27dd7b6..9f9b66f 100644 --- a/action.yml +++ b/action.yml @@ -16,6 +16,9 @@ inputs: number: description: The number of the issue or pull request. required: false + colors: + description: The labels' color to be set. Separate multiple colors with line breaks, each color will be assigned to the label at the matching index. + required: false runs: using: node12 main: dist/index.js diff --git a/dist/index.js b/dist/index.js index baba32a..c2ddb37 100644 --- a/dist/index.js +++ b/dist/index.js @@ -53,12 +53,40 @@ function run() { return; } const octokit = github.getOctokit(githubToken); - yield octokit.rest.issues.addLabels({ + const { data: githubLabels } = yield octokit.rest.issues.addLabels({ labels, owner, repo, issue_number: number }); + const githubLabelsByName = githubLabels + .reduce((acc, label) => (Object.assign(Object.assign({}, acc), { [label.name]: label })), {}); + const colors = core + .getInput('colors') + .split('\n') + .filter(c => c !== '') + .slice(0, labels.length); + for (const [i, color] of colors.entries()) { + const name = labels[i]; + const githubLabel = githubLabelsByName[name]; + if (!githubLabel || githubLabel.color == color) { + continue; + } + try { + yield octokit.rest.issues.updateLabel({ + owner, + repo, + name, + color + }); + core.info(`Updated color for label "${githubLabel.name}" from #${githubLabel.color} to #${color}`); + } + catch (e) { + if (e instanceof Error) { + core.error(e); + } + } + } } catch (e) { if (e instanceof Error) { @@ -3760,7 +3788,7 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau var endpoint = __nccwpck_require__(9440); var universalUserAgent = __nccwpck_require__(5030); -var isPlainObject = __nccwpck_require__(9062); +var isPlainObject = __nccwpck_require__(3287); var nodeFetch = _interopDefault(__nccwpck_require__(467)); var requestError = __nccwpck_require__(537); @@ -3931,52 +3959,6 @@ exports.request = request; //# sourceMappingURL=index.js.map -/***/ }), - -/***/ 9062: -/***/ ((__unused_webpack_module, exports) => { - -"use strict"; - - -Object.defineProperty(exports, "__esModule", ({ value: true })); - -/*! - * is-plain-object - * - * Copyright (c) 2014-2017, Jon Schlinkert. - * Released under the MIT License. - */ - -function isObject(o) { - return Object.prototype.toString.call(o) === '[object Object]'; -} - -function isPlainObject(o) { - var ctor,prot; - - if (isObject(o) === false) return false; - - // If has modified constructor - ctor = o.constructor; - if (ctor === undefined) return true; - - // If has modified prototype - prot = ctor.prototype; - if (isObject(prot) === false) return false; - - // If constructor does not have an Object-specific method - if (prot.hasOwnProperty('isPrototypeOf') === false) { - return false; - } - - // Most likely a plain Object - return true; -} - -exports.isPlainObject = isPlainObject; - - /***/ }), /***/ 3682: @@ -6750,6 +6732,52 @@ module.exports.array = (stream, options) => getStream(stream, Object.assign({}, module.exports.MaxBufferError = MaxBufferError; +/***/ }), + +/***/ 3287: +/***/ ((__unused_webpack_module, exports) => { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", ({ value: true })); + +/*! + * is-plain-object + * + * Copyright (c) 2014-2017, Jon Schlinkert. + * Released under the MIT License. + */ + +function isObject(o) { + return Object.prototype.toString.call(o) === '[object Object]'; +} + +function isPlainObject(o) { + var ctor,prot; + + if (isObject(o) === false) return false; + + // If has modified constructor + ctor = o.constructor; + if (ctor === undefined) return true; + + // If has modified prototype + prot = ctor.prototype; + if (isObject(prot) === false) return false; + + // If constructor does not have an Object-specific method + if (prot.hasOwnProperty('isPrototypeOf') === false) { + return false; + } + + // Most likely a plain Object + return true; +} + +exports.isPlainObject = isPlainObject; + + /***/ }), /***/ 1554: diff --git a/src/main.ts b/src/main.ts index 455c68a..fffe86b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -20,12 +20,41 @@ async function run(): Promise { } const octokit = github.getOctokit(githubToken); - await octokit.rest.issues.addLabels({ + const { data: githubLabels } = await octokit.rest.issues.addLabels({ labels, owner, repo, issue_number: number }); + const githubLabelsByName: { [label: string]: (typeof githubLabels)[0] } = githubLabels + .reduce((acc, label) => ({ ...acc, [label.name]: label }), {}); + + const colors = core + .getInput('colors') + .split('\n') + .filter(c => c !== '') + .slice(0, labels.length); + + for (const [i, color] of colors.entries()) { + const name = labels[i]; + const githubLabel = githubLabelsByName[name]; + if (!githubLabel || githubLabel.color == color) { + continue; + } + try { + await octokit.rest.issues.updateLabel({ + owner, + repo, + name, + color + }); + core.info(`Updated color for label "${githubLabel.name}" from #${githubLabel.color} to #${color}`); + } catch (e) { + if (e instanceof Error) { + core.error(e); + } + } + } } catch (e) { if (e instanceof Error) { core.error(e);