Skip to content

Commit

Permalink
Commenter specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronklaassen committed Dec 31, 2021
1 parent 3aaf224 commit c04fd18
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 12 deletions.
4 changes: 2 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
12 changes: 6 additions & 6 deletions lib/commenter.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
85 changes: 85 additions & 0 deletions spec/commenter_spec.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/github_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "spec_helper"
require "pry"

RSpec.describe GitHub do
describe "#pr_number" do
Expand Down
4 changes: 0 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c04fd18

Please sign in to comment.