Skip to content

Commit

Permalink
Handle multiple entries (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangoslen authored Nov 18, 2023
1 parent 193b8ee commit 3d4d1ba
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 28 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
## [UNRELEASED]

### Refactored
- Refactors how a changelog gets updated to allow for multiple entries to be written in one invocation of the GitHub Action. This is prepatory work to allow for multi-package updates
- Refactors how a changelog gets updated to allow for multiple entries to be written in one invocation of the GitHub Action. This is preparatory work to allow for multi-package updates
- Refactors `entry-extractor` to return an array of `DependabotEntry` values to update the changelog with. This is preparatory work to allow for multi-package updates

## [3.6.0]
### Fixed
Expand Down
26 changes: 13 additions & 13 deletions __tests__/entry-extractor.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DependabotEntry, getDependabotEntry} from '../src/entry-extractor'
import {DependabotEntry, getDependabotEntries} from '../src/entry-extractor'

const PULL_REQUEST_EVENT = {
repository: {
Expand Down Expand Up @@ -83,63 +83,63 @@ const PULL_REQUEST_LOWER_CASE_UPDATE_WITH_DOCKER_PREFIX = {
}

test('extracts package and simple number verions', async () => {
const entry: DependabotEntry = getDependabotEntry(PULL_REQUEST_EVENT)
const entries = getDependabotEntries(PULL_REQUEST_EVENT)

const entry = entries[0]
expect(entry.package).toStrictEqual('package')
expect(entry.repository).toStrictEqual('owner/repo')
expect(entry.oldVersion).toStrictEqual('6.0')
expect(entry.newVersion).toStrictEqual('7.0')
})

test('extracts package and -alpha -beta versions', async () => {
const entry: DependabotEntry = getDependabotEntry(
PULL_REQUEST_EVENT_ALPHA_TO_BETA
)
const entries = getDependabotEntries(PULL_REQUEST_EVENT_ALPHA_TO_BETA)

const entry = entries[0]
expect(entry.package).toStrictEqual('package-with-dashes')
expect(entry.repository).toStrictEqual('owner/repo')
expect(entry.oldVersion).toStrictEqual('6.0-alpha')
expect(entry.newVersion).toStrictEqual('6.0-beta')
})

test('extracts package with odd package name', async () => {
const entry: DependabotEntry = getDependabotEntry(
PULL_REQUEST_EVENT_ODD_PACKAGE_NIL_REPO
)
const entries = getDependabotEntries(PULL_REQUEST_EVENT_ODD_PACKAGE_NIL_REPO)

const entry = entries[0]
expect(entry.package).toStrictEqual('@package-with_odd_characters+')
expect(entry.repository).toBeUndefined()
expect(entry.oldVersion).toStrictEqual('6.0')
expect(entry.newVersion).toStrictEqual('7.0')
})

test('extracts package with rust style requirement update', async () => {
const entry: DependabotEntry = getDependabotEntry(
const entries = getDependabotEntries(
PULL_REQUEST_EVENT_RUST_REQUIREMENT_UPDATE
)

const entry = entries[0]
expect(entry.package).toStrictEqual('clap')
expect(entry.repository).toStrictEqual('owner/repo')
expect(entry.oldVersion).toStrictEqual('~2')
expect(entry.newVersion).toStrictEqual('~4')
})

test('extracts package with prefix and lowercase bump', async () => {
const entry: DependabotEntry = getDependabotEntry(
PULL_REQUEST_LOWER_CASE_BUMP_WITH_PREFIX
)
const entries = getDependabotEntries(PULL_REQUEST_LOWER_CASE_BUMP_WITH_PREFIX)

const entry = entries[0]
expect(entry.package).toStrictEqual('package')
expect(entry.repository).toStrictEqual('owner/repo')
expect(entry.oldVersion).toStrictEqual('v2')
expect(entry.newVersion).toStrictEqual('v4')
})

test('extracts docker deps with prefix and lowercase update', async () => {
const entry: DependabotEntry = getDependabotEntry(
const entries = getDependabotEntries(
PULL_REQUEST_LOWER_CASE_UPDATE_WITH_DOCKER_PREFIX
)

const entry = entries[0]
expect(entry.package).toStrictEqual('deps')
expect(entry.repository).toStrictEqual('owner/repo')
expect(entry.oldVersion).toStrictEqual('v2')
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.
21 changes: 14 additions & 7 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.

7 changes: 4 additions & 3 deletions src/dependabot-helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {PathLike} from 'fs'
import * as core from '@actions/core'
import * as github from '@actions/github'
import {DependabotEntry, getDependabotEntry} from './entry-extractor'
import {getDependabotEntries} from './entry-extractor'
import {ChangelogUpdater} from './changelog-updater'

async function run(): Promise<void> {
Expand All @@ -21,8 +21,9 @@ async function run(): Promise<void> {

if (label !== '' && pullRequestHasLabel(label)) {
updater.readChangelog()
const entry: DependabotEntry = getDependabotEntry(github.context.payload)
await updater.updateChangelog(entry)
for (const entry of getDependabotEntries(github.context.payload)) {
await updater.updateChangelog(entry)
}
await updater.writeChangelog()
}
} catch (err) {
Expand Down
6 changes: 4 additions & 2 deletions src/entry-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@ export interface DependabotEntry {
newVersion: string
}

export function getDependabotEntry(event: WebhookPayload): DependabotEntry {
export function getDependabotEntries(event: WebhookPayload): DependabotEntry[] {
const pullRequestNumber: number = event.pull_request!.number
const repository: string | undefined = event.repository?.full_name
const titleResult = TITLE_REGEX.exec(event.pull_request!.title)
if (titleResult === null) {
throw new Error('Unable to extract entry from pull request title!')
}

return {
const entry = {
pullRequestNumber,
repository,
package: titleResult[1],
oldVersion: titleResult[2],
newVersion: titleResult[3]
}

return [entry]
}

0 comments on commit 3d4d1ba

Please sign in to comment.