Class: Fastlane::Actions::BitbucketCreatePullRequestAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/bitbucket_cloud/actions/bitbucket_create_pull_request.rb

Class Method Summary collapse

Class Method Details

.authorsObject



116
117
118
# File 'lib/fastlane/plugin/bitbucket_cloud/actions/bitbucket_create_pull_request.rb', line 116

def self.authors
  ["Luca Tagliabue"]
end

.available_optionsObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/fastlane/plugin/bitbucket_cloud/actions/bitbucket_create_pull_request.rb', line 120

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :username,
                                 env_name: "FL_POST_BITBUCKET_PULL_REQUEST_USERNAME",
                                 description: "Bitbucket username",
                                 sensitive: true,
                                 code_gen_sensitive: true,
                                 is_string: true,
                                 default_value: ENV.fetch("BITBUCKET_USERNAME", nil),
                                 default_value_dynamic: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :password,
                                 env_name: "FL_POST_BITBUCKET_PULL_REQUEST_PASSWORD",
                                 description: "Bitbucket password",
                                 sensitive: true,
                                 code_gen_sensitive: true,
                                 is_string: true,
                                 default_value: ENV.fetch("BITBUCKET_PASSWORD", nil),
                                 default_value_dynamic: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :company_host_name,
                                 env_name: "FL_POST_BITBUCKET_PULL_REQUEST_COMPANY_HOST_NAME",
                                 description: "Bitbucket company host name",
                                 sensitive: true,
                                 code_gen_sensitive: true,
                                 is_string: true,
                                 default_value: ENV.fetch("BITBUCKET_COMPANY_HOST_NAME", nil),
                                 default_value_dynamic: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :repository_name,
                                 env_name: "FL_POST_BITBUCKET_PULL_REQUEST_REPOSITORY_NAME",
                                 description: "Bitbucket repository name",
                                 sensitive: true,
                                 code_gen_sensitive: true,
                                 is_string: true,
                                 default_value: ENV.fetch("BITBUCKET_REPOSITORY_NAME", nil),
                                 default_value_dynamic: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :title,
                                 env_name: "FL_POST_BITBUCKET_PULL_REQUEST_TITLE",
                                 description: "Title of the pull request",
                                 is_string: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :description,
                                 env_name: "FL_POST_BITBUCKET_PULL_REQUEST_DESCRIPTION",
                                 description: "Description of the pull request",
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :reviewers,
                                 env_name: "FL_POST_BITBUCKET_PULL_REQUEST_REVIEWERS",
                                 description: "List of reviewer's usernames for the pull request. If no reviewers are passed, fails back to default ones",
                                 type: Array,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :source_branch,
                                 env_name: "FL_POST_BITBUCKET_PULL_REQUEST_SOURCE_BRANCH",
                                 description: "Name of the source branch",
                                 is_string: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :destination_branch,
                                 env_name: "FL_POST_BITBUCKET_PULL_REQUEST_DESTINATION_BRANCH",
                                 description: "Name of the destination branch",
                                 is_string: true,
                                 optional: true)
  ]
end

.descriptionObject



108
109
110
# File 'lib/fastlane/plugin/bitbucket_cloud/actions/bitbucket_create_pull_request.rb', line 108

def self.description
  "Create a new pull request inside your Bitbucket project"
end

.detailsObject



112
113
114
# File 'lib/fastlane/plugin/bitbucket_cloud/actions/bitbucket_create_pull_request.rb', line 112

def self.details
  "Wrapper of Bitbucket cloud rest apis in order to make easy integration of Bitbucket CI inside fastlane workflow"
end

.example_codeObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/fastlane/plugin/bitbucket_cloud/actions/bitbucket_create_pull_request.rb', line 196

def self.example_code
  [
    'bitbucket_create_pull_request(
        username: "YOUR_USERNAME_HERE",
        password: "YOUR_PASSWORD_HERE",
        company_host_name: "YOUR_COMPANY_HOST_HERE",
        repository_name: "YOUR_REPOSITORY_NAME_HERE",
        title: "PULL_REQUEST_TITLE_HERE",
        description: "PULL_REQUEST_DESCRIPTION_HERE",
        reviewers: ["FIRST_REVIEWER", "SECOND_REVIEWER"],
        source_branch: "YOUR_SOURCE_BRANCH_HERE",
        destination_branch: "YOUR_DESTINATION_BRANCH_HERE"
    )'
  ]
end

.formatted_context_result(response) ⇒ Object



96
97
98
# File 'lib/fastlane/plugin/bitbucket_cloud/actions/bitbucket_create_pull_request.rb', line 96

def self.formatted_context_result(response)
  "Status code: #{response[:status]}, reason: #{response[:reason_phrase]}"
end

.formatted_result(response) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/fastlane/plugin/bitbucket_cloud/actions/bitbucket_create_pull_request.rb', line 87

def self.formatted_result(response)
  {
    status: response[:status],
    reason_phrase: response[:reason_phrase],
    body: response.body || "",
    json: self.parse_json(response.body) || {}
  }
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/fastlane/plugin/bitbucket_cloud/actions/bitbucket_create_pull_request.rb', line 212

def self.is_supported?(platform)
  true
end

.outputObject



186
187
188
189
190
# File 'lib/fastlane/plugin/bitbucket_cloud/actions/bitbucket_create_pull_request.rb', line 186

def self.output
  [
    ['BITBUCKET_CREATE_PULL_REQUEST_RESULT', 'The result of the bitbucket rest cloud api']
  ]
end

.parse_json(value) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/fastlane/plugin/bitbucket_cloud/actions/bitbucket_create_pull_request.rb', line 100

def self.parse_json(value)
  require 'json'

  JSON.parse(value)
rescue JSON::ParserError
  nil
end

.return_valueObject



192
193
194
# File 'lib/fastlane/plugin/bitbucket_cloud/actions/bitbucket_create_pull_request.rb', line 192

def self.return_value
  'The result of the bitbucket rest cloud api'
end

.run(options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fastlane/plugin/bitbucket_cloud/actions/bitbucket_create_pull_request.rb', line 12

def self.run(options)
  require 'excon'

  company_host_name = options[:company_host_name]
  repository_name = options[:repository_name]
  destination_branch = options[:destination_branch]
  description = options[:description]
  reviewers = options[:reviewers]

  api_token = Base64.strict_encode64("#{options[:username]}:#{options[:password]}")

  api_url = "https://api.bitbucket.org/2.0/repositories/#{company_host_name}/#{repository_name}/pullrequests"

  headers = { "Content-Type": "application/json", Authorization: "Basic #{api_token}" }

  payload = {
    title: options[:title],
    source: {
        branch: {
            name: options[:source_branch]
        }
    }
  }

  if destination_branch.instance_of?(NilClass)
    destination_log = ""
  else
    destination_obj = {
      branch: {
          name: destination_branch
      }
    }
    payload[:destination] = destination_obj
    destination_log = " to '#{destination_branch}'"
  end

  if description.instance_of?(NilClass)
    description_log = ""
  else
    payload[:description] = description
    description_log = " and description '#{description}'"
  end

  unless reviewers.instance_of?(NilClass)
    reviewers_obj = reviewers.map do |reviewer|
      {
        username: reviewer

      }
    end
    payload[:reviewers] = reviewers_obj
  end

  payload = payload.to_json

  UI.important("Plugin Bitbucket will create a new pull request from '#{options[:source_branch]}'#{destination_log} with title '#{options[:title]}'#{description_log}")

  response = Excon.post(api_url, headers: headers, body: payload)

  result = self.formatted_result(response)

  UI.important("Plugin Bitbucket finished with result")
  UI.important(result.to_s)

  Actions.lane_context[SharedValues::BITBUCKET_CREATE_PULL_REQUEST_RESULT] = formatted_context_result(response)

  if result[:status] != 201
    error_message = "Plugin Bitbucket finished with error code #{result[:status]} #{result[:reason_phrase]}"
    raise StandardError, error_message
  end

  UI.success("Successfully create a new Bitbucket pull request!")
  return result
end