Skip to content

Commit f9806ea

Browse files
authored
Compare all updates (#7)
1 parent d6010b8 commit f9806ea

File tree

8 files changed

+238
-68
lines changed

8 files changed

+238
-68
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,25 @@ on:
33
push:
44
workflow_dispatch:
55
inputs:
6-
name:
7-
description: 'Name of gem to compare'
8-
required: false
9-
type: string
10-
from:
11-
description: 'From version'
12-
required: false
13-
type: string
14-
to:
15-
description: 'To version'
16-
required: false
17-
type: string
186
pr:
19-
description: 'The number of the pull request to comment in'
7+
description: "The number of the pull request to comment in"
208
required: false
219
type: number
2210

11+
2312
jobs:
2413
ci:
2514
runs-on: ubuntu-latest
2615
steps:
2716
- uses: actions/checkout@v3
17+
- uses: ruby/setup-ruby@v1
18+
with:
19+
ruby-version: 3.2
20+
21+
- run: rake
22+
2823
- uses: ./
2924
with:
30-
pr_title: "Bump ${{ inputs.name || 'pg' }} from ${{ inputs.from || '1.4.0' }} to ${{ inputs.to || '1.4.1' }}"
31-
pr_number: ${{ inputs.pr || 1 }}
25+
pr_diff: test/fixtures/sentry.diff
26+
pr_number: ${{ inputs.pr || 2 }}
3227
github-token: ${{ secrets.GITHUB_TOKEN }}

Rakefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
require "rake/testtask"
4+
5+
Rake::TestTask.new do |t|
6+
t.libs << "test"
7+
t.test_files = FileList["test/*_test.rb"]
8+
t.options = "--verbose=normal" unless ENV.key?("TESTOPTS")
9+
end
10+
11+
task default: :test

action.yml

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ inputs:
88
description: "Version of the gem-compare plugin"
99
default: "1.2.1"
1010
required: false
11-
pr_title:
12-
description: "String with gem name and from/to versions"
13-
default: "Bump pg from 1.4.0 to 1.4.1"
11+
pr_diff:
12+
description: "Output from 'git diff' for Gemfile.lock"
13+
default: ""
1414
required: false
1515
pr_number:
1616
description: "The number of the pull request to comment in"
@@ -25,68 +25,46 @@ runs:
2525
steps:
2626
- uses: ruby/setup-ruby@v1
2727
with:
28-
ruby-version: 3.1
29-
30-
- name: Extract info from pull request
31-
shell: bash
32-
run: |
33-
echo "PR_NUMBER=${{ github.event.pull_request.number || inputs.pr_number }}" >> $GITHUB_ENV
34-
echo "PR_TITLE=${{ github.event.pull_request.title || inputs.pr_title }}" >> $GITHUB_ENV
28+
ruby-version: 3.2
3529

3630
# Ensure our scripts can be found when running the action
3731
# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action#creating-an-action-metadata-file
3832
- run: echo "${{ github.action_path }}" >> $GITHUB_PATH
3933
shell: bash
4034

41-
- name: Parse pull request title
35+
- name: Get pull request number
4236
shell: bash
4337
run: |
44-
echo "COMPARE_GEM=$(echo $PR_TITLE | parse_title name)" >> $GITHUB_ENV
45-
echo "COMPARE_FROM_VERSION=$(echo $PR_TITLE | parse_title from)" >> $GITHUB_ENV
46-
echo "COMPARE_TO_VERSION=$(echo $PR_TITLE | parse_title to)" >> $GITHUB_ENV
38+
echo "PR_NUMBER=${{ github.event.pull_request.number || inputs.pr_number }}" >> $GITHUB_ENV
4739
48-
- name: Post Diffend URL
40+
- name: Get pull request diff from input
41+
if: ${{ inputs.pr_diff != '' }}
4942
shell: bash
50-
env:
51-
GITHUB_TOKEN: ${{ inputs.github-token }}
5243
run: |
53-
echo "https://my.diffend.io/gems/$COMPARE_GEM/$COMPARE_FROM_VERSION/$COMPARE_TO_VERSION" >> comment0.txt
54-
with_retries gh pr comment $PR_NUMBER --body-file comment0.txt --repo ${{ github.repository }}
44+
{
45+
echo 'PR_DIFF<<EOF'
46+
cat ${{ inputs.pr_diff }}
47+
echo EOF
48+
} >> "$GITHUB_ENV"
5549
56-
- name: Run gem install gem-compare
50+
- name: Get pull request diff from repo
51+
if: ${{ inputs.pr_diff == '' }}
5752
shell: bash
5853
run: |
59-
with_retries gem install gem-compare --version ${{ inputs.gem_compare_version }} || true
54+
{
55+
echo 'PR_DIFF<<EOF'
56+
git diff HEAD^ HEAD -- Gemfile.lock
57+
echo EOF
58+
} >> "$GITHUB_ENV"
6059
61-
- name: Run gem compare
60+
- name: Run gem install gem-compare
6261
shell: bash
6362
run: |
64-
with_retries gem compare $COMPARE_GEM $COMPARE_FROM_VERSION $COMPARE_TO_VERSION > compare_output.txt || true
63+
with_retries gem install gem-compare --version ${{ inputs.gem_compare_version }} || true
6564
66-
- name: Run gem compare --diff
65+
- name: Run compare steps
6766
shell: bash
6867
run: |
69-
with_retries gem compare --diff $COMPARE_GEM $COMPARE_FROM_VERSION $COMPARE_TO_VERSION > compare_diff.txt || true
70-
71-
- name: Run gh pr comment
72-
shell: bash
68+
compare_gem_versions
7369
env:
7470
GITHUB_TOKEN: ${{ inputs.github-token }}
75-
run: |
76-
echo "## <code>gem compare $COMPARE_GEM $COMPARE_FROM_VERSION $COMPARE_TO_VERSION</code>" > comment1.txt
77-
echo '' >> comment1.txt
78-
echo '````ruby' >> comment1.txt
79-
cat compare_output.txt >> comment1.txt
80-
echo '````' >> comment1.txt
81-
echo '' >> comment1.txt
82-
83-
echo "<Details><Summary><h2><code>gem compare --diff"\
84-
"$COMPARE_GEM $COMPARE_FROM_VERSION $COMPARE_TO_VERSION</code></h2></Summary>" >> comment2.txt
85-
echo '' >> comment2.txt
86-
echo '````ruby' >> comment2.txt
87-
cat compare_diff.txt >> comment2.txt
88-
echo '````' >> comment2.txt
89-
echo '</Details>' >> comment2.txt
90-
91-
with_retries gh pr comment $PR_NUMBER --body-file comment1.txt --repo ${{ github.repository }} || true
92-
gh pr comment $PR_NUMBER --body-file comment2.txt --repo ${{ github.repository }} || true

compare_gem_versions

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/usr/bin/env ruby
2+
3+
require "json"
4+
require "open3"
5+
require "stringio"
6+
require "tempfile"
7+
8+
require_relative "lib/parse_diff"
9+
10+
def to_file(str)
11+
file = Tempfile.new("gem-compare-action")
12+
file.write(str)
13+
file.rewind
14+
file.path
15+
end
16+
17+
def diffend_url(gem_name:, from:, to:)
18+
"https://my.diffend.io/gems/#{gem_name}/#{from}/#{to}"
19+
end
20+
21+
def post_gh_comment(pr_number:, body:, repo:)
22+
command = %W[
23+
gh
24+
pr
25+
comment
26+
#{pr_number}
27+
--body-file
28+
#{to_file(body)}
29+
--repo
30+
#{repo}
31+
]
32+
33+
run_with_retries(command:)
34+
end
35+
36+
def run_with_retries(command: [])
37+
command_args = command.dup.prepend("./with_retries")
38+
39+
if ENV.key?("DEBUG")
40+
puts "+ #{command_args.join(" ")}"
41+
return
42+
end
43+
44+
system(*command_args, exception: true)
45+
end
46+
47+
def output_from_with_retries(command: [])
48+
command_args = command.dup.prepend("./with_retries")
49+
50+
if ENV.key?("DEBUG")
51+
puts "+ #{command_args.join(" ")}"
52+
return "dummy output"
53+
end
54+
55+
stdout_str, stderr_str, status = Open3.capture3(*command_args)
56+
57+
if status.success?
58+
stdout_str
59+
else
60+
raise "Command '#{command_args.join(' ')}' failed: #{stderr_str}"
61+
end
62+
end
63+
64+
def gem_compare_command(gem_name:, from:, to:, diff: false)
65+
diff_flag = diff ? "--diff" : ""
66+
67+
[
68+
"gem",
69+
"compare",
70+
diff ? "--diff" : nil,
71+
gem_name,
72+
from,
73+
to,
74+
].compact
75+
end
76+
77+
def gem_compare_comment(command:, output:)
78+
<<~COMMENT
79+
<Details><Summary><h2><code>#{command.join(" ")}</code></h2></Summary>
80+
81+
````ruby
82+
#{output}
83+
````
84+
</Details>
85+
86+
COMMENT
87+
end
88+
89+
#
90+
# main
91+
#
92+
93+
repo = ENV.fetch("GITHUB_REPOSITORY", "dentarg/gem-compare")
94+
pr_number = ENV.fetch("PR_NUMBER", 2)
95+
diff_input = ENV["PR_DIFF"] || STDIN.readlines.join
96+
versions = ParseDiff.parse(diff_input)
97+
diffend_comment = StringIO.new
98+
diffend_comment.puts "## Diff URLs"
99+
100+
puts "Changes detected:"
101+
versions.each do |gem_name, (from, to)|
102+
puts "* #{gem_name} #{from}#{to}"
103+
104+
diffend_comment.puts "* #{diffend_url(gem_name:, from:, to:)}"
105+
end
106+
107+
puts "Posting comment with Diffend URLs"
108+
post_gh_comment(pr_number:, body: diffend_comment.string, repo:)
109+
110+
versions.each do |gem_name, (from, to)|
111+
puts "Comparing: #{gem_name} #{from}#{to}"
112+
113+
command = gem_compare_command(gem_name:, from:, to:)
114+
output = output_from_with_retries(command:)
115+
body = gem_compare_comment(command:, output:)
116+
117+
puts "Posting comment with 'gem compare' output"
118+
post_gh_comment(pr_number:, body:, repo:)
119+
120+
command = gem_compare_command(gem_name:, from:, to:, diff: true)
121+
output = output_from_with_retries(command:)
122+
body = gem_compare_comment(command:, output:)
123+
124+
puts "Posting comment with 'gem compare --diff' output"
125+
post_gh_comment(pr_number:, body:, repo:)
126+
end

lib/parse_diff.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
class ParseDiff
4+
def self.parse(input)
5+
versions = Hash.new { |hash, key| hash[key] = [] }
6+
versions_re = %r{\A(-|\+) (?<gem>((\w|-))+) \((?<version>.+)\)\z}
7+
8+
input.each_line do |line|
9+
$stderr.puts "line=#{line.inspect}" if ENV.key?("DEBUG")
10+
11+
if m = line.chomp.match(versions_re)
12+
gem_name = m.match(:gem)
13+
version = m.match(:version)
14+
15+
versions[gem_name] << version
16+
end
17+
end
18+
19+
versions
20+
end
21+
end

parse_title

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/fixtures/sentry.diff

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
diff --git a/Gemfile.lock b/Gemfile.lock
2+
index b7bae4a..9079ca6 100644
3+
--- a/Gemfile.lock
4+
+++ b/Gemfile.lock
5+
@@ -82,7 +82,7 @@ GEM
6+
rdiscount (2.2.7.1)
7+
redis (5.0.7)
8+
redis-client (>= 0.9.0)
9+
- redis-client (0.16.0)
10+
+ redis-client (0.17.0)
11+
connection_pool
12+
redis-namespace (1.11.0)
13+
redis (>= 4)
14+
@@ -117,10 +117,10 @@ GEM
15+
nokogiri (>= 1.10.8)
16+
rubyzip (>= 1.3.0)
17+
rubyzip (2.3.2)
18+
- sentry-resque (5.10.0)
19+
+ sentry-resque (5.11.0)
20+
resque (>= 1.24)
21+
- sentry-ruby (~> 5.10.0)
22+
- sentry-ruby (5.10.0)
23+
+ sentry-ruby (~> 5.11.0)
24+
+ sentry-ruby (5.11.0)
25+
concurrent-ruby (~> 1.0, >= 1.0.2)
26+
sequel (5.72.0)
27+
bigdecimal

test/parse_diff_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
require "test/unit"
4+
5+
require_relative "../lib/parse_diff"
6+
7+
class ParseDiffTest < Test::Unit::TestCase
8+
def test_parse_diff
9+
fixture = File.join(__dir__, "fixtures", "sentry.diff")
10+
input = File.read(fixture)
11+
expected = {
12+
"redis-client" => %w[0.16.0 0.17.0],
13+
"sentry-resque" => %w[5.10.0 5.11.0],
14+
"sentry-ruby" => %w[5.10.0 5.11.0],
15+
}
16+
17+
assert_equal expected, ParseDiff.parse(input)
18+
end
19+
end

0 commit comments

Comments
 (0)