diff --git a/entrypoint.sh b/entrypoint.sh index ef03418..f40db37 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -10,14 +10,14 @@ if !ENV["GITHUB_TOKEN"] exit(1) end -if ARGV[0].empty? +if ARGV[0]&.empty? puts "Missing message argument." exit(1) end commenter = Commenter.new(github: GitHub.new(env: ENV), message: ARGV[0], - check_for_dupes: ARGV[1], + check_for_duplicates: ARGV[1], duplicate_pattern: ARGV[2], delete_previous_pattern: ARGV[3]) diff --git a/lib/commenter.rb b/lib/commenter.rb index 73e526c..7acfa55 100644 --- a/lib/commenter.rb +++ b/lib/commenter.rb @@ -1,8 +1,8 @@ class Commenter - def initialize(github:, message:, check_for_dupes: true, duplicate_pattern: nil, delete_previous_pattern: nil) + def initialize(github:, message:, check_for_duplicates: true, duplicate_pattern: nil, delete_previous_pattern: nil) @github = github @message = message - @block_duplicates = check_for_dupes.to_s.downcase.strip == "true" + @block_duplicates = check_for_duplicates.to_s.downcase.strip == "true" @duplicate_pattern = !duplicate_pattern.to_s.empty? && Regexp.new(duplicate_pattern) @delete_previous_pattern = !delete_previous_pattern.to_s.empty? && Regexp.new(delete_previous_pattern) end @@ -15,16 +15,16 @@ def existing_duplicates? if @duplicate_pattern @github.comments.any? { |c| c["body"].match(/#{@duplicate_pattern}/) } else - @github.comments.any? { |c| c["body"] == message } + @github.comments.any? { |c| c["body"] == @message } end end def delete_matching_comments! return if !@delete_previous_pattern - comments.each do |comment| - if comment["body"].match(/#{@delete_prev_regex_msg}/) - @github.delete_comment(repo, comment["id"]) + @github.comments.each do |comment| + if comment["body"].match(/#{@delete_previous_pattern}/) + @github.delete_comment(@github.repo, comment["id"]) end end end diff --git a/spec/commenter_spec.rb b/spec/commenter_spec.rb new file mode 100644 index 0000000..c50bece --- /dev/null +++ b/spec/commenter_spec.rb @@ -0,0 +1,85 @@ + require "spec_helper" + + RSpec.describe Commenter do + let(:github) do + spy("GitHub", comments: [ + { "body" => "Oh, hi! I didn't see you there." }, + { "body" => "Ah, that's just because of my invisibility powers." } + ], + delete_comment: true) + end + + describe "#block_duplicates?" do + it "default" do + commenter = Commenter.new(github: github, message: "") + expect(commenter.block_duplicates?).to eq true + end + + it "given true" do + commenter = Commenter.new(github: github, message: "", check_for_duplicates: true) + expect(commenter.block_duplicates?).to eq true + end + + it "given 'true'" do + commenter = Commenter.new(github: github, message: "", check_for_duplicates: "true") + expect(commenter.block_duplicates?).to eq true + end + + it "given 'false'" do + commenter = Commenter.new(github: github, message: "", check_for_duplicates: false) + expect(commenter.block_duplicates?).to eq false + end + + it "given nil" do + commenter = Commenter.new(github: github, message: "", check_for_duplicates: nil) + expect(commenter.block_duplicates?).to eq false + end + end + + describe "#existing_duplicates?" do + context "default/no pattern" do + it "finds exact comment" do + commenter = Commenter.new(github: github, message: "Ah, that's just because of my invisibility powers.") + expect(commenter.existing_duplicates?).to eq true + end + + it "gives false when no matches" do + commenter = Commenter.new(github: github, message: "Today I will say a sentence never been said.") + expect(commenter.existing_duplicates?).to eq false + end + end + + context "given pattern" do + it "finds matching comment" do + commenter = Commenter.new(github: github, message: "Is anyone listening?", duplicate_pattern: "invisibility powers") + expect(commenter.existing_duplicates?).to eq true + end + + it "gives false when no matches" do + commenter = Commenter.new(github: github, message: "Is anyone listening?", duplicate_pattern: "common idioms") + expect(commenter.existing_duplicates?).to eq false + end + end + end + + describe "#delete_matching_comments!" do + it "does nothing by default" do + commenter = Commenter.new(github: github, message: "") + expect(commenter.delete_matching_comments!).to eq nil + expect(github).to have_received(:delete_comment).exactly(0).times + end + + it "does nothing given empty string" do + commenter = Commenter.new(github: github, message: "", delete_previous_pattern: "") + expect(commenter.delete_matching_comments!).to eq nil + expect(github).to have_received(:delete_comment).exactly(0).times + end + + it "deletes matching comments given pattern" do + commenter = Commenter.new(github: github, message: "", delete_previous_pattern: "invisibility") + expect(commenter.delete_matching_comments!).to_not eq nil + expect(github).to have_received(:delete_comment).once + end + end + + end diff --git a/spec/github_spec.rb b/spec/github_spec.rb index 41f994c..fc63f9c 100644 --- a/spec/github_spec.rb +++ b/spec/github_spec.rb @@ -1,4 +1,5 @@ require "spec_helper" +require "pry" RSpec.describe GitHub do describe "#pr_number" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 81f3d7a..208d196 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,7 +2,3 @@ require "octokit" require_relative "../lib/github" require_relative "../lib/commenter" - -def stub_env(env) - stub_const("ENV", ENV.to_hash.merge(env)) -end