Sha256: 5bb3c45433566edb96d95cd74250d54474030705d72162d3e681f07c340c5944

Contents?: true

Size: 1.95 KB

Versions: 7

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

require 'octokit'

module Unwrappr
  module GitHub
    # GitHub Interactions
    module Client
      class << self
        def reset_client
          @git_client = nil
          @github_token = nil
        end

        def make_pull_request!
          create_and_annotate_pull_request
        rescue Octokit::ClientError => e
          raise "Failed to create and annotate pull request: #{e}"
        end

        private

        def repo_name_and_org
          repo_url = Unwrappr::GitCommandRunner.remote.gsub(/\.git$/, '')
          pattern = %r{github.com[/:](?<org>.*)/(?<repo>.*)}
          m = pattern.match(repo_url)
          [m[:org], m[:repo]].join('/')
        end

        def create_and_annotate_pull_request
          pr = git_client.create_pull_request(
            repo_name_and_org,
            'master',
            Unwrappr::GitCommandRunner.current_branch_name,
            'Automated Bundle Update',
            pull_request_body
          )
          annotate_pull_request(pr.number)
        end

        def pull_request_body
          <<~BODY
            Gems brought up-to-date with :heart: by [Unwrappr](https://github.com/envato/unwrappr).
             See individual annotations below for details.
          BODY
        end

        def annotate_pull_request(pr_number)
          LockFileAnnotator.annotate_github_pull_request(
            repo: repo_name_and_org,
            pr_number: pr_number,
            client: git_client
          )
        end

        def git_client
          @git_client ||= Octokit::Client.new(access_token: github_token)
        end

        def github_token
          @github_token ||= ENV.fetch('GITHUB_TOKEN') do
            raise %(
Missing environment variable GITHUB_TOKEN.
See https://github.com/settings/tokens to set up personal access tokens.
Add to the environment:

    export GITHUB_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

)
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
unwrappr-0.4.0 lib/unwrappr/github/client.rb
unwrappr-0.3.5 lib/unwrappr/github/client.rb
unwrappr-0.3.4 lib/unwrappr/github/client.rb
unwrappr-0.3.3 lib/unwrappr/github/client.rb
unwrappr-0.3.2 lib/unwrappr/github/client.rb
unwrappr-0.3.1 lib/unwrappr/github/client.rb
unwrappr-0.3.0 lib/unwrappr/github/client.rb