Sha256: 0ed96ee7a79e8924ba4093edf845d2aa855f301a54558abc87a9c35e42ae274d

Contents?: true

Size: 1.79 KB

Versions: 7

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

require "octokit"
require "jwt"

module CiToolkit
  # Utility class that provides an access token that can be used with the Github API
  class GithubBot
    # Provides a jwt token for authentication. Sores the private key and app id for the bot
    class Credentials
      attr_reader :app_id

      def initialize(app_id = ENV.fetch("CRVSH_BOT_GITHUB_APP_ID", nil),
                     private_key = ENV.fetch("CRVSH_BOT_GITHUB_APP_PRIVATE_KEY", nil))
        @app_id = app_id.to_i
        @private_key = private_key
      end

      def jwt_token
        return if @private_key.nil?

        JWT.encode(
          {
            iat: Time.now.to_i,
            exp: Time.now.to_i + (9 * 60),
            iss: @app_id
          },
          OpenSSL::PKey::RSA.new(@private_key),
          "RS256"
        )
      end
    end
    # stack = Faraday::RackBuilder.new do |builder|
    #   builder.response :logger
    #   builder.use Octokit::Response::RaiseError
    #   builder.adapter Faraday.default_adapter
    # end
    # Octokit.middleware = stack

    def initialize(
      credentials = CiToolkit::GithubBot::Credentials.new,
      client = Octokit::Client.new(bearer_token: credentials.jwt_token, auto_paginate: true)
    )
      @app_id = credentials.app_id
      @client = client
    end

    def create_token
      return unless (installation_id = find_app_installation)

      @client.create_app_installation_access_token(
        installation_id,
        { accept: Octokit::Preview::PREVIEW_TYPES[:integrations] }
      )[:token]
    end

    private

    def find_app_installation
      @client.find_app_installations(
        { accept: Octokit::Preview::PREVIEW_TYPES[:integrations] }
      ).select { |installation| @app_id.equal?(installation[:app_id]) }.first[:id]
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ci_toolkit-1.6.4 lib/ci_toolkit/github_bot.rb
ci_toolkit-1.6.3 lib/ci_toolkit/github_bot.rb
ci_toolkit-1.6.2 lib/ci_toolkit/github_bot.rb
ci_toolkit-1.6.1 lib/ci_toolkit/github_bot.rb
ci_toolkit-1.6.0 lib/ci_toolkit/github_bot.rb
ci_toolkit-1.5.24 lib/ci_toolkit/github_bot.rb
ci_toolkit-1.5.23 lib/ci_toolkit/github_bot.rb