From 679119afe1fc3a05bc372f775cd42ebba8d967ef Mon Sep 17 00:00:00 2001 From: Barry Gordon Date: Wed, 26 May 2021 17:18:43 +0100 Subject: [PATCH] Add a lib to extract update metadata from commit messages --- src/dependabot/update_metadata.test.ts | 101 +++++++++++++++++++++++++ src/dependabot/update_metadata.ts | 27 +++++++ 2 files changed, 128 insertions(+) create mode 100644 src/dependabot/update_metadata.test.ts create mode 100644 src/dependabot/update_metadata.ts diff --git a/src/dependabot/update_metadata.test.ts b/src/dependabot/update_metadata.test.ts new file mode 100644 index 0000000..7d7d00e --- /dev/null +++ b/src/dependabot/update_metadata.test.ts @@ -0,0 +1,101 @@ +import * as updateMetadata from './update_metadata' + +test('it returns an empty array for a blank string', async () => { + expect(updateMetadata.parse('')).toEqual([]) +}) + +test('it returns an empty array for commit message with no dependabot yaml fragment', async () => { + const commitMessage = `Bumps [coffee-rails](https://github.com/rails/coffee-rails) from 4.0.1 to 4.2.2. + - [Release notes](https://github.com/rails/coffee-rails/releases) + - [Changelog](https://github.com/rails/coffee-rails/blob/master/CHANGELOG.md) + - [Commits](rails/coffee-rails@v4.0.1...v4.2.2) + + Signed-off-by: dependabot[bot] ` + + expect(updateMetadata.parse(commitMessage)).toEqual([]) +}) + +test('it returns the updated dependency information when there is a yaml fragment', async () => { + const commitMessage = `Bumps [coffee-rails](https://github.com/rails/coffee-rails) from 4.0.1 to 4.2.2. + - [Release notes](https://github.com/rails/coffee-rails/releases) + - [Changelog](https://github.com/rails/coffee-rails/blob/master/CHANGELOG.md) + - [Commits](rails/coffee-rails@v4.0.1...v4.2.2) + + --- + updated-dependencies: + - dependency-name: coffee-rails + dependency-type: direct:production + update-type: version-update:semver-minor + ... + + Signed-off-by: dependabot[bot] ` + + const updatedDependencies = updateMetadata.parse(commitMessage) + + expect(updatedDependencies).toHaveLength(1) + + expect(updatedDependencies[0].name).toEqual('coffee-rails') + expect(updatedDependencies[0].type).toEqual('direct:production') + expect(updatedDependencies[0].updateType).toEqual('version-update:semver-minor') +}) + +test('it supports multiple dependencies within a single fragment', async () => { + const commitMessage = `Bumps [coffee-rails](https://github.com/rails/coffee-rails) from 4.0.1 to 4.2.2. + - [Release notes](https://github.com/rails/coffee-rails/releases) + - [Changelog](https://github.com/rails/coffee-rails/blob/master/CHANGELOG.md) + - [Commits](rails/coffee-rails@v4.0.1...v4.2.2) + + --- + updated-dependencies: + - dependency-name: coffee-rails + dependency-type: direct:production + update-type: version-update:semver-minor + - dependency-name: coffeescript + dependency-type: indirect:production + update-type: version-update:semver-patch + ... + + Signed-off-by: dependabot[bot] ` + + const updatedDependencies = updateMetadata.parse(commitMessage) + + expect(updatedDependencies).toHaveLength(2) + + expect(updatedDependencies[0].name).toEqual('coffee-rails') + expect(updatedDependencies[0].type).toEqual('direct:production') + expect(updatedDependencies[0].updateType).toEqual('version-update:semver-minor') + + expect(updatedDependencies[1].name).toEqual('coffeescript') + expect(updatedDependencies[1].type).toEqual('indirect:production') + expect(updatedDependencies[1].updateType).toEqual('version-update:semver-patch') +}) + +test('it only returns information within the first fragment if there are multiple yaml documents', async () => { + const commitMessage = `Bumps [coffee-rails](https://github.com/rails/coffee-rails) from 4.0.1 to 4.2.2. + - [Release notes](https://github.com/rails/coffee-rails/releases) + - [Changelog](https://github.com/rails/coffee-rails/blob/master/CHANGELOG.md) + - [Commits](rails/coffee-rails@v4.0.1...v4.2.2) + + --- + updated-dependencies: + - dependency-name: coffee-rails + dependency-type: direct:production + update-type: version-update:semver-minor + ... + --- + updated-dependencies: + - dependency-name: coffeescript + dependency-type: indirect:production + update-type: version-update:semver-patch + ... + + Signed-off-by: dependabot[bot] ` + + const updatedDependencies = updateMetadata.parse(commitMessage) + + expect(updatedDependencies).toHaveLength(1) + + expect(updatedDependencies[0].name).toEqual('coffee-rails') + expect(updatedDependencies[0].type).toEqual('direct:production') + expect(updatedDependencies[0].updateType).toEqual('version-update:semver-minor') +}) diff --git a/src/dependabot/update_metadata.ts b/src/dependabot/update_metadata.ts new file mode 100644 index 0000000..45b6204 --- /dev/null +++ b/src/dependabot/update_metadata.ts @@ -0,0 +1,27 @@ +import * as YAML from 'yaml' + +interface updatedDependency { + name: string, + type: string, + updateType: string, +} + +export function parse (commitMessage: string): Array { + const yamlFragment = commitMessage.match(/-{3}\n(?[\S|\s]*?)(?=\s*\.{3}\n)/m) + + if (yamlFragment?.groups) { + const data = YAML.parse(yamlFragment.groups.dependencies) + + if (data['updated-dependencies']) { + return data['updated-dependencies'].map(dependency => { + return { + name: dependency['dependency-name'], + type: dependency['dependency-type'], + updateType: dependency['update-type'] + } + }) + } + } + + return [] +}