diff --git a/fastlane/Fastfile b/fastlane/Fastfile index aa87810..6526c8d 100644 --- a/fastlane/Fastfile +++ b/fastlane/Fastfile @@ -153,4 +153,14 @@ fourth line prefix: "+", text: text ) + + bitbucket_open_pull_request( + base_url: "https://api.bitbucket.org", + project_key: 'my-project', + repo_slug: 'my-repo', + basic_creds: "username:password", + title: "My PR Title", + source_branch: "my-new-branch-name", + destination_branch: "main" + ) end \ No newline at end of file diff --git a/lib/fastlane/plugin/bitbucket/actions/bitbucket_open_pull_request_action.rb b/lib/fastlane/plugin/bitbucket/actions/bitbucket_open_pull_request_action.rb new file mode 100644 index 0000000..99eca78 --- /dev/null +++ b/lib/fastlane/plugin/bitbucket/actions/bitbucket_open_pull_request_action.rb @@ -0,0 +1,110 @@ +require 'fastlane/action' +require_relative '../helper/bitbucket_helper' +require 'base64' + +module Fastlane + module Actions + class BitbucketOpenPullRequestAction < Action + def self.run(params) + title = params[:title] + source_branch = params[:source_branch] + destination_branch = params[:destination_branch] + + 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 + + if params[:close_source_branch] then + close_source_branch = params[:close_source_branch] + else + close_source_branch = true + end + + Helper::BitbucketHelper.open_pull_request(auth_header, base_url, params[:project_key], params[:repo_slug], title, source_branch, destination_branch, close_source_branch) + end + + def self.description + "This action allows fastlane to open BitBucket Pull Requests." + end + + def self.authors + ["Daniel Nazareth"] + 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 \":\". 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: :source_branch, + description: "The pull request source branch", + type: String, + optional: false + ), + FastlaneCore::ConfigItem.new( + key: :destination_branch, + description: "The pull destination branch", + type: String, + optional: false + ), + FastlaneCore::ConfigItem.new( + key: :close_source_branch, + description: "Should close source branch after pull request merge", + type: Boolean, + optional: true + ), + FastlaneCore::ConfigItem.new( + key: :title, + description: "The pull request title", + type: String, + optional: false + ) + ] + end + + def self.is_supported?(platform) + true + end + end + end +end diff --git a/lib/fastlane/plugin/bitbucket/helper/bitbucket_helper.rb b/lib/fastlane/plugin/bitbucket/helper/bitbucket_helper.rb index 4cc8919..17367ec 100644 --- a/lib/fastlane/plugin/bitbucket/helper/bitbucket_helper.rb +++ b/lib/fastlane/plugin/bitbucket/helper/bitbucket_helper.rb @@ -219,6 +219,24 @@ def self.compare_two_commits(access_header, baseurl, project_key, repo_slug, sou data = prresp.body data end + + def self.open_pull_request(access_header, baseurl, project_key, repo_slug, title, source_branch, destination_branch, close_source_branch=true) + uri = URI.parse("#{baseurl}/2.0/repositories/#{project_key}/#{repo_slug}/pullrequests") + self.perform_post(uri, access_header, { + "title": "#{title}", + "source": { + "branch": { + "name": "#{source_branch}" + } + }, + "destination": { + "branch": { + "name": "#{destination_branch}" + } + }, + "close_source_branch": close_source_branch + }) + end end end end diff --git a/lib/fastlane/plugin/bitbucket/version.rb b/lib/fastlane/plugin/bitbucket/version.rb index b805696..897eaea 100644 --- a/lib/fastlane/plugin/bitbucket/version.rb +++ b/lib/fastlane/plugin/bitbucket/version.rb @@ -1,5 +1,5 @@ module Fastlane module Bitbucket - VERSION = "0.1.23" + VERSION = "0.1.24" end end