class Cyclid::API::Plugins::GithubComment

Post a comment to a Github plugin (including Pull Requests)

Public Class Methods

new(args = {}) click to toggle source
# File lib/cyclid/plugins/action/github_comment.rb, line 29
def initialize(args = {})
  args.symbolize_keys!

  # There must be a repository name.
  raise 'a github comment action requires a repository' unless args.include? :repo

  # There must be an issue number.
  raise 'a github comment action requires an issue number' unless args.include? :number

  @repo = args[:repo]
  @number = args[:number].to_s

  @comment = args[:comment] if args.include? :comment
  @path = args[:path] if args.include? :path

  # There must be either a comment or a file to read as a comment
  raise 'a github comment action requires a comment or file' if @comment.nil? && @path.nil?
end

Public Instance Methods

perform(log) click to toggle source
# File lib/cyclid/plugins/action/github_comment.rb, line 48
def perform(log)
  # If a path was given, read the file and use it as the comment
  if @path
    content = StringIO.new
    @transport.download(content, @path**@ctx)
    @comment = content.string
  end

  # Insert context
  repo = @repo**@ctx
  number = @number**@ctx
  comment = @comment**@ctx

  # Append the comment
  client = Octokit::Client.new(access_token: oauth_token)
  client.add_comment(repo, number, comment)

  [true, 0]
rescue StandardError => ex
  log.write "Failed to add Github comment to #{repo} ##{number}: #{ex}"
  [false, 0]
end

Private Instance Methods

config() click to toggle source
# File lib/cyclid/plugins/action/github_comment.rb, line 76
def config
  # Retrieve the configuration
  plugin_config = Cyclid::API::Plugins::Github.get_config(@ctx[:organization])
  plugin_config['config']
end
oauth_token() click to toggle source
# File lib/cyclid/plugins/action/github_comment.rb, line 82
def oauth_token
  @token ||= config['oauth_token']
end