lock_diff 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c3e80c316563aa4edf24bcff9f381bf872dc4515
4
+ data.tar.gz: d1f8fc2975b7a84f052bea293eb4c50884b0ba92
5
+ SHA512:
6
+ metadata.gz: c860e0271d1ca5fe86e302a322bc01d6a58a9bcfcef04b76dffbfa1ec3d137692777c120f4bd8074d2893227fc2a49196101e6368a2d6ebd5eeb02d321cc01e8
7
+ data.tar.gz: 376ff2488e661cc13e051c8c486933a9779031744a7436fceffb7a0d75767dc15fcdaaa99e77be8fc0849f1ca27c8a1f732eb95407cd52a55ea9b9d762550bf3
@@ -0,0 +1,34 @@
1
+ version: 2
2
+ jobs:
3
+ build:
4
+ working_directory: ~/lock_diff
5
+ docker:
6
+ - image: circleci/ruby:2.4.1-node
7
+ steps:
8
+ - checkout
9
+
10
+ # Restore bundle cache
11
+ - type: cache-restore
12
+ key: bundle-cache-{{ checksum "Gemfile.lock" }}
13
+
14
+ # Bundle install dependencies
15
+ - run: bundle install --path vendor/bundle
16
+
17
+ # Store bundle cache
18
+ - type: cache-save
19
+ key: bundle-cache-{{ checksum "Gemfile.lock" }}
20
+ paths:
21
+ - vendor/bundle
22
+
23
+ # Run rspec in parallel
24
+ - type: shell
25
+ command: |
26
+ bundle exec rspec --profile 10 \
27
+ --format RspecJunitFormatter \
28
+ --out /tmp/test-results/rspec.xml \
29
+ --format progress \
30
+ $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)
31
+
32
+ # Save artifacts
33
+ - type: store_test_results
34
+ path: /tmp/test-results
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .pryrc
11
+ .envrc
12
+
13
+ # rspec failure tracking
14
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at tkoyama@aiming-inc.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in lock_diff.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 vividmuimui
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # LockDiff
2
+
3
+ [![CircleCI](https://circleci.com/gh/vividmuimui/lock_diff.svg?style=svg)](https://circleci.com/gh/vividmuimui/lock_diff)
4
+
5
+ This Gem is generate `changelog url`, `github compare link` by lock file of Github pull request. And commant to that pull request.
6
+ Like as this.
7
+
8
+ > https://github.com/vividmuimui/rails_tutorial/pull/26#issuecomment-312491272
9
+ > ![image](https://user-images.githubusercontent.com/1803598/27770131-21db6112-5f74-11e7-80ed-28e5793beffc.png)
10
+
11
+ ## Strategies
12
+
13
+ - Gemfile.lock(Ruby/Bundler)
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'lock_diff'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install lock_diff
30
+
31
+ ## Usage
32
+
33
+ Require `GITHUB_ACCESS_TOKEN` environment.
34
+
35
+ ### Command line
36
+
37
+ ```sh
38
+ $ lock_diff
39
+ Usage: lock_diff [options]
40
+ -r, --repository=REPOSITORY required. Like as rails/rails
41
+ -n, --number=PULL_REQUEST_NUMBER required
42
+ --post-comment=true or false dafault=false
43
+ ```
44
+
45
+ ```sh
46
+ $ lock_diff -r "vividmuimui/rails_tutorial" -n 26 --post-comment=false
47
+ ```
48
+
49
+ ### Ruby
50
+
51
+ ```ruby
52
+ require 'lock_diff'
53
+ LockDiff.run(repository: "vividmuimui/rails_tutorial", number: 26, post_comment: false)
54
+ ```
55
+
56
+ ## Development
57
+
58
+ TODO:
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lock_diff. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
67
+
68
+ ## Code of Conduct
69
+
70
+ Everyone interacting in the LockDiff project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/lock_diff/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lock_diff"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/lock_diff ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "lock_diff"
4
+ require "optparse"
5
+
6
+ options = {
7
+ post_comment: false
8
+ }
9
+ opt = OptionParser.new
10
+
11
+ opt.on('-r', '--repository=REPOSITORY', 'required. Like as rails/rails') { |v| options[:repository] = v }
12
+ opt.on('-n', '--number=PULL_REQUEST_NUMBER', 'required') { |v| options[:number] = v }
13
+ opt.on('--post-comment=true or false', 'dafault=false') { |v| options[:post_comment] = v }
14
+ opt.parse!
15
+
16
+ unless options[:repository] && options[:number]
17
+ $stdout.puts opt.help
18
+ else
19
+ LockDiff.run(options)
20
+ end
@@ -0,0 +1,58 @@
1
+ module LockDiff
2
+ module CorExt
3
+ module Memoize
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ def has_memoized?(key)
9
+ memoized_table.key?(key)
10
+ end
11
+
12
+ def memoize(key, value)
13
+ memoized_table[key] = value
14
+ end
15
+
16
+ def memoized(key)
17
+ memoized_table[key]
18
+ end
19
+
20
+ def memoized_table
21
+ @memoized_table ||= {}
22
+ end
23
+
24
+ module ClassMethods
25
+ def memoize(method_name)
26
+ original_visibility =
27
+ case
28
+ when protected_instance_methods.include?(method_name)
29
+ :protected
30
+ when private_instance_methods.include?(method_name)
31
+ :private
32
+ else
33
+ :public
34
+ end
35
+
36
+ define_method("#{method_name}_with_memoize") do |*args, &block|
37
+ if LockDiff.memoize_response
38
+ if has_memoized?(method_name)
39
+ memoized(method_name)
40
+ else
41
+ memoize(method_name, send("#{method_name}_without_memoize", *args, &block))
42
+ end
43
+ else
44
+ send("#{method_name}_without_memoize", *args, &block)
45
+ end
46
+ end
47
+ send(original_visibility, "#{method_name}_with_memoize")
48
+
49
+ alias_method "#{method_name}_without_memoize", method_name
50
+ alias_method method_name, "#{method_name}_with_memoize"
51
+ end
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+
58
+ Object.send(:include, LockDiff::CorExt::Memoize)
@@ -0,0 +1,71 @@
1
+ module LockDiff
2
+ module Formatter
3
+ class GithubMarkdown
4
+ def self.format(diff_infos)
5
+ new(diff_infos).call
6
+ end
7
+
8
+ def initialize(diff_infos)
9
+ @diff_infos = diff_infos
10
+ end
11
+
12
+
13
+ def call
14
+ (headers + body).join("\n")
15
+ end
16
+
17
+ private
18
+
19
+ def headers
20
+ [
21
+ "| gem_name | diff | change log |",
22
+ "|----------|------|------------|"
23
+ ]
24
+ end
25
+
26
+ def body
27
+ @diff_infos.map { |diff_info| DiffFormmater.new(diff_info).call }
28
+ end
29
+
30
+ class DiffFormmater
31
+ def initialize(diff_info)
32
+ LockDiff.logger.debug diff_info.name
33
+ @diff_info = diff_info
34
+ end
35
+
36
+ def call
37
+ text = []
38
+ text << name
39
+ text << diff_text
40
+ text << change_log
41
+ "| #{text.join(' | ')} |"
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :diff_info
47
+
48
+ def name
49
+ "[#{diff_info.name}](#{diff_info.url})"
50
+ end
51
+
52
+ def diff_text
53
+ if diff_info.diff_url
54
+ "[#{diff_info.old_version}...#{diff_info.new_version}](#{diff_info.diff_url})"
55
+ else
56
+ "#{diff_info.old_version}...#{diff_info.new_version}"
57
+ end
58
+ end
59
+
60
+ def change_log
61
+ if diff_info.change_log_url
62
+ "[change log](#{diff_info.change_log_url})"
63
+ else
64
+ ""
65
+ end
66
+ end
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,36 @@
1
+ module LockDiff
2
+ module Gem
3
+ class DiffInfo
4
+ extend Forwardable
5
+
6
+ attr_reader :old_version, :new_version
7
+ def_delegators :@gem, :name, :url, :change_log_url
8
+
9
+ def self.by(old_spec:, new_spec:)
10
+ gem = Gem.new(new_spec.name)
11
+ new(
12
+ gem: gem,
13
+ old_version: Version.new(gem: gem, spec: old_spec),
14
+ new_version: Version.new(gem: gem, spec: new_spec)
15
+ )
16
+ end
17
+
18
+ def initialize(gem:, old_version:, new_version:)
19
+ @gem = gem
20
+ @old_version = old_version
21
+ @new_version = new_version
22
+ end
23
+
24
+ def changed?
25
+ @old_version.revision != @new_version.revision ||
26
+ @old_version.version != @new_version.version
27
+ end
28
+
29
+ def diff_url
30
+ return unless @gem.github_url && @old_version.ref && @new_version.ref
31
+ "#{@gem.github_url}/compare/#{@old_version.ref}...#{@new_version.ref}"
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ module LockDiff
2
+ module Gem
3
+ class Gem
4
+ extend Forwardable
5
+
6
+ attr_reader :name
7
+ def_delegator :ruby_gem, :github_url
8
+
9
+ def initialize(name)
10
+ @name = name
11
+ end
12
+
13
+ def url
14
+ ruby_gem.github_url || ruby_gem.homepage_url
15
+ end
16
+
17
+ def change_log_url
18
+ Github::ChangeLogUrlFinder.new(repository: repository, github_url: ruby_gem.github_url).call
19
+ end
20
+ memoize :change_log_url
21
+
22
+ def repository
23
+ Github::RepositoryNameDetector.new(ruby_gem.github_url).call
24
+ end
25
+
26
+ def tag_names
27
+ LockDiff.client.tag_names(repository)
28
+ end
29
+ memoize :tag_names
30
+
31
+ private
32
+
33
+ def ruby_gem
34
+ RubyGem.new(name)
35
+ end
36
+ memoize :ruby_gem
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,27 @@
1
+ module LockDiff
2
+ module Gem
3
+ class LockfileComparator
4
+ def self.compare_by(pr_gemfile_lock)
5
+ new(
6
+ old_lockfile: pr_gemfile_lock.base_file,
7
+ new_lockfile: pr_gemfile_lock.head_file
8
+ ).call
9
+ end
10
+
11
+ def initialize(old_lockfile:, new_lockfile:)
12
+ @old_lockfile = old_lockfile
13
+ @new_lockfile = new_lockfile
14
+ end
15
+
16
+ def call
17
+ old_specs_by_name = Spec.specs_by(@old_lockfile).map { |spec| [spec.name, spec] }.to_h
18
+
19
+ Spec.specs_by(@new_lockfile).map do |new_spec|
20
+ old_spec = old_specs_by_name[new_spec.name]
21
+ next unless old_spec
22
+ DiffInfo.by(old_spec: old_spec, new_spec: new_spec)
23
+ end.compact.select(&:changed?)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ module LockDiff
2
+ module Gem
3
+ class PrLockfile
4
+ def initialize(pull_request)
5
+ @pr = pull_request
6
+ end
7
+
8
+ def changed?
9
+ !!path
10
+ end
11
+
12
+ def path
13
+ @pr.find_content_path("Gemfile.lock")
14
+ end
15
+ memoize :path
16
+
17
+ def base_file
18
+ LockDiff.client.file(@pr.repository, path: path, ref: @pr.base_sha)
19
+ end
20
+ memoize :base_file
21
+
22
+ def head_file
23
+ LockDiff.client.file(@pr.repository, path: path, ref: @pr.head_sha)
24
+ end
25
+ memoize :head_file
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ require "httpclient"
2
+ require 'ostruct'
3
+
4
+ module LockDiff
5
+ module Gem
6
+ # wrapper of RubyGem
7
+ class RubyGem
8
+ extend Forwardable
9
+
10
+ def_delegator :@ruby_gem, :homepage_uri, :homepage_url
11
+ def_delegator :@ruby_gem, :source_code_uri, :source_code_url
12
+
13
+ def initialize(name)
14
+ content = HTTPClient.get_content("https://rubygems.org/api/v1/gems/#{name}.json")
15
+ @ruby_gem = OpenStruct.new(JSON.parse(content))
16
+ rescue => e
17
+ LockDiff.logger.warn("Could not fetch gem info of #{@spec.full_name} because of #{e.inspect}")
18
+ end
19
+
20
+ def github_url
21
+ Github::GithubUrlDetector.new([source_code_url, homepage_url]).call
22
+ end
23
+ memoize :github_url
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ module LockDiff
2
+ module Gem
3
+ # wrapper of lazy_specification
4
+ class Spec
5
+ extend Forwardable
6
+
7
+ def_delegators :@spec, :name, :version
8
+
9
+ def self.specs_by(lockfile)
10
+ Bundler::LockfileParser.new(lockfile).specs.map do |lazy_specification|
11
+ new(lazy_specification)
12
+ end
13
+ end
14
+
15
+ def initialize(lazy_specification)
16
+ @spec = lazy_specification
17
+ end
18
+
19
+ def revision
20
+ @spec.git_version&.strip
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,34 @@
1
+ module LockDiff
2
+ module Gem
3
+ class Version
4
+ extend Forwardable
5
+
6
+ def_delegators :@spec, :revision, :version
7
+
8
+ def initialize(gem:, spec:)
9
+ @gem = gem
10
+ @spec = spec
11
+ end
12
+
13
+ def ref
14
+ revision || git_tag
15
+ end
16
+
17
+ def to_s
18
+ ref || version.to_s
19
+ end
20
+
21
+ private
22
+
23
+ def git_tag
24
+ version_str = version.to_s
25
+ @gem.tag_names.find do |tag_name|
26
+ tag_name == version_str ||
27
+ tag_name == "v#{version_str}" ||
28
+ tag_name == "#{@gem.name}-#{version_str}"
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,23 @@
1
+ require "bundler"
2
+ require_relative "gem/diff_info"
3
+ require_relative "gem/gem"
4
+ require_relative "gem/lockfile_comparator"
5
+ require_relative "gem/pr_lockfile"
6
+ require_relative "gem/ruby_gem"
7
+ require_relative "gem/spec"
8
+ require_relative "gem/version"
9
+
10
+ module LockDiff
11
+ module Gem
12
+ class << self
13
+ class NotChangedLockfile < StandardError; end
14
+
15
+ def lock_file_diffs(pull_request)
16
+ pr_lockfile = PrLockfile.new(pull_request)
17
+ raise NotChangedLockfile unless pr_lockfile.changed?
18
+ LockfileComparator.compare_by(pr_lockfile)
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ module LockDiff
2
+ module Github
3
+ class AccessToken < ::String
4
+ def initialize(token = nil)
5
+ super(token || ENV.fetch('GITHUB_ACCESS_TOKEN'))
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,42 @@
1
+ module LockDiff
2
+ module Github
3
+ class ChangeLogUrlFinder
4
+ CHANGE_LOG_CANDIDATES = %w(
5
+ changelog
6
+ changes
7
+ history
8
+ releases
9
+ releasenote
10
+ news
11
+ )
12
+
13
+ def initialize(repository:, github_url:)
14
+ @repository = repository
15
+ @github_url = github_url
16
+ end
17
+
18
+ def call
19
+ find_change_log_url || find_release_url
20
+ end
21
+
22
+ private
23
+
24
+ def find_change_log_url
25
+ Github.client.contents(@repository).
26
+ select(&:file?).
27
+ find { |content|
28
+ name = content.name.downcase.delete('_')
29
+ CHANGE_LOG_CANDIDATES.any? { |candidate| name.start_with? candidate }
30
+ }&.html_url
31
+ end
32
+
33
+ def find_release_url
34
+ return unless @github_url
35
+ unless Github.client.exist_releases?(@repository)
36
+ @github_url + "/releases"
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,57 @@
1
+ require 'octokit'
2
+
3
+ module LockDiff
4
+ module Github
5
+ # wrapper of Octokit::Client
6
+
7
+ class << self
8
+ def client
9
+ Github::Client.new(Github::AccessToken.new)
10
+ end
11
+ memoize :client
12
+ end
13
+
14
+ class Client
15
+ def initialize(access_token)
16
+ @client = Octokit::Client.new(access_token: access_token)
17
+ end
18
+
19
+ def file(repository, path:, ref:)
20
+ content = @client.contents(repository, path: path, ref: ref)
21
+ Base64.decode64(content.content)
22
+ end
23
+
24
+ def pull_request(repository, number)
25
+ Github::PullRequest.new(@client.pull_request(repository, number))
26
+ end
27
+
28
+ def pull_request_content_path(repository, number, file_name)
29
+ content = @client.pull_request_files(repository, number).
30
+ find { |file| file.filename.include?(file_name) }
31
+ content&.filename
32
+ end
33
+
34
+ def exist_releases?(repository)
35
+ return false unless repository
36
+ @client.releases(repository).empty?
37
+ end
38
+
39
+ def contents(repository)
40
+ return [] unless repository
41
+ @client.contents(repository).map do |content|
42
+ Content.new(content)
43
+ end
44
+ end
45
+
46
+ def tag_names(repository)
47
+ return [] unless repository
48
+ @client.tags(repository).map(&:name)
49
+ end
50
+
51
+ def add_comment(repository, number, comment)
52
+ @client.add_comment(repository, number, comment)
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,19 @@
1
+ module LockDiff
2
+ module Github
3
+ # wrapper of Github Content
4
+ class Content
5
+ extend Forwardable
6
+
7
+ def_delegators :@content, :name, :html_url
8
+
9
+ def initialize(content)
10
+ @content = content
11
+ end
12
+
13
+ def file?
14
+ @content.type == 'file'
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ require "httpclient"
2
+
3
+ module LockDiff
4
+ module Github
5
+ class GithubUrlDetector
6
+ REGEXP = %r!https?://([^/]+)\.github\.[^/]+/([^/]+)! # github.com, github.io
7
+
8
+ def initialize(urls)
9
+ @urls = Array(urls).compact
10
+ end
11
+
12
+ def call
13
+ url = @urls.find { |url| url.include?("github.com") }
14
+ return unless url
15
+
16
+ response = HTTPClient.get(url, follow_redirect: true)
17
+ url = response.header.request_uri.to_s
18
+
19
+ if url.match?(REGEXP)
20
+ _, owner, repo = url.match(REGEXP).to_a
21
+ url = "https://github.com/#{owner}/#{repo}"
22
+ HTTPClient.get(url).ok? ? url : nil
23
+ else
24
+ repository = RepositoryNameDetector.new(url).call
25
+ "https://github.com/#{repository}"
26
+ end
27
+ rescue => e
28
+ LockDiff.logger.warn("Could not detect github url by #{url} because of #{e.inspect}")
29
+ nil
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,18 @@
1
+ module LockDiff
2
+ module Github
3
+ # wrapper of github PullRequest
4
+ class PullRequest
5
+ def initialize(pull_request)
6
+ @pr = pull_request
7
+ end
8
+
9
+ def base_sha
10
+ @pr.base.sha
11
+ end
12
+
13
+ def head_sha
14
+ @pr.head.sha
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module LockDiff
2
+ module Github
3
+ class RepositoryNameDetector
4
+ REGEXP = %r!github\.com/([^/]+)/([^/]+)!
5
+
6
+ def initialize(url)
7
+ @url = url
8
+ end
9
+
10
+ def call
11
+ return unless @url
12
+ _, repo_owner, repo_name = @url.match(REGEXP).to_a
13
+ "#{repo_owner}/#{repo_name}"
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "github/access_token"
2
+ require_relative "github/change_log_url_finder"
3
+ require_relative "github/client"
4
+ require_relative "github/content"
5
+ require_relative "github/github_url_detector"
6
+ require_relative "github/pull_request"
7
+ require_relative "github/repository_name_detector"
8
+
9
+ module LockDiff
10
+ module Github
11
+ end
12
+ end
@@ -0,0 +1,30 @@
1
+ module LockDiff
2
+ class PullRequest
3
+ attr_reader :repository, :number
4
+
5
+ class NotFoundPullRequest < StandardError; end
6
+
7
+ def initialize(repository:, number:)
8
+ @repository = repository
9
+ @number = number
10
+ @pr = LockDiff.client.pull_request(repository, number)
11
+ rescue => e
12
+ message = "Not found pull request by (repository: #{repository}, number: #{number}, client: #{LockDiff.client.class}). Becase of #{e.inspect}"
13
+ LockDiff.logger.warn(message)
14
+ raise NotFoundPullRequest.new(message)
15
+ end
16
+
17
+ def base_sha
18
+ @pr.base_sha
19
+ end
20
+
21
+ def head_sha
22
+ @pr.head_sha
23
+ end
24
+
25
+ def find_content_path(file_name)
26
+ LockDiff.client.pull_request_content_path(@repository, @number, file_name)
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module LockDiff
2
+ VERSION = "0.1.1"
3
+ end
data/lib/lock_diff.rb ADDED
@@ -0,0 +1,37 @@
1
+ require "logger"
2
+ require "forwardable"
3
+
4
+ require "lock_diff/core_ext/memoize"
5
+ require "lock_diff/formatter/github_markdown"
6
+ require "lock_diff/gem"
7
+ require "lock_diff/github"
8
+ require "lock_diff/pull_request"
9
+ require "lock_diff/version"
10
+
11
+ module LockDiff
12
+ class << self
13
+ attr_accessor :client_class, :formatter, :strategy, :memoize_response, :logger
14
+
15
+ def client
16
+ client_class.client
17
+ end
18
+
19
+ def run(repository:, number:, post_comment: false)
20
+ pr = PullRequest.new(repository: repository, number: number)
21
+ lockfile_diff_infos = strategy.lock_file_diffs(pr)
22
+ result = formatter.format(lockfile_diff_infos)
23
+
24
+ if post_comment
25
+ client.add_comment(repository, number, result)
26
+ else
27
+ $stdout.puts result
28
+ end
29
+ end
30
+ end
31
+
32
+ self.client_class = Github
33
+ self.formatter = Formatter::GithubMarkdown
34
+ self.strategy = Gem
35
+ self.memoize_response = true
36
+ self.logger = Logger.new(nil)
37
+ end
data/lock_diff.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "lock_diff/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lock_diff"
8
+ spec.version = LockDiff::VERSION
9
+ spec.authors = ["vividmuimui"]
10
+ spec.email = ["vivid.muimui@gmail.com"]
11
+
12
+ spec.summary = %q{Generate links for Gemfile.lock's diff and post to PullRequest}
13
+ spec.description = %q{Generate links for Gemfile.lock's diff and post to PullRequest}
14
+ # spec.homepage = "https://github.com/vividmuimui/lock_diff"
15
+ spec.homepage = ""
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+ spec.required_ruby_version = ">= 2.4.0"
25
+
26
+ spec.add_dependency "octokit", "~> 4.0"
27
+ spec.add_dependency "httpclient"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.15"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency "pry"
33
+ spec.add_development_dependency "pry-byebug"
34
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lock_diff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - vividmuimui
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: octokit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httpclient
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.15'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Generate links for Gemfile.lock's diff and post to PullRequest
112
+ email:
113
+ - vivid.muimui@gmail.com
114
+ executables:
115
+ - lock_diff
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".circleci/config.yml"
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - exe/lock_diff
130
+ - lib/lock_diff.rb
131
+ - lib/lock_diff/core_ext/memoize.rb
132
+ - lib/lock_diff/formatter/github_markdown.rb
133
+ - lib/lock_diff/gem.rb
134
+ - lib/lock_diff/gem/diff_info.rb
135
+ - lib/lock_diff/gem/gem.rb
136
+ - lib/lock_diff/gem/lockfile_comparator.rb
137
+ - lib/lock_diff/gem/pr_lockfile.rb
138
+ - lib/lock_diff/gem/ruby_gem.rb
139
+ - lib/lock_diff/gem/spec.rb
140
+ - lib/lock_diff/gem/version.rb
141
+ - lib/lock_diff/github.rb
142
+ - lib/lock_diff/github/access_token.rb
143
+ - lib/lock_diff/github/change_log_url_finder.rb
144
+ - lib/lock_diff/github/client.rb
145
+ - lib/lock_diff/github/content.rb
146
+ - lib/lock_diff/github/github_url_detector.rb
147
+ - lib/lock_diff/github/pull_request.rb
148
+ - lib/lock_diff/github/repository_name_detector.rb
149
+ - lib/lock_diff/pull_request.rb
150
+ - lib/lock_diff/version.rb
151
+ - lock_diff.gemspec
152
+ homepage: ''
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: 2.4.0
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.6.11
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: Generate links for Gemfile.lock's diff and post to PullRequest
176
+ test_files: []