Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
feature: seti-587 prevent labeler fail (#9)
Browse files Browse the repository at this point in the history
* feature: [SETI-587] - added hard-failure input
  • Loading branch information
therussiankid92 authored Jun 3, 2020
1 parent d3fef52 commit 9006642
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ REPOSITORY_NAME=
LABEL_ACTION=
LABEL=
BASE_BRANCH=

HARD_FAILURE=
4 changes: 3 additions & 1 deletion .github/workflows/check-no-deploy-window.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
name: check no deploy window
steps:
- name: is deploy calendar busy?
uses: Typeform/siesta@v1
uses: Typeform/siesta@v1.1
id: siesta
with:
google-credentials: ${{ secrets.siesta_google_credentials }}
Expand All @@ -19,6 +19,7 @@ jobs:
custom-calendar-busy-message: 'There is an event in the Calendar. Deploying not recommended.'
custom-calendar-not-busy-message: 'No event in the Calendar. Feel free to deploy.'
fail-if-busy: false
hard-failure: true
- name: set label action based on result
uses: haya14busa/[email protected]
id: condval
Expand All @@ -35,3 +36,4 @@ jobs:
label: no-deploy-window
label-action: ${{steps.condval.outputs.value}}
base-branch: master
hard-failure: true
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ jobs:
uses: ./
id: labeler
with:
github-token: ${{ secrets.labeler_github_token }}
github-token: ${{ secrets.GITHUB_TOKEN }}
repository-name: ${{ github.repository }}
label-action: add
label: LABELER_TEST
base-branch: master
hard-failure: true
3 changes: 2 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ A Github Action that helps you to add and remove labels of all PRs in a given re
```label_action```: Mandatory, option between add or remove
```label```: Mandatory, how you want to name your label
```base-branch```: Optional, filter pulls by base branch name. Default: all base branches

```hard-failure```: Optional, boolean, if true, fails the build on any error. If false, throws an warning instead. Default is false.

## Contributing
All code should pass tests, as well as be well documented. [Please also see the Commit Message Guidelines](CONTRIBUTING.MD) for how commit messages should be structured.

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ inputs:
description: filter pulls by base branch name
required: false
default: all
hard-failure:
description: boolean, if true, fails the build on any error. If false, throws an warning instead.
required: false

runs:
using: 'node12'
main: 'dist/index.js'
Expand Down
55 changes: 49 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2643,13 +2643,15 @@ const DEFAULT_REPOSITORY_NAME = process.env.REPOSITORY_NAME
const DEFAULT_LABEL_ACTION = process.env.LABEL_ACTION
const DEFAULT_LABEL = process.env.LABEL
const DEFAULT_BASE_BRANCH = process.env.BASE_BRANCH
const DEFAULT_HARD_FAILURE = process.env.HARD_FAILURE || 'false'

module.exports = {
DEFAULT_GITHUB_TOKEN,
DEFAULT_LABEL,
DEFAULT_LABEL_ACTION,
DEFAULT_REPOSITORY_NAME,
DEFAULT_BASE_BRANCH,
DEFAULT_HARD_FAILURE,
}


Expand Down Expand Up @@ -5455,7 +5457,14 @@ module.exports = resolveCommand;

const core = __webpack_require__(470)

const { DEFAULT_GITHUB_TOKEN, DEFAULT_LABEL, DEFAULT_LABEL_ACTION, DEFAULT_REPOSITORY_NAME, DEFAULT_BASE_BRANCH } = __webpack_require__(328)
const {
DEFAULT_GITHUB_TOKEN,
DEFAULT_LABEL,
DEFAULT_LABEL_ACTION,
DEFAULT_REPOSITORY_NAME,
DEFAULT_BASE_BRANCH,
DEFAULT_HARD_FAILURE,
} = __webpack_require__(328)

/**
* Gets github token and parses it
Expand Down Expand Up @@ -5503,17 +5512,45 @@ const getBaseBranch = () => {
return (core.getInput('base-branch') || DEFAULT_BASE_BRANCH)
}

/**
* Gets hard-failure
*/
const getHardFailure = () => {
const shouldHardFailure = core.getInput('hard-failure') || DEFAULT_HARD_FAILURE
if (shouldHardFailure.toLowerCase() === 'true') return true
return false
}

/**
* Sets the Github Action to fail
* @param {String} message
* @param {string} message
*/
const throwGithubError = (message) => {
core.setFailed(message)
}

/**
* If app fails outputs a Warning or either throws an Error depending on hard-failure env variable
* @param {Object} error
*/
const outputFailure = (error) => {
if (getHardFailure()) {
throwGithubError(error.message)
} else {
throwGithubWarning(error.message)
}
}
/**
* Sets a Github warning in the console
* @param {string} message
*/
const throwGithubWarning = (message) => {
core.warning(message)
}

/**
* returns the owner and name of the repo
* @param {String} repositoryNameAndOwner
* @param {string} repositoryNameAndOwner
*/
const getSeparatedRepositoryNameAndOwner = (repositoryNameAndOwner) => {
const splitNameAndOwner = repositoryNameAndOwner.split('/')
Expand All @@ -5527,7 +5564,10 @@ module.exports = {
getLabelAction,
getRepositorySlug,
getBaseBranch,
getHardFailure,
getSeparatedRepositoryNameAndOwner,
outputFailure,
throwGithubWarning,
}


Expand Down Expand Up @@ -6106,10 +6146,10 @@ module.exports = (promise, onFinally) => {
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {

__webpack_require__(63).config()
const { getLabel, throwGithubError, getLabelAction, getBaseBranch } = __webpack_require__(501)
const { getLabel, outputFailure, getLabelAction, getBaseBranch } = __webpack_require__(501)
const GithubAPI = __webpack_require__(880)

const aGithubAPI = new GithubAPI()
let aGithubAPI = {}

/**
* Adds a label in a PullRequest
Expand All @@ -6118,6 +6158,7 @@ const aGithubAPI = new GithubAPI()
* @param {String} number of the pull request
*/
const addLabel = (labelToAdd, labels, number) => {
console.log(`...Adding label ${labelToAdd} to PR ${number}`)
return aGithubAPI.updatePRLabels(number, [...labels, labelToAdd])
}

Expand All @@ -6128,6 +6169,7 @@ const addLabel = (labelToAdd, labels, number) => {
* @param {String} number of the pull request
*/
const removeLabel = (labelToDelete, labels, number) => {
console.log(`...Removing label ${labelToDelete} from PR ${number}`)
return aGithubAPI.updatePRLabels(number, labels.filter(label => label !== labelToDelete))
}

Expand All @@ -6141,6 +6183,7 @@ const listAllOpenPRs = (baseBranch) => {

const main = async () => {
try {
aGithubAPI = new GithubAPI()
const openPullRequests = await listAllOpenPRs(getBaseBranch())

for (const pr of openPullRequests) {
Expand All @@ -6159,7 +6202,7 @@ const main = async () => {
}
}
} catch (error) {
throwGithubError(error.message)
outputFailure(error)
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/application/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require('dotenv').config()
const { getLabel, throwGithubError, getLabelAction, getBaseBranch } = require('../infrastructure/github-actions')
const { getLabel, outputFailure, getLabelAction, getBaseBranch } = require('../infrastructure/github-actions')
const GithubAPI = require('../infrastructure/githubapi')

const aGithubAPI = new GithubAPI()
let aGithubAPI = {}

/**
* Adds a label in a PullRequest
Expand All @@ -11,6 +11,7 @@ const aGithubAPI = new GithubAPI()
* @param {String} number of the pull request
*/
const addLabel = (labelToAdd, labels, number) => {
console.log(`...Adding label ${labelToAdd} to PR ${number}`)
return aGithubAPI.updatePRLabels(number, [...labels, labelToAdd])
}

Expand All @@ -21,6 +22,7 @@ const addLabel = (labelToAdd, labels, number) => {
* @param {String} number of the pull request
*/
const removeLabel = (labelToDelete, labels, number) => {
console.log(`...Removing label ${labelToDelete} from PR ${number}`)
return aGithubAPI.updatePRLabels(number, labels.filter(label => label !== labelToDelete))
}

Expand All @@ -34,6 +36,7 @@ const listAllOpenPRs = (baseBranch) => {

const main = async () => {
try {
aGithubAPI = new GithubAPI()
const openPullRequests = await listAllOpenPRs(getBaseBranch())

for (const pr of openPullRequests) {
Expand All @@ -52,7 +55,7 @@ const main = async () => {
}
}
} catch (error) {
throwGithubError(error.message)
outputFailure(error)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/infrastructure/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const DEFAULT_REPOSITORY_NAME = process.env.REPOSITORY_NAME
const DEFAULT_LABEL_ACTION = process.env.LABEL_ACTION
const DEFAULT_LABEL = process.env.LABEL
const DEFAULT_BASE_BRANCH = process.env.BASE_BRANCH
const DEFAULT_HARD_FAILURE = process.env.HARD_FAILURE || 'false'

module.exports = {
DEFAULT_GITHUB_TOKEN,
DEFAULT_LABEL,
DEFAULT_LABEL_ACTION,
DEFAULT_REPOSITORY_NAME,
DEFAULT_BASE_BRANCH,
DEFAULT_HARD_FAILURE,
}
40 changes: 39 additions & 1 deletion src/infrastructure/github-actions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const core = require('@actions/core')

const { DEFAULT_GITHUB_TOKEN, DEFAULT_LABEL, DEFAULT_LABEL_ACTION, DEFAULT_REPOSITORY_NAME, DEFAULT_BASE_BRANCH } = require('./constants')
const {
DEFAULT_GITHUB_TOKEN,
DEFAULT_LABEL,
DEFAULT_LABEL_ACTION,
DEFAULT_REPOSITORY_NAME,
DEFAULT_BASE_BRANCH,
DEFAULT_HARD_FAILURE,
} = require('./constants')

/**
* Gets github token and parses it
Expand Down Expand Up @@ -48,6 +55,15 @@ const getBaseBranch = () => {
return (core.getInput('base-branch') || DEFAULT_BASE_BRANCH)
}

/**
* Gets hard-failure
*/
const getHardFailure = () => {
const shouldHardFailure = core.getInput('hard-failure') || DEFAULT_HARD_FAILURE
if (shouldHardFailure.toLowerCase() === 'true') return true
return false
}

/**
* Sets the Github Action to fail
* @param {string} message
Expand All @@ -56,6 +72,25 @@ const throwGithubError = (message) => {
core.setFailed(message)
}

/**
* If app fails outputs a Warning or either throws an Error depending on hard-failure env variable
* @param {Object} error
*/
const outputFailure = (error) => {
if (getHardFailure()) {
throwGithubError(error.message)
} else {
throwGithubWarning(error.message)
}
}
/**
* Sets a Github warning in the console
* @param {string} message
*/
const throwGithubWarning = (message) => {
core.warning(message)
}

/**
* returns the owner and name of the repo
* @param {string} repositoryNameAndOwner
Expand All @@ -72,5 +107,8 @@ module.exports = {
getLabelAction,
getRepositorySlug,
getBaseBranch,
getHardFailure,
getSeparatedRepositoryNameAndOwner,
outputFailure,
throwGithubWarning,
}
52 changes: 52 additions & 0 deletions src/infrastructure/tests/github-actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ describe('getBaseBranch', () => {
})
})

describe('getHardFailure', () => {
beforeEach(() => {
jest.resetModules()
})
it('should return a the value of HARD_FAILURE env', () => {
jest.mock('../constants', () => ({ DEFAULT_HARD_FAILURE: 'TRUE' }))

// eslint-disable-next-line global-require
const { getHardFailure } = require('../github-actions')
expect(getHardFailure()).toEqual(true)
})
})

describe('throwGithubError', () => {
beforeEach(() => {
jest.resetModules()
Expand All @@ -130,6 +143,45 @@ describe('throwGithubError', () => {
})
})

describe('outputFailure', () => {
beforeEach(() => {
jest.resetModules()
})
it('should throw github error if HARD_FAILURE true', () => {
jest.mock('../constants', () => ({ DEFAULT_HARD_FAILURE: 'true' }))

// eslint-disable-next-line global-require
const { outputFailure } = require('../github-actions')
// eslint-disable-next-line global-require
const core = require('@actions/core')
outputFailure({ message: 'message' })
expect(core.setFailed).toHaveBeenCalledWith('message')
})
it('should throw github warning if HARD_FAILURE false', () => {
jest.mock('../constants', () => ({ DEFAULT_HARD_FAILURE: 'false' }))
// eslint-disable-next-line global-require
const { outputFailure } = require('../github-actions')
// eslint-disable-next-line global-require
const core = require('@actions/core')
outputFailure({ message: 'message' })
expect(core.warning).toBeCalledWith('message')
})
})

describe('throwGithubWarning', () => {
beforeEach(() => {
jest.resetModules()
})
it('should throw github warning', () => {
// eslint-disable-next-line global-require
const { throwGithubWarning } = require('../github-actions')
// eslint-disable-next-line global-require
const core = require('@actions/core')
throwGithubWarning('message')
expect(core.warning).toHaveBeenCalledWith('message')
})
})

describe('getSeparatedRepositoryNameAndOwner', () => {
beforeEach(() => {
jest.resetModules()
Expand Down

0 comments on commit 9006642

Please sign in to comment.