Skip to content

Commit

Permalink
Merge pull request #158 from simplybusiness/generalise
Browse files Browse the repository at this point in the history
Generalise
  • Loading branch information
peaky76 authored Jan 12, 2024
2 parents 65d349f + 06ccd17 commit 0e76445
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

![Forget-me-not flower by Tauno Erik](images/flower.jpg)

A Github Action for Ruby projects that checks that the semantic version has been updated in a pull request.
A Github Action that checks that the semantic version has been updated in a pull request.

The aim is to remind engineers to update the version before merging, since this step is often forgotten and requires a retroactive fix.

It is operational on Ruby, Python and Javascript projects.

## Installation

1. Create a file called `.github/workflows/version-forget-me-not.yml` in your Gem's repository with the following YAML (modify as instructed in the comments):
1. Create a file called `.github/workflows/version-forget-me-not.yml` in your repository with the following YAML (modify as instructed in the comments):

```yaml
name: Version Forget-Me-Not
Expand All @@ -27,11 +29,13 @@ The aim is to remind engineers to update the version before merging, since this
build:
runs-on: ubuntu-20.04
steps:
- uses: simplybusiness/version-forget-me-not@v2.1.0
- uses: simplybusiness/version-forget-me-not@v2
env:
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Change to the file path where you keep the Gem's version.
# It is usually `lib/<gem name>/version.rb` or in the gemspec file.
# Change to the file path where you keep the version.
# It is usually `lib/<gem name>/version.rb` or in the gemspec file for Ruby.
# It is usually 'package.json' for Javascript/Typescript.
# It can often be 'pyproject.toml' for Python.
VERSION_FILE_PATH: "<PATH>"

```
Expand All @@ -40,6 +44,6 @@ The aim is to remind engineers to update the version before merging, since this

![Gem Version status check failing after initial installation](images/after-initial-installation.png)

1. Go to Settings → Branches → Your default branch → Mark `Gem Version` as required.
1. Go to Settings → Branches → Your default branch → Mark `Version check` as required.

![The required status check that needs to be ticked](images/required-status-checks.png)
13 changes: 7 additions & 6 deletions lib/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
class Action
attr_reader :client, :repo, :pull_number, :head_branch, :head_commit, :base_branch, :file_path, :failed_description

SEMVER_VERSION =
/["'](0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?["']/ # rubocop:disable Layout/LineLength
GEMSPEC_VERSION = Regexp.new(/\.version\s*=\s*/.to_s + SEMVER_VERSION.to_s).freeze
SEMVER = /["']*(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?["']*/ # rubocop:disable Layout/LineLength
SEPARATOR = /\s*[:=]\s*/
VERSION_KEY = /(^|\.|\s)version/
VERSION_SETTING = Regexp.new(VERSION_KEY.source + SEPARATOR.source + SEMVER.source, Regexp::IGNORECASE).freeze

def initialize(config)
@client = config.client
Expand All @@ -28,7 +29,7 @@ def check_version
puts "::error path#{file_path}=title=Failure::#{message}"
end

client.create_status(repo, head_commit, state, description: description, context: 'Gem Version')
client.create_status(repo, head_commit, state, description: description, context: 'Version check')
end

def version_increased?(branch_name:, trunk_name: 'master')
Expand All @@ -45,7 +46,7 @@ def version_increased?(branch_name:, trunk_name: 'master')

def fetch_version(ref:)
content = Base64.decode64(client.contents(repo, path: file_path, query: { ref: ref })['content'])
match = content.match(GEMSPEC_VERSION) || content.match(SEMVER_VERSION)
match = content.match(VERSION_SETTING)

format_version(match)
end
Expand All @@ -58,7 +59,7 @@ def fetch_version_safe(ref:)
end

def format_version(version)
Gem::Version.new(version[0].split('=').last.gsub(/\s/, '').gsub(/'|"/, ''))
Gem::Version.new(version[0].split(SEPARATOR).last.gsub(/\s/, '').gsub(/'|"/, ''))
end

def assign_pr_attributes(config)
Expand Down
6 changes: 3 additions & 3 deletions spec/action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'simplybusiness/test',
'1111',
'success',
context: 'Gem Version',
context: 'Version check',
description: 'Updated'
)
action.check_version
Expand All @@ -43,7 +43,7 @@
'simplybusiness/test',
'1111',
'failure',
context: 'Gem Version',
context: 'Version check',
description: "Update: #{config.file_path}"
)
action.check_version
Expand All @@ -58,7 +58,7 @@
'simplybusiness/test',
'1111',
'failure',
context: 'Gem Version',
context: 'Version check',
description: 'Version file not found on version.rb'
)
action.check_version
Expand Down

0 comments on commit 0e76445

Please sign in to comment.