diff --git a/README.md b/README.md new file mode 100644 index 0000000..aca3ffd --- /dev/null +++ b/README.md @@ -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) diff --git a/index.js b/index.js index c974ad0..729fa08 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/screenshots/json.png b/screenshots/json.png new file mode 100644 index 0000000..e95e9ab Binary files /dev/null and b/screenshots/json.png differ diff --git a/screenshots/labels.png b/screenshots/labels.png new file mode 100644 index 0000000..1520cec Binary files /dev/null and b/screenshots/labels.png differ