Finalizing initial version of action & adding docs

This commit is contained in:
Benjamin Lannon 2018-12-05 21:53:04 -05:00
commit dd3ba1906c
4 changed files with 19 additions and 8 deletions

13
README.md Normal file
View file

@ -0,0 +1,13 @@
# Issue Label Manager Action
This GitHub Action allows you to declaratively state the labels to be defined in a repo.
In the repo you'd like to use this, define a JSON file in `.github/labels.json`. This file will contain an array of objects that have a name, color, and description as shown in the example below.
![labels.json file](screenshots/json.png)
Then, set up a workflow that executes this action. When run, it will update the list of labels in the repo to match the JSON file and will delete any other labels.
The result of using the labels.json file shown above is as follows:
![Labels result](screenshots/labels.png)

View file

@ -1,10 +1,12 @@
const { Toolkit } = require("actions-toolkit");
const fs = require("fs");
const path = require("path");
const tools = new Toolkit();
const octokit = tools.createOctokit();
async function run() {
// Get current labels on GitHub
let response = await octokit.issues.listLabelsForRepo(tools.context.repo());
let labels = response.data;
@ -14,11 +16,10 @@ async function run() {
"labels.json"
);
// Get the labels to be pushed from the labels.json file
let newLabels = JSON.parse(fs.readFileSync(url).toString());
console.log({ newLabels, labels });
let idxs = await Promise.all(
let indexesOfLabelsToBeRemovedFromArray = await Promise.all(
newLabels.map(async label => {
return new Promise(async resolve => {
let { name, color, description } = label;
@ -29,8 +30,6 @@ async function run() {
idx = labels.findIndex(issue => issue.name === name);
}
console.log({ idx });
if (idx !== -1) {
let params = tools.context.repo({
current_name: name,
@ -38,7 +37,6 @@ async function run() {
description,
headers: { accept: "application/vnd.github.symmetra-preview+json" }
});
console.log("UPDATE");
await octokit.issues.updateLabel(params);
resolve(idx);
} else {
@ -48,7 +46,6 @@ async function run() {
description,
headers: { accept: "application/vnd.github.symmetra-preview+json" }
});
console.log("CREATE");
await octokit.issues.createLabel(params);
resolve(-1);
}
@ -56,8 +53,9 @@ async function run() {
})
);
// Filter labels array to include labels not defined in json file
labels = labels.filter((_, idx) => {
return !idxs.includes(idx);
return !indexesOfLabelsToBeRemovedFromArray.includes(idx);
});
// Delete labels that exist on GitHub that aren't in labels.json

BIN
screenshots/json.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

BIN
screenshots/labels.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB