Class: Fastlane::Actions::FindOrCreatePullRequestAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/wpmreleasetoolkit/actions/common/find_or_create_pull_request_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



55
56
57
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/find_or_create_pull_request_action.rb', line 55

def self.authors
  ['Automattic']
end

.available_optionsObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/find_or_create_pull_request_action.rb', line 67

def self.available_options
  # Parameters we forward as-is from Fastlane's `create_pull_request` action
  forwarded_param_keys = %i[
    api_url
    draft
    labels
    assignees
    reviewers
    team_reviewers
    milestone
  ].freeze

  forwarded_params = Fastlane::Actions::CreatePullRequestAction.available_options.select do |opt|
    forwarded_param_keys.include?(opt.key)
  end

  [
    *forwarded_params,
    Fastlane::Helper::GithubHelper.github_token_config_item, # forwarded to `api_token` in the `create_pull_request` action
    FastlaneCore::ConfigItem.new(
      key: :repository,
      env_name: 'GHHELPER_REPOSITORY',
      description: 'The remote path of the GH repository on which we work, e.g. `wordpress-mobile/wordpress-ios`',
      optional: false,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :title,
      description: 'The title of the Pull Request to create if none exists yet',
      optional: false,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :body,
      description: 'The body of the Pull Request to create if none exists yet',
      optional: true,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :head,
      description: 'The head branch of the Pull Request (the branch with the changes)',
      optional: false,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :base,
      description: 'The base branch the Pull Request targets (e.g. `trunk`)',
      optional: false,
      type: String
    ),
  ]
end

.descriptionObject



40
41
42
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/find_or_create_pull_request_action.rb', line 40

def self.description
  'Returns the URL of the open Pull Request for a head branch, creating one if none exists yet'
end

.detailsObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/find_or_create_pull_request_action.rb', line 44

def self.details
  <<~DETAILS
    Looks for an open Pull Request whose head is the given branch and which targets the given base,
    and returns its URL if found. Otherwise, creates a new Pull Request and returns its URL.

    This is useful for "rolling" automations (e.g. a daily translations or dependency-update job) that
    force-push the same head branch on every run: GitHub automatically refreshes the diff of the existing
    PR, so this action only needs to open a PR the first time.
  DETAILS
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/find_or_create_pull_request_action.rb', line 120

def self.is_supported?(platform)
  true
end

.return_typeObject



59
60
61
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/find_or_create_pull_request_action.rb', line 59

def self.return_type
  :string
end

.return_valueObject



63
64
65
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/find_or_create_pull_request_action.rb', line 63

def self.return_value
  'The URL of the existing or newly-created Pull Request'
end

.run(params) ⇒ Object



9
10
11
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
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/find_or_create_pull_request_action.rb', line 9

def self.run(params)
  github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token])

  existing_pr = github_helper.find_pull_request(
    repository: params[:repository],
    head: params[:head],
    base: params[:base]
  )

  unless existing_pr.nil?
    UI.message("An open Pull Request already exists for `#{params[:head]}`: #{existing_pr.html_url}")
    return existing_pr.html_url
  end

  other_action.create_pull_request(
    api_url: params[:api_url],
    api_token: params[:github_token],
    repo: params[:repository],
    title: params[:title],
    body: params[:body],
    draft: params[:draft],
    head: params[:head],
    base: params[:base],
    labels: params[:labels],
    assignees: params[:assignees],
    reviewers: params[:reviewers],
    team_reviewers: params[:team_reviewers],
    milestone: params[:milestone]
  )
end