diff options
| author | mo khan <mo@mokhan.ca> | 2021-12-22 16:03:04 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2021-12-22 16:03:04 -0700 |
| commit | c5cf142e2a93e4e1dfa1538c3aaf14cf04799ae2 (patch) | |
| tree | f59c094cefd406273f13f457036f91102d50aa01 | |
| parent | fa78d39ecb2697d564cd3520a27a4726c478140b (diff) | |
refactor: delegate to GitHub to create pull request
| -rw-r--r-- | lib/dependabot.rb | 11 | ||||
| -rw-r--r-- | lib/dependabot/callback.rb | 13 | ||||
| -rw-r--r-- | lib/dependabot/publish.rb | 30 | ||||
| -rw-r--r-- | lib/github.rb | 10 |
4 files changed, 52 insertions, 12 deletions
diff --git a/lib/dependabot.rb b/lib/dependabot.rb index 2aa13bc..899b80a 100644 --- a/lib/dependabot.rb +++ b/lib/dependabot.rb @@ -9,6 +9,7 @@ require "spandx" require "straw" require_relative "dependabot/bundler/update" +require_relative "dependabot/callback" require_relative "dependabot/git" require_relative "dependabot/publish" require_relative "dependabot/version" @@ -34,3 +35,13 @@ module Dependabot @github ||= GitHub.new end end + +module Spandx + module Core + class LicensePlugin + def enhance(dependency) + dependency + end + end + end +end diff --git a/lib/dependabot/callback.rb b/lib/dependabot/callback.rb new file mode 100644 index 0000000..b19141e --- /dev/null +++ b/lib/dependabot/callback.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Dependabot + class Callback + def initialize(&block) + @block = block + end + + def call + @block.call + end + end +end diff --git a/lib/dependabot/publish.rb b/lib/dependabot/publish.rb index 11fda8a..77e2564 100644 --- a/lib/dependabot/publish.rb +++ b/lib/dependabot/publish.rb @@ -14,28 +14,34 @@ module Dependabot end def update!(push: false) + transaction(push: push) do |after_commit| + ::Spandx::Core::Plugin.enhance(dependency) + after_commit.new do + Dependabot.logger.debug(git.patch) + Dependabot.github.create_pull_request_from(git.repo, base, head, title, description) + end + end + end + + private + + def transaction(push:) git.checkout(branch: head) - ::Spandx::Core::Plugin.enhance(dependency) + callback = yield Callback return if git.patch.empty? || !push - Dependabot.logger.debug(git.patch) git.commit(all: true, message: commit_message) git.push(remote: "origin", branch: head) - - Dependabot.octokit.create_pull_request( - GitHub.name_with_owner_from(git.repo.remotes["origin"].url), - base, - head, - title, - description - ) + callback.call ensure + reset + end + + def reset git.repo.checkout_head(strategy: :force) git.repo.checkout(base) end - private - def title memoize(:title) do "chore(deps): bump #{dependency.name} from #{dependency.version}" diff --git a/lib/github.rb b/lib/github.rb index c55daa1..002b95c 100644 --- a/lib/github.rb +++ b/lib/github.rb @@ -18,6 +18,16 @@ class GitHub @workspace = workspace end + def create_pull_request_from(repo, base, head, title, description) + Dependabot.octokit.create_pull_request( + GitHub.name_with_owner_from(repo.remotes["origin"].url), + base, + head, + title, + description + ) + end + class << self def name_with_owner_from(url) regex = %r{(?<x>(?<scheme>https|ssh)://)?(?<username>git@)?github.com[:|/](?<nwo>\w+/\w+)(?<extension>\.git)?} |
