Sha256: 028f2a09d10df750e2b1162faff9f75b9f098197533dbaa91277304689d04635

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

module Pronto
  class Gitlab
    def initialize(repo)
      @repo = repo
      @config = Config.new
      @comment_cache = {}
    end

    def commit_comments(sha)
      @comment_cache[sha.to_s] ||= begin
        client.commit_comments(slug, sha, per_page: 500).map do |comment|
          Comment.new(sha, comment.note, comment.path, comment.line)
        end
      end
    end

    def create_commit_comment(comment)
      @config.logger.log("Creating commit comment on #{comment.sha}")
      client.create_commit_comment(slug, comment.sha, comment.body,
                                   path: comment.path, line: comment.position,
                                   line_type: 'new')
    end

    private

    def slug
      return @config.gitlab_slug if @config.gitlab_slug
      @slug ||= begin
        slug = @repo.remote_urls.map do |url|
          match = slug_regex(url).match(url)
          match[:slug] if match
        end.compact.first
        URI.escape(slug, '/') if slug
      end
    end

    def slug_regex(url)
      if url =~ %r{^ssh:\/\/}
        %r{.*#{host}(:[0-9]+)?(:|\/)(?<slug>.*).git}
      else
        %r{.*#{host}(:|\/)(?<slug>.*).git}
      end
    end

    def host
      @host ||= URI.split(gitlab_api_endpoint)[2, 2].compact.join(':')
    end

    def client
      @client ||= ::Gitlab.client(endpoint: gitlab_api_endpoint,
                                  private_token: gitlab_api_private_token)
    end

    def gitlab_api_private_token
      @config.gitlab_api_private_token
    end

    def gitlab_api_endpoint
      @config.gitlab_api_endpoint
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pronto-0.8.2 lib/pronto/gitlab.rb
pronto-0.8.1 lib/pronto/gitlab.rb
pronto-0.8.0 lib/pronto/gitlab.rb
pronto-0.7.1 lib/pronto/gitlab.rb
pronto-0.7.0 lib/pronto/gitlab.rb