<commit_before># coding: utf-8

require "danger/helpers/comments_helper"

module Danger
  module RequestSources
    class BitbucketCloudAPI
      attr_accessor :host, :project, :slug, :access_token, :pull_request_id

      def initialize(repo_slug, pull_request_id, branch_name, environment)
        @username = environment["DANGER_BITBUCKETCLOUD_USERNAME"]
        @password = environment["<PASSWORD>CLOUD_PASSWORD"]
        self.project, self.slug = repo_slug.split("/")
        self.access_token = fetch_access_token(environment)
        self.pull_request_id = pull_request_id || fetch_pr_from_branch(branch_name)
        self.host = "https://bitbucket.org/"
      end

      def inspect
        inspected = super

        if @password
          inspected = inspected.sub! @password, "********".freeze
        end

        inspected
      end

      def credentials_given?
        @username && !@username.empty? && @password && !<PASSWORD>.empty?
      end

      def pull_request(*)
        fetch_pr_json
      end

      def fetch_pr_json
        uri = URI(pr_api_endpoint)
        fetch_json(uri)
      end

      def fetch_last_comments
        uri = URI("#{pr_api_endpoint}/activity?limit=1000")
        fetch_json(uri)[:values].select { |v| v[:comment] }.map { |v| v[:comment] }
      end

      def delete_comment(id)
        uri = URI("#{pr_api_endpoint}/comments/#{id}")
        delete(uri)
      end

      def post_comment(text)
        uri = URI("#{pr_api_endpoint}/comments")
        body = {
          content: { 
            raw: text
          }
        }.to_json
        post(uri, body)
      end

      private

      def base_url(version)
        "https://api.bitbucket.org/#{version}.0/repositories/#{project}/#{slug}/pullrequests"
      end

      def pr_api_endpoint
        "#{base_url(2)}/#{pull_request_id}"
      end

      def prs_api_endpoint(branch_name)<commit_msg>Add file: and line: params to BitbucketCloudAPI<commit_after># coding: utf-8

require "danger/helpers/comments_helper"

module Danger
  module RequestSources
    class BitbucketCloudAPI
      attr_accessor :host, :project, :slug, :access_token, :pull_request_id

      def initialize(repo_slug, pull_request_id, branch_name, environment)
        @username = environment["DANGER_BITBUCKETCLOUD_USERNAME"]
        @password = environment["<PASSWORD>"]
        self.project, self.slug = repo_slug.split("/")
        self.access_token = fetch_access_token(environment)
        self.pull_request_id = pull_request_id || fetch_pr_from_branch(branch_name)
        self.host = "https://bitbucket.org/"
      end

      def inspect
        inspected = super

        if @password
          inspected = inspected.sub! @password, "********".freeze
        end

        inspected
      end

      def credentials_given?
        @username && !@username.empty? && @password && !<PASSWORD>.empty?
      end

      def pull_request(*)
        fetch_pr_json
      end

      def fetch_pr_json
        uri = URI(pr_api_endpoint)
        fetch_json(uri)
      end

      def fetch_last_comments
        uri = URI("#{pr_api_endpoint}/activity?limit=1000")
        fetch_json(uri)[:values].select { |v| v[:comment] }.map { |v| v[:comment] }
      end

      def delete_comment(id)
        uri = URI("#{pr_api_endpoint}/comments/#{id}")
        delete(uri)
      end

      def post_comment(text, file: nil, line: nil)
        uri = URI("#{pr_api_endpoint}/comments")
        body = {
          content: {
            raw: text
          }
        }
        body.merge(inline: { path: file, line: line }) if file && line
        post(uri, body.to_json)
      end

      private

      def base_url(version)
        "https://api.bitbucket.org/#{version}.0/repositories/#{project}/#{slug}/pullrequests"
      end

      def pr_api_endpoint
        "#{base_url(2)}/#{pull_request_id}"
      end

      def prs_api_endpoint(branch_name)
{'fraction_non_alphanumeric': 0.11642784032753327, 'fraction_numerical': 0.0035823950870010235, 'mean_word_length': 2.3991304347826086, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6534142', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1200, 'n_words': 230}