diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..e674c38 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,22 @@ +on: + pull_request: + push: + branches: [ main ] + +jobs: + remove_before_launch: + runs-on: ubuntu-latest + name: Remove before launch + + steps: + - name: Checkout + uses: actions/checkout@v5 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: .ruby-version + bundler-cache: true + + - name: Run Ruby tests + run: rake test diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..4ae6486 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +ruby-3.4.6 diff --git a/Dockerfile b/Dockerfile index f21591f..6dbe8a7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,4 @@ FROM ruby:3.4.6-alpine COPY remove_before_launch.rb /remove_before_launch.rb +COPY /lib /lib ENTRYPOINT ["ruby", "/remove_before_launch.rb"] diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..97eae5a --- /dev/null +++ b/Gemfile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } + +gem "minitest" +gem "rake" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..5cfeea6 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,16 @@ +GEM + remote: https://rubygems.org/ + specs: + minitest (5.26.0) + rake (13.3.0) + +PLATFORMS + arm64-darwin-25 + x86_64-linux + +DEPENDENCIES + minitest + rake + +BUNDLED WITH + 2.7.1 diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..56926c6 --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +require "minitest/test_task" + +Minitest::TestTask.create(:test) do |t| + t.libs << "test" + t.warning = false + t.verbose = true + t.test_globs = ["test/**/*_test.rb"] +end diff --git a/lib/pull_request_body.rb b/lib/pull_request_body.rb new file mode 100644 index 0000000..1f85dc0 --- /dev/null +++ b/lib/pull_request_body.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class PullRequestBody + INCOMPLETE_TODO_REGEX = /^\s*-\s+\[\s\]\s+(.+)$/.freeze + + def initialize(text:) + @text = text + end + + def incomplete_todos + @text.scan(INCOMPLETE_TODO_REGEX).map(&:first) + end +end diff --git a/remove_before_launch.rb b/remove_before_launch.rb index f69ddb3..9aae4d7 100644 --- a/remove_before_launch.rb +++ b/remove_before_launch.rb @@ -1,18 +1,16 @@ # frozen_string_literal: true -REGEX = /^\s*-\s+\[\s\]\s+(.+)$/.freeze +require_relative "lib/pull_request_body" -pull_request_body = ARGV[0] +pull_request_body = PullRequestBody.new(text: ARGV[0]) -incomplete_todos = pull_request_body.scan(REGEX) - -if incomplete_todos.empty? +if pull_request_body.incomplete_todos.empty? exit 0 else puts "⚠️ There are still uncompleted todos that need your attention:\n\n" - incomplete_todos.each do |incomplete_todo| - puts "□ #{incomplete_todo.first}\n" + pull_request_body.incomplete_todos.each do |incomplete_todo| + puts "□ #{incomplete_todo}\n" end exit 1 diff --git a/test/pull_request_body_test.rb b/test/pull_request_body_test.rb new file mode 100644 index 0000000..e0a5a3c --- /dev/null +++ b/test/pull_request_body_test.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require "minitest/autorun" +require_relative "../lib/pull_request_body" + +class PullRequestBodyTest < Minitest::Test + TEST_MARKDOWN = <<~MARKDOWN + This is a test to see if the parser can look for the different unchecked TODOs that can be bundled in a PR description. + + - [ ] Good syntax + - [x] This is a checked TODO + - [ ] Nested + - [ ] Bad syntax: extra space after the dash + + MARKDOWN + + def test_find_variously_formatted_incomplete_todos + pull_request_body = PullRequestBody.new(text: TEST_MARKDOWN) + incomplete_todos = pull_request_body.incomplete_todos + + assert incomplete_todos.length, 3 + assert incomplete_todos == ["Good syntax", "Nested", "Bad syntax: extra space after the dash"] + end +end