Skip to content

Commit 389c60f

Browse files
authored
Post/update incremental coverage report on PRs. (#8186)
* Post/update incremental coverage report on PRs.
1 parent acc2d15 commit 389c60f

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# USAGE: git diff -U0 [base_commit] HEAD | get_diff_lines.sh
16+
#
17+
# This will generate a JSON output of changed files and their newly added
18+
# lines.
19+
20+
require 'octokit'
21+
require 'json'
22+
23+
COMMENT_HEADER = "### Incremental code coverage report"
24+
REMOVE_PATTERN = /### Incremental code coverage report/
25+
REPO = ENV['GITHUB_REPOSITORY']
26+
GITHUB_WORKFLOW_URL = "https://github.com/#{REPO}/actions/runs/#{ENV['GITHUB_RUN_ID']}"
27+
UNCOVERED_LINE_FILE = ENV["UNCOVERED_LINE_FILE"]
28+
TESTING_COMMIT = ENV["TESTING_COMMIT"]
29+
PULL_REQUEST = ENV["PULL_REQUEST"].to_i
30+
31+
client = Octokit::Client.new(access_token: ENV["INPUT_ACCESS_TOKEN"])
32+
uncovered_files = JSON.parse(File.read(UNCOVERED_LINE_FILE))
33+
34+
# Clean comments matching REMOVE_PATTERN.
35+
def clean_coverage_comments(client)
36+
comment_page = 0
37+
loop do
38+
comment_page += 1
39+
cur_page_comment = client.pull_request_comments(REPO, PULL_REQUEST, { :per_page =>100, :page => comment_page })
40+
if cur_page_comment.length == 0
41+
break
42+
end
43+
for cmt in cur_page_comment do
44+
# Remove comments when the comment body meets the REMOVE_PATTERN.
45+
if cmt.body =~ REMOVE_PATTERN
46+
client.delete_pull_request_comment(REPO,cmt.id)
47+
end
48+
end
49+
end
50+
end
51+
52+
def generate_comment(comment_header, xcresult_file)
53+
body = "Tests for New code lines are not detected in [#{xcresult_file}](#{GITHUB_WORKFLOW_URL}), please add tests on highlighted lines."
54+
return "#{comment_header} \n #{body}"
55+
end
56+
57+
def add_coverage_comments(client, uncovered_files)
58+
for changed_file in uncovered_files do
59+
coverage_line = changed_file['coverage']
60+
xcresult_file = changed_file['xcresultBundle'].split('/').last
61+
start_line = -1
62+
coverage_line.each_with_index do |line, idx|
63+
# Init start_line to the first uncovered line of a file.
64+
if start_line == -1
65+
start_line = line
66+
end
67+
if idx < coverage_line.length() && line + 1 == coverage_line[idx+1]
68+
next
69+
else
70+
comment = generate_comment(COMMENT_HEADER, xcresult_file)
71+
if start_line == line
72+
# One line code comment will only rely on the position param, which is
73+
# 'line' here.
74+
client.create_pull_request_comment(REPO,PULL_REQUEST, comment, TESTING_COMMIT,changed_file['fileName'], line, {:side=>"RIGHT"})
75+
else
76+
# multiple-line code block comment needs start_line and line options,
77+
# which will override the position param.
78+
client.create_pull_request_comment(REPO,PULL_REQUEST, comment, TESTING_COMMIT,changed_file['fileName'],0, {:side=>"RIGHT", :start_line=> start_line, :line=> line})
79+
end
80+
start_line = coverage_line[idx+1]
81+
end
82+
end
83+
end
84+
end
85+
86+
clean_coverage_comments(client)
87+
add_coverage_comments(client, uncovered_files)

0 commit comments

Comments
 (0)