Skip to content

Commit

Permalink
Fix defaulting (#250)
Browse files Browse the repository at this point in the history
* Fix defaulting and adjust defaults in action.yaml

* Changelog and package updates
  • Loading branch information
dangoslen authored Feb 11, 2024
1 parent ffabc6e commit 3e4e9cc
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [UNRELEASED]

## [3.8.1]

### Fixed
- Fixes improper defaulting for deprecated `activationLabel` to `activationLabels` input

## [3.8.0]

### Added
Expand Down
55 changes: 51 additions & 4 deletions __tests__/dependabot-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core'
import * as factory from '../src/entries/extractor-factory'
import * as updater from '../src/changelog-updater'
import * as factory from '../src/entries/extractor-factory'

import {run} from '../src/dependabot-helper'

Expand All @@ -11,7 +11,7 @@ describe('run', () => {
jest.resetAllMocks()
})

test('should update changelog when labels are included', () => {
test('should use activationLabels only', () => {
const mockGetInput = jest.spyOn(core, 'getInput')

const mockUpdater = {
Expand All @@ -34,7 +34,7 @@ describe('run', () => {

mockGetInput
.mockReturnValueOnce('1.0.0') // version
.mockReturnValueOnce('') // activationLabel
.mockReturnValueOnce('label') // activationLabel
.mockReturnValueOnce('dependabot-helper') // activationLabels
.mockReturnValueOnce('/path/to/changelog.md') // changelogPath
.mockReturnValueOnce('Bump') // entryPrefix
Expand All @@ -58,7 +58,7 @@ describe('run', () => {
})
})

test('should not update changelog when labels are not all included', () => {
test('should use activationLabels when there are multiple labels', () => {
const mockGetInput = jest.spyOn(core, 'getInput')

const mockUpdater = {
Expand Down Expand Up @@ -90,4 +90,51 @@ describe('run', () => {
expect(core.setFailed).not.toHaveBeenCalled()
})
})

test('use the activation label only when activationLabels are empty', () => {
const mockGetInput = jest.spyOn(core, 'getInput')

const mockUpdater = {
readChangelog: jest.fn(),
updateChangelog: jest.fn(),
writeChangelog: jest.fn()
}

const mockExtractor = {
getEntries: jest.fn().mockReturnValue([
{
pullRequestNumber: 123,
repository: 'repo',
package: 'package',
oldVersion: 'v2',
newVersion: 'v3'
}
])
}

mockGetInput
.mockReturnValueOnce('1.0.0') // version
.mockReturnValueOnce('dependabot-helper') // activationLabel
.mockReturnValueOnce('') // activationLabels
.mockReturnValueOnce('/path/to/changelog.md') // changelogPath
.mockReturnValueOnce('Bump') // entryPrefix
.mockReturnValueOnce('Dependencies') // sectionHeader

jest.spyOn(factory, 'getExtractor').mockReturnValue(mockExtractor)
jest.spyOn(updater, 'newUpdater').mockReturnValue(mockUpdater)

run().then(() => {
expect(mockUpdater.readChangelog).toHaveBeenCalled()
expect(mockExtractor.getEntries).toHaveBeenCalled()
expect(mockUpdater.updateChangelog).toHaveBeenCalledWith({
pullRequestNumber: 123,
repository: 'repo',
package: 'package',
oldVersion: 'v2',
newVersion: 'v3'
})
expect(mockUpdater.writeChangelog).toHaveBeenCalled()
expect(core.setFailed).not.toHaveBeenCalled()
})
})
})
47 changes: 47 additions & 0 deletions __tests__/label-extractor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {parseLabels} from '../src/label-extractor'

test('parseLabels should return an empty array when given an empty string', () => {
const labelsString = ''
const result = parseLabels(labelsString)
expect(result).toEqual([])
})

test('parseLabels should correctly parse a single label', () => {
const labelsString = 'label1'
const result = parseLabels(labelsString)
expect(result).toEqual(['label1'])
})

test('parseLabels should correctly parse multiple labels separated by commas', () => {
const labelsString = 'label1, label2, label3'
const result = parseLabels(labelsString)
expect(result).toEqual(['label1', 'label2', 'label3'])
})

test('parseLabels should correctly parse labels with special characters', () => {
const labelsString =
'label1, label-with-dashes, label+with+plus, label?with?question, label;with;semicolon, label[with]brackets, label(with)parenthesis, label/with/forward-slashes'
const result = parseLabels(labelsString)
expect(result).toEqual([
'label1',
'label-with-dashes',
'label+with+plus',
'label?with?question',
'label;with;semicolon',
'label[with]brackets',
'label(with)parenthesis',
'label/with/forward-slashes'
])
})

test('parseLabels should ignore leading and trailing whitespace', () => {
const labelsString = ' label1 , label2 , label3 '
const result = parseLabels(labelsString)
expect(result).toEqual(['label1', 'label2', 'label3'])
})

test('parseLabels should ignore empty labels', () => {
const labelsString = 'label1, , label2, , label3'
const result = parseLabels(labelsString)
expect(result).toEqual(['label1', 'label2', 'label3'])
})
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ inputs:
description: |
"DEPRECATED! Please use `activationLabels` instead. The label to indicate that the action should run"
required: true
default: 'dependabot'
default: ''
activationLabels:
description: |
"The labels to activate/run this action. Labels are comma-separated. Each label must be present on the pull request for the action to run."
required: true
default: ''
default: 'dependabot'
entryPrefix:
description: |
"The prefix word (after the hyphen) of the changelog entry, for example: '- [entryPrefix] `dependency` from v1.0 to v2.0'"
Expand Down
2 changes: 1 addition & 1 deletion coverage/badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 12 additions & 8 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dependabot-helper",
"version": "3.8.0",
"name": "dependabot-changelog-helper",
"version": "3.8.1",
"private": false,
"description": "A GitHub Action to auto-add dependabot changes to your changelog and increment version numbers",
"main": "lib/dependabot-helper.js",
Expand Down
14 changes: 8 additions & 6 deletions src/dependabot-helper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {PathLike} from 'fs'
import * as core from '@actions/core'
import * as github from '@actions/github'
import {PathLike} from 'fs'
import {newUpdater} from './changelog-updater'
import {getExtractor} from './entries/extractor-factory'
import {DefaultChangelogUpdater, newUpdater} from './changelog-updater'
import {parseLabels} from './label-extractor'
import {pullRequestHasLabels} from './label-checker'
import {parseLabels} from './label-extractor'

export async function run(): Promise<void> {
try {
Expand All @@ -22,9 +22,11 @@ export async function run(): Promise<void> {
)
}

const labels = parseLabels(labelsString)
if (label !== '' && !labels.includes(label)) {
labels.push(label)
// If the `activationLabels` input is not set, use the `activationLabel` input only
// If the `activationLabels` input is set, use it and ignore the `activationLabel` input
let labels = parseLabels(labelsString)
if (labels.length === 0) {
labels = [label]
}

if (labels.length > 0 && pullRequestHasLabels(payload, labels)) {
Expand Down
8 changes: 5 additions & 3 deletions src/label-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ export function parseLabels(labelsString: string): string[] {
// Matches words (\w), whitespace characters (\s), dashes (-), plus signs (+), questions marks (\?), semi-colons (;), brackets (\[\]), parenthesis (\(\)) and forward-slashes (\/)
// Each match may are may not have a trailing comma (,?). If one exists, it is removed before appending it to the list
const regex = new RegExp(/([\w\s-\/+\?;\[\]\(\)]+,?)/, 'g')
let labels = []
const labels = []
let groups
do {
groups = regex.exec(labelsString)
if (groups) {
// Removes the trailing comma and removes all whitespace
let label = groups[0].replace(',', '').trim()
labels.push(label)
const label = groups[0].replace(',', '').trim()
if (label !== '') {
labels.push(label)
}
}
} while (groups)
return labels
Expand Down

0 comments on commit 3e4e9cc

Please sign in to comment.