Skip to content

Commit

Permalink
Allow specifying colors
Browse files Browse the repository at this point in the history
  • Loading branch information
petermarathas committed Mar 22, 2024
1 parent 1a9c371 commit 321fdd1
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 49 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
124 changes: 76 additions & 48 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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 <https://github.com/jonschlinkert/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:
Expand Down Expand Up @@ -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 <https://github.com/jonschlinkert/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:
Expand Down
31 changes: 30 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,41 @@ async function run(): Promise<void> {
}

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);
Expand Down

0 comments on commit 321fdd1

Please sign in to comment.