Skip to content

Commit

Permalink
Merge pull request #26 from TinkurLab/patching-2022-11
Browse files Browse the repository at this point in the history
Patching 2022-11
  • Loading branch information
adamzolyak authored Nov 19, 2022
2 parents d44d7d3 + cedacf2 commit 2bba595
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 556 deletions.
2 changes: 0 additions & 2 deletions dev.example → .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

export GITHUB_TOKEN="12345"
export GITHUB_REPOSITORY="tinkurlab/actions-playground"
export GITHUB_EVENT_PATH="/issue-labeler-action/tests/fixtures/actionTrigger.json"

node app.js
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.DS_Store
dev
dev
.env
16 changes: 4 additions & 12 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,10 @@ Tests are written in [Jest](https://jestjs.io/en/). Tests automatically run on c
### To run action locally

1. `npm install` to install dependencies
2. create a `dev` file with the following contents:

```bash
export GITHUB_TOKEN="12345"
export GITHUB_REPOSITORY="tinkurlab/actions-playground"
export GITHUB_EVENT_PATH="/issue-labeler-action/tests/fixtures/actionTrigger.json"

node app.js
```

3. modify contents of [/tests/fixtures/actionTrigger.json](./tests/fixtures/actionTrigger.json) as needed for test data
4. run `bash dev` to run locally
1. create a Github Personal Access Token at https://github.com/settings/tokens
1. create a `.env` file similar to `.env.example`
1. modify contents of [/tests/fixtures/actionTrigger.json](./tests/fixtures/actionTrigger.json) as needed for test data
1. run `npm start` to run locally

## Debugging

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:14
FROM node:16

LABEL "com.github.actions.name"="Issue Bulk Labeler"
LABEL "com.github.actions.description"="Bulk add labels when creating an issue"
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A [GitHub Action](https://github.com/features/actions) to add bulk labels when c

## How It Works

This GitHub Action runs when an [`issues` event webhook](https://developer.github.com/v3/activity/events/types/#issuesevent) is fired in your GitHub repo. The action checks if there is an array of bulk labels to add `[aaa, bbb, ccc]` in markdown in the issue's description. The action works with existing labels defined in your GitHub repo; the labeler expects the first 3 characters of the label name (ex. `hel` for a "help wanted" label). If there is 1 or > bulk labels, the action labels the issue with the specified label(s).
This GitHub Action runs when an [`issues` event webhook](https://developer.github.com/v3/activity/events/types/#issuesevent) is fired in your GitHub repo. The action checks if there is an array of bulk labels to add `[aaa, bbb, ccc]` in markdown in the issue's description on a new line. The action works with existing labels defined in your GitHub repo; the labeler expects the first 3 characters of the label name (ex. `hel` for a "help wanted" label). If there is 1 or > bulk labels, the action labels the issue with the specified label(s).

## Examples

Expand Down Expand Up @@ -46,8 +46,6 @@ If you have suggestions for how this GitHub Action could be improved, or want to
## License
[ISC](LICENSE) © 2021 Adam Zolyak <[email protected]> (www.tinkurlab.com)
[ISC](LICENSE) © 2022 Adam Zolyak <[email protected]> (www.tinkurlab.com)
![analytics](https://grabify.link/1WWMV5)
12 changes: 6 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: "Issue Bulk Labeler"
description: "Issue Bulk Labeler"
name: 'Issue Bulk Labeler'
description: 'Issue Bulk Labeler'
runs:
using: "docker"
image: "Dockerfile"
using: 'docker'
image: 'Dockerfile'
branding:
icon: "tag"
color: "green"
icon: 'tag'
color: 'green'
70 changes: 32 additions & 38 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
console.log('started nodejs...')
require('dotenv').config()

const packageInfo = require('./package.json')
console.log(`Starting ${packageInfo.name}`)

const helpers = require('./helpers')

//require octokit rest.js
//more info at https://github.com/octokit/rest.js
const Octokit = require('@octokit/rest')
const { Octokit } = require('@octokit/rest')
const octokit = new Octokit({
auth: `token ${process.env.GITHUB_TOKEN}`
auth: `token ${process.env.GITHUB_TOKEN}`,
})

//set eventOwner and eventRepo based on action's env variables
Expand All @@ -15,52 +18,43 @@ const eventOwner = helpers.getOwner(eventOwnerAndRepo)
const eventRepo = helpers.getRepo(eventOwnerAndRepo)

async function bulkLabelAdd() {
//read contents of action's event.json
const eventData = await helpers.readFilePromise(
'..' + process.env.GITHUB_EVENT_PATH
)
const eventJSON = JSON.parse(eventData)
try {
//read contents of action's event.json
const eventData = await helpers.readFilePromise('..' + process.env.GITHUB_EVENT_PATH)
const eventJSON = JSON.parse(eventData)

//set eventAction and eventIssueNumber
eventAction = eventJSON.action
eventIssueNumber = eventJSON.issue.number
eventIssueBody = eventJSON.issue.body
//set eventAction and eventIssueNumber
eventAction = eventJSON.action
eventIssueNumber = eventJSON.issue.number
eventIssueBody = eventJSON.issue.body

//if an issue was opened, edited, or reopened
if (eventAction === 'opened') {
//check if there are bulk labels
const bulkLabels = await helpers.getBulkLabels(eventIssueBody)
//if an issue was opened, edited, or reopened
if (eventAction === 'opened') {
//check if there are bulk labels
const bulkLabels = await helpers.getBulkLabels(eventIssueBody)
console.log('bulkLabels: ', bulkLabels)

//if one or more bulk labels
if (bulkLabels) {
console.log('bulk labels found in issue...')
//if one or more bulk labels
if (bulkLabels) {
console.log('bulk labels found in issue...')

const repoLabels = await helpers.getRepoLabels(
octokit,
eventOwner,
eventRepo
)
const repoLabels = await helpers.getRepoLabels(octokit, eventOwner, eventRepo)

const repoShortLabels = await helpers.addShortLabelName(repoLabels)
const repoShortLabels = await helpers.addShortLabelName(repoLabels)

for (const issueLabel of bulkLabels) {
for (const repoLabel of repoShortLabels) {
if (
issueLabel.toLowerCase() === repoLabel.shortLabelName.toLowerCase()
) {
console.log('issue label matches repo issue; labeling...')
for (const issueLabel of bulkLabels) {
for (const repoLabel of repoShortLabels) {
if (issueLabel.toLowerCase() === repoLabel.shortLabelName.toLowerCase()) {
console.log('issue label matches repo issue; labeling...')

helpers.addLabel(
octokit,
eventOwner,
eventRepo,
eventIssueNumber,
repoLabel.name
)
helpers.addLabel(octokit, eventOwner, eventRepo, eventIssueNumber, repoLabel.name)
}
}
}
}
}
} catch (error) {
console.log(error)
}
}

Expand Down
33 changes: 14 additions & 19 deletions helpers.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,69 @@
const fs = require('fs')

module.exports.readFilePromise = function(filename) {
module.exports.readFilePromise = function (filename) {
return new Promise((resolve, reject) => {
fs.readFile(filename, 'utf8', (err, data) => {
if (err) reject(err)
else resolve(data)
})
}).catch(err => {
}).catch((err) => {
console.log(err)
})
}

module.exports.getOwner = function(eventOwnerAndRepo) {
module.exports.getOwner = function (eventOwnerAndRepo) {
const slicePos1 = eventOwnerAndRepo.indexOf('/')
return eventOwnerAndRepo.slice(0, slicePos1)
}

module.exports.getRepo = function(eventOwnerAndRepo) {
module.exports.getRepo = function (eventOwnerAndRepo) {
const slicePos1 = eventOwnerAndRepo.indexOf('/')
return eventOwnerAndRepo.slice(slicePos1 + 1, eventOwnerAndRepo.length)
}

module.exports.getBulkLabels = function(eventIssueBody) {
module.exports.getBulkLabels = function (eventIssueBody) {
const regex = RegExp(/^\[(.*){3}\]/m)
const matches = regex.exec(eventIssueBody)

if (matches) {
return matches[0].slice(1, matches[0].length - 1).split(', ')
} else {
return matches
}
}

module.exports.addLabel = function(
octokit,
eventOwner,
eventRepo,
eventIssueNumber,
label
) {
module.exports.addLabel = function (octokit, eventOwner, eventRepo, eventIssueNumber, label) {
octokit.issues
.addLabels({
owner: eventOwner,
repo: eventRepo,
issue_number: eventIssueNumber,
labels: [label] // ['Incomplete Tasks']
labels: [label], // ['Incomplete Tasks']
})
.then(({ data, headers, status }) => {
// handle data
})
.catch(err => {
.catch((err) => {
console.log(err)
})
}

module.exports.getRepoLabels = async function(octokit, eventOwner, eventRepo) {
module.exports.getRepoLabels = async function (octokit, eventOwner, eventRepo) {
return octokit.issues
.listLabelsForRepo({
owner: eventOwner,
repo: eventRepo
repo: eventRepo,
})
.then(({ data, headers, status }) => {
return data
})
.catch(err => {
.catch((err) => {
console.log(err)
})
}

module.exports.addShortLabelName = async function(repoLabels) {
repoLabel = await repoLabels.map(repoLabel => {
module.exports.addShortLabelName = async function (repoLabels) {
repoLabel = await repoLabels.map((repoLabel) => {
repoLabel.shortLabelName = repoLabel.name.slice(0, 3)
return repoLabel
})
Expand Down
Loading

0 comments on commit 2bba595

Please sign in to comment.