Finalizing initial version of action & adding docs
This commit is contained in:
parent
911fcaf7e0
commit
dd3ba1906c
4 changed files with 19 additions and 8 deletions
13
README.md
Normal file
13
README.md
Normal 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.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|

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