Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependencies + GitLab support #48

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
16 changes: 16 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"prettier"
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
},
"ignorePatterns": ["temp.js", "**/dist/*.js"]
}
12 changes: 11 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Created by https://www.gitignore.io/api/node
# Created by https://www.gitignore.io/api/node,visualstudiocode

### Node ###
# Logs
Expand Down Expand Up @@ -67,3 +67,13 @@ dist/

# Generated documentation
docs/


### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# End of https://www.gitignore.io/api/node,visualstudiocode
9 changes: 9 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.babelrc
.editorconfig
.esdoc.json
.travis.yml
yarn.lock
node_modules/
src/
types/*
!types/index.d.ts
5 changes: 0 additions & 5 deletions .prettierrc

This file was deleted.

11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Development

Install [Yarn](https://yarnpkg.com/en/), and install the dependencies - `yarn install`.

Run the [Jest](https://facebook.github.io/jest/) test suite with `yarn test`.

This project uses [semantic-release](https://github.com/semantic-release/semantic-release) for automated NPM package publishing.

The main caveat: instead of running `git commit`, run `yarn commit` and follow the prompts to input a conventional changelog message via [commitizen](https://github.com/commitizen/cz-cli).

:heart:
64 changes: 22 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
"dist"
],
"scripts": {
"precommit": "lint-staged",
"commit": "git-cz",
"commitmsg": "validate-commit-msg",
"build": "tsc -p tsconfig.json",
"lint": "tslint 'src/**/*.ts'",
"build": "babel src --out-dir dist --ignore *.test.js",
"lint": "eslint -c .eslintrc.json --ext .js src",
"test": "jest --env=node",
"prepublishOnly": "npm run build",
"semantic-release": "semantic-release",
"travis-deploy-once": "travis-deploy-once"
"prepublish": "npm run build",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"repository": {
"type": "git",
Expand All @@ -28,47 +27,28 @@
],
"author": "Macklin Underdown <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/macklinu/danger-plugin-jira-issue/issues"
},
"homepage": "https://github.com/macklinu/danger-plugin-jira-issue#readme",
"engines": {
"node": ">= 4.0.0"
},
"devDependencies": {
"@types/jest": "^21.1.4",
"@types/node": "^8.0.6",
"husky": "^0.14.1",
"jest": "^21.2.1",
"lint-staged": "^4.0.0",
"prettier": "^1.14.3",
"@babel/cli": "^7.23.0",
"@babel/core": "^7.23.3",
"@babel/preset-env": "^7.23.3",
"babel-jest": "^29.7.0",
"commitizen": "^2.9.6",
"cz-conventional-changelog": "^2.0.0",
"danger": "^11.3.0",
"eslint": "^8.53.0",
"eslint-config-prettier": "^9.0.0",
"husky": "^0.13.3",
"jest": "^29.7.0",
"prettier": "^1.3.1",
"semantic-release": "^15.9.17",
"travis-deploy-once": "^5.0.9",
"ts-jest": "^21.1.3",
"tslint": "^5.11.0",
"tslint-config-macklinu": "^1.0.0",
"typescript": "^3.1.1",
"validate-commit-msg": "^2.12.1"
},
"jest": {
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
]
"validate-commit-msg": "^2.14.0"
},
"lint-staged": {
"*.ts": [
"tslint --fix",
"git add"
],
"*.json": [
"prettier --write",
"git add"
]
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
}
}
3 changes: 0 additions & 3 deletions src/ambient.d.ts

This file was deleted.

61 changes: 27 additions & 34 deletions src/index.ts → src/index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,28 @@
import { resolve } from 'url'

export interface Options {
/**
* The JIRA issue key(s) (e.g. the ABC in ABC-123).
* Supports multiple JIRA projects (e.g. `['ABC', 'DEF']`).
*/
key: string | string[]
/** The JIRA instance issue base URL (e.g. https://jira.atlassian.com/browse/). */
url: string
/**
* The emoji to display with the JIRA issue link.
* See the possible emoji values, listed as keys in the
* [GitHub API `/emojis` response](https://api.github.com/emojis).
* Defaults to `':link:'`.
*/
emoji?: string
/**
* A format function to format the message
* @param {string} emoji
* @param {string[]} jiraUrls
* @returns {string}
*/
format?: (emoji: string, jiraUrls: string[]) => string
/**
* The location of the JIRA issue, either the PR title, or the git branch
* Defaults to `title`
*/
location?: 'title' | 'branch'
}

const link = (href: string, text: string): string =>
const link = (href, text) =>
`<a href="${href}">${text}</a>`

const ensureUrlEndsWithSlash = (url: string) => {
const ensureUrlEndsWithSlash = (url) => {
if (!url.endsWith('/')) {
return url.concat('/')
}
return url
}

const isObject = (value) => {
const type = typeof value;
return value != null && (type == 'object' || type == 'function');
}

/**
* A Danger plugin to add a JIRA issue link to the Danger pull request comment.
* If a pull request title does not contain the supplied JIRA issue identifier (e.g. ABC-123),
* then Danger will comment with a warning on the pull request asking the developer
* to include the JIRA issue identifier in the pull request title.
*/
export default function jiraIssue(options: Options) {
export default function jiraIssue(options) {
const { key = '', url = '', emoji = ':link:', location = 'title' } =
options || {}
if (!url) {
Expand All @@ -55,21 +32,37 @@ export default function jiraIssue(options: Options) {
throw Error(`'key' missing - must supply JIRA issue key`)
}

const repositoryType = [
"github",
"gitlab"
].find(type => isObject(danger?.[type]) && Object.keys(danger[type]).length > 0)

if (repositoryType === undefined) {
throw Error(
`Could not detect the repository's type.`
)
}

// Support multiple JIRA projects.
const keys = Array.isArray(key) ? `(${key.join('|')})` : key

const jiraKeyRegex = new RegExp(`(${keys}-[0-9]+)`, 'g')
let match
const jiraIssues = []
// tslint:disable-next-line:no-conditional-assignment
let jiraLocation
switch (location) {
case 'title': {
jiraLocation = danger.github.pr.title
jiraLocation = {
'github': danger?.github?.pr?.title,
'gitlab': danger?.gitlab?.mr?.title
}[repositoryType]
break
}
case 'branch': {
jiraLocation = danger.github.pr.head.ref
jiraLocation = {
'github': danger?.github?.pr?.head?.ref,
'gitlab': danger?.gitlab?.mr?.source_branch
}[repositoryType]
break
}
default: {
Expand Down
52 changes: 46 additions & 6 deletions src/index.test.ts → src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import jiraIssue from './'

declare const global: any

describe('jiraIssue()', () => {
beforeEach(() => {
global.warn = jest.fn()
Expand All @@ -13,11 +11,11 @@ describe('jiraIssue()', () => {
global.message = undefined
})
it('throws when supplied invalid configuration', () => {
const anyJira = jiraIssue as any
const anyJira = jiraIssue
expect(() => anyJira()).toThrow()
expect(() => jiraIssue({} as any)).toThrow()
expect(() => jiraIssue({ key: 'ABC' } as any)).toThrow()
expect(() => jiraIssue({ url: 'https://jira.net/browse' } as any)).toThrow()
expect(() => jiraIssue({})).toThrow()
expect(() => jiraIssue({ key: 'ABC' })).toThrow()
expect(() => jiraIssue({ url: 'https://jira.net/browse' })).toThrow()
})
it('warns when PR title is missing JIRA issue key', () => {
global.danger = { github: { pr: { title: 'Change some things' } } }
Expand Down Expand Up @@ -146,4 +144,46 @@ describe('jiraIssue()', () => {
':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'
)
})
it('correctly identifies the repository type when the other type is an empty object', () => {
global.danger = {
github: {},
gitlab: { mr: { source_branch: 'ABC-808/some-things' } },
}
jiraIssue({
key: 'ABC',
location: 'branch',
url: 'https://jira.net/browse',
})
expect(global.message).toHaveBeenCalledWith(
':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'
)
})
it('correctly identifies the repository type when the other type is undefined', () => {
global.danger = {
github: undefined,
gitlab: { mr: { source_branch: 'ABC-808/some-things' } },
}
jiraIssue({
key: 'ABC',
location: 'branch',
url: 'https://jira.net/browse',
})
expect(global.message).toHaveBeenCalledWith(
':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'
)
})
it('correctly identifies the repository type when the other type is an empty string', () => {
global.danger = {
github: "",
gitlab: { mr: { source_branch: 'ABC-808/some-things' } },
}
jiraIssue({
key: 'ABC',
location: 'branch',
url: 'https://jira.net/browse',
})
expect(global.message).toHaveBeenCalledWith(
':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'
)
})
})
20 changes: 0 additions & 20 deletions tsconfig.json

This file was deleted.

3 changes: 0 additions & 3 deletions tslint.json

This file was deleted.

Loading