Skip to content

Commit

Permalink
Merge pull request #12 from natura-cosmeticos/STEM-3000-fech-changed-…
Browse files Browse the repository at this point in the history
…files

Including bitbucket fetch pull request changed files action
  • Loading branch information
dbnaza authored May 31, 2022
2 parents cca47fa + 1de7d89 commit 7587ee8
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 4 deletions.
7 changes: 7 additions & 0 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,11 @@ lane :test do
repo_slug: 'my-repo',
basic_creds: "username:password"
)

bitbucket_fetch_pull_request_changed_files(
project_key: 'my-project',
repo_slug: 'my-repo',
basic_creds: "username:password",
request_id: "pr-id"
)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'fastlane/action'
require_relative '../helper/bitbucket_helper'
require 'base64'


module Fastlane
module Actions
class BitbucketFetchPullRequestChangedFilesAction < Action
def self.run(params)
auth_header = Helper::BitbucketHelper.get_auth_header(params)

if params[:base_url] then
base_url = params[:base_url]
else
base_url = 'https://api.bitbucket.org'
end

Helper::BitbucketHelper.fetch_pull_request_changed_files(auth_header, base_url, params[:project_key], params[:repo_slug], params[:request_id])
end

def self.description
"This action allows fastlane to fetch the list of modified files in a pull request"
end

def self.authors
["Ana Ludmila, Daniel Nazareth, Igor Matos, Elize Delabrida"]
end

def self.return_value
"The return value is the listing of files changed in a pull request"
end

def self.available_options
[
FastlaneCore::ConfigItem.new(
key: :base_url,
env_name: "BITBUCKET_BASE_URL",
description: "The base URL for your BitBucket Server, including protocol",
optional: true,
type: String,
verify_block: proc do |value|
UI.user_error!("Bitbucket Server url should use https") unless !!(value.match"^https://")
end
),
FastlaneCore::ConfigItem.new(
key: :project_key,
env_name: "BITBUCKET_PROJECT_KEY",
description: "The Project Key for your repository",
optional: false,
type: String
),
FastlaneCore::ConfigItem.new(
key: :repo_slug,
env_name: "BITBUCKET_REPO_SLUG",
description: "The repository slug for your repository",
optional: false,
type: String
),
FastlaneCore::ConfigItem.new(
key: :access_token,
env_name: "BITBUCKET_PERSONAL_ACCESS_TOKEN",
description: "A Personal Access Token from BitBucket for the account used. One of access_token or basic_creds must be specified; if both are supplied access_token is used",
optional: true,
type: String
),
FastlaneCore::ConfigItem.new(
key: :basic_creds,
env_name: "BITBUCKET_BASIC_AUTH_CREDENTIALS",
description: "To use Basic Auth for Bitbuket provide a base64 encoded version of \"<username>:<password>\". One of access_token or basic_creds must be specified; if both are supplied access_token is used",
optional: true,
type: String
),
FastlaneCore::ConfigItem.new(
key: :request_id,
description: "The Identifier Key For Your Pull Request",
type: Integer,
optional: true
)
]
end

def self.is_supported?(platform)
true
end
end
end
end
21 changes: 17 additions & 4 deletions lib/fastlane/plugin/bitbucket/helper/bitbucket_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def self.response_handler(response)
end
end

def self.perform_get(uri, access_header, params)
def self.perform_get(uri, access_header, params, limit = 10)
raise ArgumentError, 'HTTP redirect too deep' if limit == 0

uri.query = URI.encode_www_form(params)

req = Net::HTTP::Get.new(uri)
Expand All @@ -40,10 +42,14 @@ def self.perform_get(uri, access_header, params)
http.use_ssl = uri.instance_of? URI::HTTPS
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

res = http.request(req)
self.response_handler(res)
response = http.request(req)
self.response_handler(response)

res
while response.is_a?(Net::HTTPRedirection) && limit >= 0
response = self.perform_get(URI.parse(response['location']), access_header, params, limit -1)
end

response
end

def self.perform_post(uri, access_header, params, query_params={})
Expand Down Expand Up @@ -124,6 +130,13 @@ def self.fetch_diffstat(access_header, baseurl, project_key, repo_slug, request_
data
end

def self.fetch_pull_request_changed_files(access_header, baseurl, project_key, repo_slug, request_id)
pruri = URI.parse("#{baseurl}/2.0/repositories/#{project_key}/#{repo_slug}/pullrequests/#{request_id}/diffstat")
prresp = self.perform_get(pruri, access_header, { from_pullrequest_id: request_id })
data = JSON.parse(prresp.body)
data
end

def self.get_pull_request_version(access_header, baseurl, project_key, repo_slug, request_id)
data = self.fetch_pull_request(access_header, baseurl, project_key, repo_slug, request_id)
version = data["version"]
Expand Down

0 comments on commit 7587ee8

Please sign in to comment.