Skip to content

Commit 41f16f6

Browse files
authored
Add option to support closed-but-not-merged PRs (#72)
Signed-off-by: Phil Dibowitz <phil@ipom.com>
1 parent 92abc8e commit 41f16f6

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

examples/repo_stats_config.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
log_level :info
1414
ci_timeout 600
1515
include_list false
16+
# by default we don't count unmerged-but-closed PRs as in "closed PRs"
17+
# but some workflows push commits that reference the PR as a way of
18+
# "merging" the PR, and if your workflow does so, you can set this so
19+
# that we count such PRs as "merged."
20+
count_unmerged_prs false
1621

1722
# the most interesting part about this config file
1823
# is the organizations block. It allows you to specify

lib/oss_stats/config/repo_stats.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module RepoStats
3636
buildkite_token nil
3737
limit_gh_ops_per_minute nil
3838
include_list false
39+
count_unmerged_prs false
3940
mode ['all']
4041
organizations({})
4142

lib/oss_stats/repo_stats.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,14 @@ def get_pr_and_issue_stats(gh_client, options)
106106
# window
107107
next unless closed_date && closed_date >= cutoff_date
108108

109-
# if it's a PR make sure it was closed by merging
110-
next unless !is_pr || item.pull_request.merged_at
109+
# Only count this PR/Issue as closed depending on settings.
110+
if is_pr
111+
unless OssStats::Config::RepoStats.count_unmerged_prs
112+
# Default: only count PRs that were merged
113+
next unless item.pull_request && item.pull_request.merged_at
114+
end
115+
# when count_unmerged_prs is true we allow closed-but-unmerged PRs
116+
end
111117

112118
# anything closed this week counts as closed regardless of when it
113119
# was opened
@@ -793,6 +799,12 @@ def parse_options
793799
'Disable Markdown links in the output (default: false)',
794800
) { options[:no_links] = true }
795801

802+
opts.on(
803+
'--count-unmerged-prs',
804+
'Count PRs that were closed without merging as closed' +
805+
' (default: false)',
806+
) { options[:count_unmerged_prs] = true }
807+
796808
opts.on(
797809
'--mode MODE',
798810
Array,

spec/repo_stats_spec.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,66 @@
9292
expect(stats[:issue][:closed]).to eq(0)
9393
end
9494

95+
it 'does not count closed-but-unmerged PRs by default' do
96+
# closed PR but not merged
97+
allow(client).to receive(:issues).with(
98+
'test_org/test_repo',
99+
hash_including(page: 1),
100+
).and_return(
101+
[
102+
double(
103+
created_at: Date.today - 7,
104+
closed_at: Date.today - 2,
105+
pull_request: double(merged_at: nil),
106+
updated_at: Date.today - 1,
107+
labels: [],
108+
),
109+
],
110+
)
111+
allow(client).to receive(:issues).with(
112+
'test_org/test_repo',
113+
hash_including(page: 2),
114+
).and_return([])
115+
116+
# ensure default is false
117+
OssStats::Config::RepoStats.count_unmerged_prs = false
118+
119+
stats = get_pr_and_issue_stats(client, options)
120+
expect(stats[:pr][:closed]).to eq(0)
121+
122+
# reset
123+
OssStats::Config::RepoStats.count_unmerged_prs = false
124+
end
125+
126+
it 'counts closed-but-unmerged PRs when configured' do
127+
allow(client).to receive(:issues).with(
128+
'test_org/test_repo',
129+
hash_including(page: 1),
130+
).and_return(
131+
[
132+
double(
133+
created_at: Date.today - 7,
134+
closed_at: Date.today - 2,
135+
pull_request: double(merged_at: nil),
136+
updated_at: Date.today - 1,
137+
labels: [],
138+
),
139+
],
140+
)
141+
allow(client).to receive(:issues).with(
142+
'test_org/test_repo',
143+
hash_including(page: 2),
144+
).and_return([])
145+
146+
OssStats::Config::RepoStats.count_unmerged_prs = true
147+
148+
stats = get_pr_and_issue_stats(client, options)
149+
expect(stats[:pr][:closed]).to eq(1)
150+
151+
# reset
152+
OssStats::Config::RepoStats.count_unmerged_prs = false
153+
end
154+
95155
it 'handles empty responses gracefully' do
96156
allow(client).to receive(:issues).and_return([])
97157

0 commit comments

Comments
 (0)