Sha256: a54a5563df15df858bffd6bc7e5036470e3406864e45dc30fda8e6ad501640e1

Contents?: true

Size: 1.31 KB

Versions: 10

Compression:

Stored size: 1.31 KB

Contents

require 'stash/pull_request/diff'

module Stash
  class PullRequest
    attr_accessor :repository, :id

    def initialize(repository, id)
      self.repository = repository
      self.id = id
    end

    def add_comment(file, line, text)
      return unless diff.added_line?(file, line)
      return if already_commented?(file, line, text)

      post_comment(file, line, text)
    end

    private

    def already_commented?(file, line, text)
      file_comments(file).detect do |comment|
        comment['anchor']['line'] == line && comment['text'] == text
      end
    end

    def file_comments(file)
      @file_comments ||= {}
      @file_comments[file] ||= get("/comments?path=#{file}")['values']
    end

    def post_comment(file, line, text)
      data = {
        text: text,
        anchor: {
          path: file,
          line: line,
          lineType: 'ADDED'
        }
      }

      logger.info(%(Commenting #{file}, line #{line}: "#{text}"...))
      post('/comments', data)
    end

    def diff
      @diff ||= Diff.new(get('/diff?withComments=false'))
    end

    def get(path)
      repository.get(endpoint + path)
    end

    def post(path, data)
      repository.post(endpoint + path, data)
    end

    def endpoint
      "/pull-requests/#{id}"
    end

    def logger
      repository.logger
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
face_control-0.6.0 lib/stash/pull_request.rb
face_control-0.5.1 lib/stash/pull_request.rb
face_control-0.5.0 lib/stash/pull_request.rb
face_control-0.4.0 lib/stash/pull_request.rb
face_control-0.3.0 lib/stash/pull_request.rb
face_control-0.2.1 lib/stash/pull_request.rb
stash_pull_request_commenter-0.2.1 lib/stash/pull_request.rb
stash_pull_request_commenter-0.2.0 lib/stash/pull_request.rb
stash_pull_request_commenter-0.0.2 lib/stash/pull_request.rb
stash_pull_request_commenter-0.0.1 lib/stash/pull_request.rb