Skip to content

Commit ac71446

Browse files
committed
Add ignored files part
1 parent 50beb25 commit ac71446

File tree

2 files changed

+91
-53
lines changed

2 files changed

+91
-53
lines changed
Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,51 @@
11
#!/usr/bin/env ruby
22

3-
require 'json'
3+
# frozen_string_literal: true
44

5-
current_stats = JSON.parse(File.read(ENV['CURRENT_STATS_PATH']), symbolize_names: true)
6-
base_stats = JSON.parse(File.read(ENV['BASE_STATS_PATH']), symbolize_names: true)
5+
require "json"
6+
7+
head_stats = JSON.parse(File.read(ENV["CURRENT_STATS_PATH"]), symbolize_names: true)
8+
base_stats = JSON.parse(File.read(ENV["BASE_STATS_PATH"]), symbolize_names: true)
79

810
# If a file is added in contrib, currently the paths will have no diff.
9-
ignored_files_paths_added = current_stats[:ignored_files][:paths] - base_stats[:ignored_files][:paths]
10-
ignored_files_paths_removed = base_stats[:ignored_files][:paths] - current_stats[:ignored_files][:paths]
11-
12-
steep_ignores_added = current_stats[:steep_ignore_comments] - base_stats[:steep_ignore_comments]
13-
steep_ignores_removed = base_stats[:steep_ignore_comments] - current_stats[:steep_ignore_comments]
14-
15-
untyped_methods_added = current_stats[:untyped_methods] - base_stats[:untyped_methods]
16-
untyped_methods_removed = base_stats[:untyped_methods] - current_stats[:untyped_methods]
17-
18-
partially_typed_methods_added = current_stats[:partially_typed_methods] - base_stats[:partially_typed_methods]
19-
partially_typed_methods_removed = base_stats[:partially_typed_methods] - current_stats[:partially_typed_methods]
20-
21-
untyped_others_added = current_stats[:untyped_others] - base_stats[:untyped_others]
22-
untyped_others_removed = base_stats[:untyped_others] - current_stats[:untyped_others]
23-
24-
partially_typed_others_added = current_stats[:partially_typed_others] - base_stats[:partially_typed_others]
25-
partially_typed_others_removed = base_stats[:partially_typed_others] - current_stats[:partially_typed_others]
26-
27-
diff_stats = {
28-
ignored_files: {
29-
added: ignored_files_paths_added,
30-
removed: ignored_files_paths_removed
31-
},
32-
steep_ignores: {
33-
added: steep_ignores_added,
34-
removed: steep_ignores_removed
35-
},
36-
methods: {
37-
untyped: {
38-
added: untyped_methods_added,
39-
removed: untyped_methods_removed
40-
},
41-
partially_typed: {
42-
added: partially_typed_methods_added,
43-
removed: partially_typed_methods_removed
44-
}
45-
},
46-
others: {
47-
untyped: {
48-
added: untyped_others_added,
49-
removed: untyped_others_removed
50-
},
51-
partially_typed: {
52-
added: partially_typed_others_added,
53-
removed: partially_typed_others_removed
54-
}
55-
}
11+
ignored_files = {
12+
added: head_stats[:ignored_files][:paths] - base_stats[:ignored_files][:paths],
13+
removed: base_stats[:ignored_files][:paths] - head_stats[:ignored_files][:paths]
5614
}
5715

58-
puts diff_stats.to_json
16+
def ignored_files_summary(head_stats, base_stats)
17+
# This will skip the summary if files are added/removed from contrib folders for now.
18+
ignored_files_added = head_stats[:ignored_files][:paths] - base_stats[:ignored_files][:paths]
19+
ignored_files_removed = base_stats[:ignored_files][:paths] - head_stats[:ignored_files][:paths]
20+
return nil if ignored_files_added.empty? && ignored_files_removed.empty?
21+
22+
typed_files_percentage_base = ((base_stats[:total_files_size] - base_stats[:ignored_files][:size]) / base_stats[:total_files_size].to_f * 100).round(2)
23+
typed_files_percentage_head = ((head_stats[:total_files_size] - head_stats[:ignored_files][:size]) / head_stats[:total_files_size].to_f * 100).round(2)
24+
25+
summary = +"This PR "
26+
summary << "adds **#{ignored_files_added.size}** ignored files " if ignored_files_added.any?
27+
summary << "and " if ignored_files_added.any? && ignored_files_removed.any?
28+
summary << "removes **#{ignored_files_removed.size}** ignored files " if ignored_files_removed.any?
29+
if typed_files_percentage_base != typed_files_percentage_head
30+
summary << "which #{(typed_files_percentage_base > typed_files_percentage_head) ? "decreases" : "increases"} the percentage of typed files from #{typed_files_percentage_base}% to #{typed_files_percentage_head}% (**#{typed_files_percentage_head - typed_files_percentage_base}**%)"
31+
end
32+
summary << "."
33+
34+
<<~IGNORED_FILES
35+
### Ignored files
36+
#{summary}
37+
<details><summary>Ignored files</summary>
38+
#{"<em>Added:</em>" if ignored_files_added.any?}
39+
#{"<pre><code>#{ignored_files_added.join("\n")}</code></pre>" if ignored_files_added.any?}
40+
#{"<em>Removed:</em>" if ignored_files_removed.any?}
41+
#{"<pre><code>#{ignored_files_removed.join("\n")}</code></pre>" if ignored_files_removed.any?}
42+
</details>
43+
44+
*__Note__: Ignored files are excluded from the next sections.*
45+
46+
IGNORED_FILES
47+
end
48+
49+
result = +""
50+
result << ignored_files_summary(head_stats, base_stats)
51+
result

.github/workflows/typing-stats.yml

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ jobs:
1515
pull-requests: write
1616
runs-on: ubuntu-24.04
1717
steps:
18+
- name: Find existing comment
19+
id: comment
20+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
21+
with:
22+
github-token: ${{secrets.GITHUB_TOKEN}}
23+
script: |
24+
const options = github.rest.issues.listComments.endpoint.merge({
25+
owner: context.repo.owner,
26+
repo: context.repo.repo,
27+
issue_number: context.payload.pull_request.number,
28+
});
29+
const comments = await github.paginate(options)
30+
const comment = comments.find((cmnt) => {
31+
return cmnt.body.startsWith("<!-- TYPING_STATS_HIDDEN_MARKER -->")
32+
})
33+
34+
return undefined === comment ? null : comment.id
35+
1836
- name: Checkout current branch
1937
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2038
with:
@@ -58,5 +76,32 @@ jobs:
5876
env:
5977
CURRENT_STATS_PATH: ${{ runner.temp }}/typing-stats-current.json
6078
BASE_STATS_PATH: ${{ runner.temp }}/typing-stats-base.json
61-
# run: ruby ${{ runner.temp }}/typing_stats_compare.rb >> "${{ runner.temp }}/typing-stats-compare.json"
62-
run: ruby ${{ runner.temp }}/typing_stats_compare.rb
79+
run: ruby ${{ runner.temp }}/typing_stats_compare.rb >> "${{ runner.temp }}/typing-stats-compare.md"
80+
81+
- name: Write comment
82+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
83+
with:
84+
github-token: ${{secrets.GITHUB_TOKEN}}
85+
script: |
86+
var fs = require('fs')
87+
88+
const previousCommentId = ${{steps.comment.outputs.result}}
89+
const commentContent = fs.readFileSync("${{ runner.temp }}/typing-stats-compare.md", "utf8")
90+
const commentBody = `<!-- TYPING_STATS_HIDDEN_MARKER -->
91+
## Typing analysis
92+
93+
${commentContent}
94+
`
95+
96+
const options = {
97+
owner: context.repo.owner,
98+
repo: context.repo.repo,
99+
issue_number: context.payload.pull_request.number,
100+
body: commentBody
101+
}
102+
103+
if (null === previousCommentId) {
104+
await github.rest.issues.createComment(options)
105+
} else {
106+
await github.rest.issues.updateComment({...options, comment_id: previousCommentId})
107+
}

0 commit comments

Comments
 (0)