From 78c9aea6939ae7b2268c172fb2e1525f9ca3d0d3 Mon Sep 17 00:00:00 2001 From: Richard Lee <14349+dlackty@users.noreply.github.com> Date: Fri, 19 Aug 2022 19:40:16 +0800 Subject: [PATCH] Avoid posting comments in non PR action runs --- lib/github_check_run_service.rb | 4 +++- spec/github_check_run_service_spec.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/github_check_run_service.rb b/lib/github_check_run_service.rb index 53e40b8..30d3932 100644 --- a/lib/github_check_run_service.rb +++ b/lib/github_check_run_service.rb @@ -28,7 +28,9 @@ def run @annotations.each_slice(MAX_ANNOTATIONS_SIZE) do |annotation| result.merge(client_patch_annotations(id, annotation)) # Don't need to merge twice - client_post_pull_requests(annotation[0]) + if @github_data[:pull_request_number] && !@github_data[:pull_request_number].empty? + client_post_pull_requests(annotation[0]) + end end end result diff --git a/spec/github_check_run_service_spec.rb b/spec/github_check_run_service_spec.rb index 960e14c..c470a99 100644 --- a/spec/github_check_run_service_spec.rb +++ b/spec/github_check_run_service_spec.rb @@ -6,7 +6,9 @@ let(:no_findings_brakeman_report) { JSON(File.read("./spec/fixtures/no_findings_report.json")) } let(:brakeman_report) { JSON(File.read("./spec/fixtures/report.json")) } let(:github_data) { { sha: "sha", token: "token", owner: "owner", repo: "repository_name", pull_request_number: "10" } } + let(:non_pr_github_data) { { sha: "sha", token: "token", owner: "owner", repo: "repository_name" } } let(:service) { GithubCheckRunService.new(brakeman_report, github_data, ReportAdapter) } + let(:non_pr_service) { GithubCheckRunService.new(brakeman_report, non_pr_github_data, ReportAdapter) } it "#run" do stub_request(:any, "https://api.github.com/repos/owner/repository_name/check-runs/id"). @@ -76,6 +78,17 @@ expect(service).to receive(:client_post_pull_requests).with(expected_comment_body) service.run end + + it "skips comments for non pr events" do + stub_request(:any, "https://api.github.com/repos/owner/repository_name/check-runs"). + to_return(status: 200, body: '{"id": "id"}') + + stub_request(:any, "https://api.github.com/repos/owner/repository_name/check-runs/id"). + to_return(status: 200, body: '{"id": "id"}') + + expect(non_pr_service).not_to receive(:client_post_pull_requests) + non_pr_service.run + end end # Eventually we'll have this comment, but for now, we don't want to fail with a mysterious github error