Skip to content

Commit

Permalink
Merge pull request #50 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 23, 2022
2 parents c27447f + 43db05e commit f837019
Show file tree
Hide file tree
Showing 12 changed files with 1,087 additions and 1,046 deletions.
7 changes: 4 additions & 3 deletions dev.example → .env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

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

export BASE_DIRS="directory1|directory2"

node app.js
export BASE_DIRS="morecode|morecode2"
export INPUT_PREFIX="prefix"
export INPUT_SUFFIX="suffix"
export INPUT_SEPARATOR="seperator"
2 changes: 1 addition & 1 deletion .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Setup NodeJS
uses: actions/setup-node@v2
with:
node-version: 14
node-version: 16
check-latest: true

- name: Install dependencies
Expand Down
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
21 changes: 5 additions & 16 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,15 @@ Tests are written in [Jest](https://jestjs.io/en/). Tests automatically run on p
### To run tests locally

1. run `npm install` to install dependencies
2. run `npm test` to run tests
1. run `npm test` to run tests

### To run action locally

1. `npm install` to install dependencies
2. create a Github Personal Access Token at https://github.com/settings/tokens
3. create a `dev` file with the following contents:

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

export BASE_DIRS="directory1|directory2"

node app.js
```

3. modify contents of [/tests/fixtures/actionTrigger.json](./tests/fixtures/actionTrigger.json) as needed for test data referencing a real PR in the configured GITHUB_REPOSITORY repo
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 referencing a real PR in the configured GITHUB_REPOSITORY repo
1. run `npm start` to run locally

## Debugging

Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
- Optionally add a `BASE_DIRS` variable under `env` if modules are located within one or more base directory(ies). You can configure one (ex. `base_directory1`) or more directories (ex. `base_directory1|bae_directory2|...`). If `BASE_DIRS` isn't specified, the action will assume the base directories in the repo are your modules (ex. `base_directory1` is a module).
- Add `prefix` or `suffix` under `with` if you wish to add prefix or suffix the repo name in the label respectively.
- Add `separator` under `with` if you wish to add a separator between the prefix, label, and suffix. For example, using a single space character `' '` will add a space between them. By default, no separator is added.
- Add `separator` under `with` if you wish to add a separator between the prefix, label, and suffix. For example, using a single space character `' '` will add a space between them. By default, no separator is added.

### Example: Repos in sub directories

Expand Down Expand Up @@ -105,8 +105,9 @@ jobs:

You can pin the action version (ex. `uses: tinkurlab/[email protected]`) to avoid issues due to breaking changes in major version updates.

### Upgrading to v4.x.x
Version 4.x.x changes the previous functionlity of v3.x.x which automatically added a space between the package name a prefix (if configured) or a suffix (if configured) when labeling pull requests. Starting with version 4.0.0, if a prefix or suffix is configured, an optional seperator can be configured via the `separator: ' '` property. This property can be used to add a space or other seperator. If no `separator` property is configured, no space or other seperator will be included in the label. It is recommended that you update your action configuration before upgrading to v4.x.x.
### Upgrading to v4.x.x or >

Version 4.x.x changes the previous functionlity of v3.x.x which automatically added a space between the package name a prefix (if configured) or a suffix (if configured) when labeling pull requests. Starting with version 4.0.0, if a prefix or suffix is configured, an optional seperator can be configured via the `separator: ' '` property. This property can be used to add a space or other seperator. If no `separator` property is configured, no space or other seperator will be included in the label. It is recommended that you update your action configuration before upgrading to v4.x.x.

## Contributing

Expand All @@ -121,4 +122,6 @@ This repo is maintained by:

## 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/T53Q8F)
10 changes: 5 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "Monorepo PR Repo Labeler"
description: "Monorepo PR Repo Labeler"
name: 'Monorepo PR Repo Labeler'
description: 'Monorepo PR Repo Labeler'
inputs:
prefix:
description: Prefix to repo name to be used in label
Expand All @@ -11,8 +11,8 @@ inputs:
description: Seperator between prefix, label, and suffix (e.g., space)
required: false
runs:
using: node12
using: node16
main: dist/index.js
branding:
icon: "git-merge"
color: "green"
icon: 'git-merge'
color: 'green'
47 changes: 28 additions & 19 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
console.log('started nodejs...')
require('dotenv').config()

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

const helpers = require('./helpers')
const uniq = require('lodash.uniq')
Expand All @@ -17,32 +20,38 @@ const eventOwner = helpers.getOwner(eventOwnerAndRepo)
const eventRepo = helpers.getRepo(eventOwnerAndRepo)

async function prMonorepoRepoLabeler() {
//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)

//set eventAction and eventIssueNumber
eventAction = eventJSON.action
eventIssueNumber = eventJSON.pull_request.number
if (eventData) {
const eventJSON = JSON.parse(eventData)

//get list of files in PR
const prFiles = await helpers.listFiles(octokit, eventOwner, eventRepo, eventIssueNumber)
//set eventAction and eventIssueNumber
eventAction = eventJSON.action
eventIssueNumber = eventJSON.pull_request.number

//get monorepo repo for each file
prFilesRepos = prFiles.map(({ filename }) => helpers.getMonorepo(baseDirectories, filename))
//get list of files in PR
const prFiles = await helpers.listFiles(octokit, eventOwner, eventRepo, eventIssueNumber)

//reduce to unique repos
const prFilesReposUnique = uniq(prFilesRepos)
//get monorepo repo for each file
prFilesRepos = prFiles.map(({ filename }) => helpers.getMonorepo(baseDirectories, filename))

//add label for each monorepo repo
for (const repo of prFilesReposUnique) {
if (repo) {
console.log(`labeling repo: ${repo}`)
//reduce to unique repos
const prFilesReposUnique = uniq(prFilesRepos)

const repoLabel = helpers.getLabel(repo)
//add label for each monorepo repo
prFilesReposUnique.forEach((repo) => {
if (repo) {
const repoLabel = helpers.getLabel(repo)
console.log(`labeling repo: ${repoLabel}`)

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

Expand Down
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

49 changes: 20 additions & 29 deletions helpers.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,45 @@
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.listFiles = async function(
octokit,
eventOwner,
eventRepo,
eventIssueNumber
) {
module.exports.listFiles = async function (octokit, eventOwner, eventRepo, eventIssueNumber) {
const options = octokit.rest.pulls.listFiles.endpoint.merge({
owner: eventOwner,
repo: eventRepo,
pull_number: eventIssueNumber
pull_number: eventIssueNumber,
})

return await octokit
.paginate(options)
.then(data => {
.then((data) => {
return data
})

.catch(err => {
.catch((err) => {
console.log(err)
})
}

module.exports.getMonorepo = function(baseDirectories, filePath) {
module.exports.getMonorepo = function (baseDirectories, filePath) {
const regexPattern = `^${baseDirectories}([^./]*)/`
var regex = new RegExp(regexPattern)
var found = filePath.match(regex)
Expand All @@ -53,33 +48,29 @@ module.exports.getMonorepo = function(baseDirectories, filePath) {
else return false
}

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

module.exports.getLabel = function(repo) {
const prefix = process.env.INPUT_PREFIX || '';
const suffix = process.env.INPUT_SUFFIX || '';
const separator = process.env.INPUT_SEPARATOR || '';
repo = repo || '';
module.exports.getLabel = function (repo) {
const prefix = process.env.INPUT_PREFIX || ''
const suffix = process.env.INPUT_SUFFIX || ''
const separator = process.env.INPUT_SEPARATOR || ''
repo = repo || ''

return `${prefix}${separator}${repo}${separator}${suffix}`.trim();
const label = `${prefix}${separator}${repo}${separator}${suffix}`.trim()

return label
}
Loading

0 comments on commit f837019

Please sign in to comment.