diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..ffe3662 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,19 @@ +name: Tests + +on: + pull_request: + types: [opened, reopened, synchronize] + branches: + - main + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.3' + - run: ruby main.rb + env: + FILENAME: test.txt diff --git a/README.md b/README.md index 6e7b125..77394ad 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # version-check -Checks if version is upgraded when updating the repository +Checks if version is upgraded when updating the repository. Pass regex for version strings and a filename to test if it has been updated. + +## Set version format regex + +I use ruby regex (rubular) for the version strings. The default value is `/[v]\d.\d.\d/` (which means e.g. v1.0.0). You can test your own formats at: https://rubular.com/. diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..6ec865a --- /dev/null +++ b/action.yml @@ -0,0 +1,25 @@ +name: "version-checker" +description: "Checks if you updated the version in your code." +inputs: + filename: + description: "Filename that contains the version." + required: true + version-regex: + description: "Ruby regex of the versions (e.g. /[v]\d.\d for v1.0)" + required: false + default: "/[v]\d.\d.\d/" + +branding: + icon: "package" + color: "red" +runs: + using: "composite" + steps: + - uses: actions/checkout@v4 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.3' + - run: ruby main.rb + env: + VERSION_REGEX: ${{ inputs.version-regex }} + FILENAME: ${{ inputs.filename }} diff --git a/main.rb b/main.rb new file mode 100644 index 0000000..c9db91e --- /dev/null +++ b/main.rb @@ -0,0 +1,53 @@ +#!/usr/bin/ruby + +class Project + + attr_reader :code_tags, :git_tags + + def initialize(filename, version_regex) + # variables + @filename = filename + @version_regex = version_regex + # upon init + @code_tags = [] + @git_tags = [] + read_versions + get_tags + end + + def read_versions() + File.open(@filename, "r") do |f| + f.each_line do |line| + @code_tags += line.scan(@version_regex) + end + end + end + + def get_tags() + @git_tags = Dir.entries(".git/refs/tags/").select{ |i| i[@version_regex] } + end + + def version_updated() + for code_tag in @code_tags do + if not @git_tags.include? code_tag + return true + end + end + return false + end + +end + +begin + regex = ENV["VERSION_REGEX"] || "/[v]\\d.\\d.\\d/" + filename = ENV["FILENAME"] || "" + + if filename.empty? + raise "No filename passed as env." + end + + project = Project.new(filename, eval(regex)) + if not project.version_updated + raise "Version in code not updated." + end +end \ No newline at end of file diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..5b213d6 --- /dev/null +++ b/test.txt @@ -0,0 +1,10 @@ +something +something +something +something +version=v1.0.0 +something +else + + +version=v1.0.1