diff --git a/README.md b/README.md index 53b122b..cb575b0 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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//version.rb` or in the gemspec file. + # Change to the file path where you keep the version. + # It is usually `lib//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: "" ``` @@ -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) diff --git a/lib/action.rb b/lib/action.rb index 732c7ea..849a0ef 100644 --- a/lib/action.rb +++ b/lib/action.rb @@ -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 @@ -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') @@ -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 @@ -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) diff --git a/spec/action_spec.rb b/spec/action_spec.rb index e6237ad..33398ee 100644 --- a/spec/action_spec.rb +++ b/spec/action_spec.rb @@ -31,7 +31,7 @@ 'simplybusiness/test', '1111', 'success', - context: 'Gem Version', + context: 'Version check', description: 'Updated' ) action.check_version @@ -43,7 +43,7 @@ 'simplybusiness/test', '1111', 'failure', - context: 'Gem Version', + context: 'Version check', description: "Update: #{config.file_path}" ) action.check_version @@ -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