diff-suggester 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 463fb1bfc4d2cde9c7992ba9538cddb1f1eff02a8661a08f206a9509d83bccd0
4
+ data.tar.gz: 02ea50699bfd32ab6293839177da4ecd8ac1a6616ccd30e02710acef5dc09dd0
5
+ SHA512:
6
+ metadata.gz: 3c3de0203fd9584175ea6c1e7607c8295e1124f43e54aece7613a0d940aa8579acbaccb584614dd11b9f6c6e572b30aaf9bdeee135b7b23c628af22e6298196b
7
+ data.tar.gz: 44c603ee33206b096a71972116610bfaa460de16a21e2356b90561387e53ed5673673c6e66abc0f027d463a63986bc6506db062176dae5e8b6566f31bd880ac0
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in diff-suggester.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 13.0"
7
+
8
+ gem "rspec", "~> 3.0"
9
+
10
+ gem "rubocop", "~> 1.21"
11
+
12
+ gem "danger"
13
+ gem "git_diff"
14
+ gem "octokit"
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ addressable (2.8.4)
5
+ public_suffix (>= 2.0.2, < 6.0)
6
+ claide (1.1.0)
7
+ claide-plugins (0.9.2)
8
+ cork
9
+ nap
10
+ open4 (~> 1.3)
11
+ colored2 (3.1.2)
12
+ cork (0.3.0)
13
+ colored2 (~> 3.1)
14
+ danger (9.2.0)
15
+ claide (~> 1.0)
16
+ claide-plugins (>= 0.9.2)
17
+ colored2 (~> 3.1)
18
+ cork (~> 0.1)
19
+ faraday (>= 0.9.0, < 3.0)
20
+ faraday-http-cache (~> 2.0)
21
+ git (~> 1.7)
22
+ kramdown (~> 2.3)
23
+ kramdown-parser-gfm (~> 1.0)
24
+ no_proxy_fix
25
+ octokit (~> 5.0)
26
+ terminal-table (>= 1, < 4)
27
+ faraday (2.7.4)
28
+ faraday-net_http (>= 2.0, < 3.1)
29
+ ruby2_keywords (>= 0.0.4)
30
+ faraday-http-cache (2.4.1)
31
+ faraday (>= 0.8)
32
+ faraday-net_http (3.0.2)
33
+ git (1.18.0)
34
+ addressable (~> 2.8)
35
+ rchardet (~> 1.8)
36
+ git_diff (0.4.3)
37
+ kramdown (2.4.0)
38
+ rexml
39
+ kramdown-parser-gfm (1.1.0)
40
+ kramdown (~> 2.0)
41
+ nap (1.1.0)
42
+ no_proxy_fix (0.1.2)
43
+ octokit (5.6.1)
44
+ faraday (>= 1, < 3)
45
+ sawyer (~> 0.9)
46
+ open4 (1.3.4)
47
+ public_suffix (5.0.1)
48
+ rchardet (1.8.0)
49
+ rexml (3.2.5)
50
+ ruby2_keywords (0.0.5)
51
+ sawyer (0.9.2)
52
+ addressable (>= 2.3.5)
53
+ faraday (>= 0.17.3, < 3)
54
+ terminal-table (3.0.2)
55
+ unicode-display_width (>= 1.1.1, < 3)
56
+ unicode-display_width (2.4.2)
57
+
58
+ PLATFORMS
59
+ arm64-darwin-21
60
+
61
+ DEPENDENCIES
62
+ danger
63
+ git_diff
64
+ octokit
65
+
66
+ BUNDLED WITH
67
+ 2.3.17
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # diff-suggester
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,36 @@
1
+ require 'net/https'
2
+ require 'json'
3
+ require 'octokit'
4
+
5
+ module GitHubRequester
6
+ class PullRequest
7
+
8
+ def initialize(repo:, pr_number:, access_token:)
9
+ octokit_client = Octokit::Client.new(access_token: access_token)
10
+ @pr = octokit_client.pull_request(repo, pr_number)
11
+
12
+ @uri = URI.parse("https://api.github.com/repos/#{repo}/pulls/#{@pr.number}/comments")
13
+ @request = Net::HTTP::Post.new(@uri)
14
+ @request['Accept'] = 'application/vnd.github+json'
15
+ @request['Authorization'] = "Bearer #{access_token}"
16
+ @request['X-GitHub-Api-Version'] = '2022-11-28'
17
+ end
18
+
19
+ def create_comment(path:, body:, line:, start_line:, side:, start_side:)
20
+ @request.body = {
21
+ 'commit_id': @pr.head.sha,
22
+ 'path': path,
23
+ 'body': body,
24
+ 'line': line,
25
+ 'start_line': start_line,
26
+ 'side': side,
27
+ 'start_side': start_side
28
+ }.to_json
29
+
30
+ response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: @uri.scheme == 'https') do |http|
31
+ http.request(@request)
32
+ end
33
+ return response
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ module GitHubDiff
2
+ class Diff
3
+
4
+ def initialize(file_path:, hunks:)
5
+ @file_path = file_path
6
+ @hunks = hunks
7
+ end
8
+
9
+ def file_path
10
+ return @file_path
11
+ end
12
+
13
+ def hunks
14
+ return @hunks
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ require 'git_diff'
2
+ require_relative 'hunk'
3
+ require_relative 'diff'
4
+
5
+ module GitHubDiff
6
+
7
+ def parse_from_string
8
+ diffs = get_diff_string.files.map { |file|
9
+ hunks = file.hunks.map { |hunk| hunk(hunk) }
10
+ Diff.new(file_path: file.b_path, hunks: hunks)
11
+ }
12
+ return diffs
13
+ end
14
+
15
+ def get_diff_string
16
+ return ::GitDiff.from_string(`git diff --unified=0 HEAD`)
17
+ end
18
+
19
+ def hunk(hunk)
20
+ addition_lines = hunk.lines.select{|l| l.content.start_with?('+')}.map do |line|
21
+ line.content.scan(/^\+([^+].*)/)
22
+ end
23
+ target_range = hunk.range_info.original_range
24
+ return Hunk.new(body: addition_lines.join("\n"), start_line: target_range.start, end_line: target_range.start + target_range.number_of_lines)
25
+ end
26
+
27
+ module_function :parse_from_string
28
+ module_function :get_diff_string
29
+ module_function :hunk
30
+ end
@@ -0,0 +1,22 @@
1
+ module GitHubDiff
2
+ class Hunk
3
+
4
+ def initialize(body:, start_line:, end_line:)
5
+ @body = code
6
+ @start_line = start_line
7
+ @end_line = end_line
8
+ end
9
+
10
+ def body
11
+ return @body
12
+ end
13
+
14
+ def start_line
15
+ return @start_line
16
+ end
17
+
18
+ def end_line
19
+ return @end_line
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'diff/diff_parser'
2
+ require_relative 'client/requester'
3
+
4
+ module DiffSuggester
5
+ class Suggester
6
+
7
+ def initialize(repo:, pr_number:, access_token:)
8
+ @pull_request = GitHubRequester::PullRequest.new(repo: repo, pr_number: pr_number, access_token: access_token)
9
+ end
10
+
11
+ def suggest
12
+ diffs = GitHubDiff.parse_from_string
13
+ diffs.each do |diff|
14
+ diff.hunks.each do |hunk|
15
+ @pull_request.create_comment(
16
+ path: diff.file_path,
17
+ body: suggestion(hunk.body),
18
+ line: hunk.end_line,
19
+ start_line: hunk.start_line,
20
+ side: 'RIGHT',
21
+ start_side: 'RIGHT'
22
+ )
23
+ end
24
+ end
25
+ end
26
+
27
+ def suggestion(body)
28
+ return "```suggestion\n#{body}\n```"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module DiffSuggester
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'diff-suggester/version'
2
+ require 'diff-suggester/suggester'
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: diff-suggester
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tick-taku
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - tick.taku.77@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - README.md
25
+ - Rakefile
26
+ - lib/diff-suggester.rb
27
+ - lib/diff-suggester/client/requester.rb
28
+ - lib/diff-suggester/diff/diff.rb
29
+ - lib/diff-suggester/diff/diff_parser.rb
30
+ - lib/diff-suggester/diff/hunk.rb
31
+ - lib/diff-suggester/suggester.rb
32
+ - lib/diff-suggester/version.rb
33
+ homepage: https://rubygems.org/gems/diff-suggester
34
+ licenses: []
35
+ metadata:
36
+ homepage_uri: https://rubygems.org/gems/diff-suggester
37
+ source_code_uri: https://github.com/tick-taku/diff-suggester
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.6.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.3.7
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Provide formatted diffs as suggestions in GitHub Pull Requests.
57
+ test_files: []