class SmartTodo::Events::IssueClose

An event that check if a GitHub Pull Request or Issue is closed. This event will make an API call to the GitHub API.

If the Pull Request or Issue is on a private repository, exporting a token with the `repos` scope in the SMART_TODO_GITHUB_TOKEN environment variable is required.

Constants

TOKEN_ENV

Public Class Methods

new(organization, repo, pr_number, type:) click to toggle source

@param organization [String] @param repo [String] @param pr_number [String, Integer]

# File lib/smart_todo/events/issue_close.rb, line 20
def initialize(organization, repo, pr_number, type:)
  @url = "/repos/#{organization}/#{repo}/#{type}/#{pr_number}"
  @organization = organization
  @repo = repo
  @pr_number = pr_number
end

Public Instance Methods

error_message() click to toggle source

Error message send to Slack in case the Pull Request or Issue couldn't be found.

@return [String]

# File lib/smart_todo/events/issue_close.rb, line 43
      def error_message
        <<~EOM
          I can't retrieve the information from the PR or Issue *#{@pr_number}* in the
          *#{@organization}/#{@repo}* repository.

          If the repository is a private one, make sure to export the `#{TOKEN_ENV}`
          environment variable with a correct GitHub token.
        EOM
      end
message() click to toggle source

@return [String]

# File lib/smart_todo/events/issue_close.rb, line 54
      def message
        <<~EOM
          The Pull Request or Issue https://github.com/#{@organization}/#{@repo}/pull/#{@pr_number}
          is now closed, your TODO is ready to be addressed.
        EOM
      end
met?() click to toggle source

@return [String, false]

# File lib/smart_todo/events/issue_close.rb, line 28
def met?
  response = client.get(@url, default_headers)

  if response.code_type < Net::HTTPClientError
    error_message
  elsif pull_request_closed?(response.body)
    message
  else
    false
  end
end

Private Instance Methods

client() click to toggle source

@return [Net::HTTP] an instance of Net::HTTP

# File lib/smart_todo/events/issue_close.rb, line 64
def client
  @client ||= Net::HTTP.new('api.github.com', Net::HTTP.https_default_port).tap do |client|
    client.use_ssl = true
  end
end
default_headers() click to toggle source

@return [Hash]

# File lib/smart_todo/events/issue_close.rb, line 79
def default_headers
  { 'Accept' => 'application/vnd.github.v3+json' }.tap do |headers|
    headers['Authorization'] = "token #{ENV[TOKEN_ENV]}" if ENV[TOKEN_ENV]
  end
end
pull_request_closed?(pull_request) click to toggle source

@param pull_request [String] the Pull Request or Issue

detail sent back from the GitHub API

@return [true, false]

# File lib/smart_todo/events/issue_close.rb, line 74
def pull_request_closed?(pull_request)
  JSON.parse(pull_request)['state'] == 'closed'
end