|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'fastlane/action' |
| 4 | +require_relative '../../helper/github_helper' |
| 5 | + |
| 6 | +module Fastlane |
| 7 | + module Actions |
| 8 | + class FindOrCreatePullRequestAction < Action |
| 9 | + def self.run(params) |
| 10 | + github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token]) |
| 11 | + |
| 12 | + existing_pr = github_helper.find_pull_request( |
| 13 | + repository: params[:repository], |
| 14 | + head: params[:head], |
| 15 | + base: params[:base] |
| 16 | + ) |
| 17 | + |
| 18 | + unless existing_pr.nil? |
| 19 | + UI.message("An open Pull Request already exists for `#{params[:head]}`: #{existing_pr.html_url}") |
| 20 | + return existing_pr.html_url |
| 21 | + end |
| 22 | + |
| 23 | + other_action.create_pull_request( |
| 24 | + api_url: params[:api_url], |
| 25 | + api_token: params[:github_token], |
| 26 | + repo: params[:repository], |
| 27 | + title: params[:title], |
| 28 | + body: params[:body], |
| 29 | + draft: params[:draft], |
| 30 | + head: params[:head], |
| 31 | + base: params[:base], |
| 32 | + labels: params[:labels], |
| 33 | + assignees: params[:assignees], |
| 34 | + reviewers: params[:reviewers], |
| 35 | + team_reviewers: params[:team_reviewers], |
| 36 | + milestone: params[:milestone] |
| 37 | + ) |
| 38 | + end |
| 39 | + |
| 40 | + def self.description |
| 41 | + 'Returns the URL of the open Pull Request for a head branch, creating one if none exists yet' |
| 42 | + end |
| 43 | + |
| 44 | + def self.details |
| 45 | + <<~DETAILS |
| 46 | + Looks for an open Pull Request whose head is the given branch and which targets the given base, |
| 47 | + and returns its URL if found. Otherwise, creates a new Pull Request and returns its URL. |
| 48 | +
|
| 49 | + This is useful for "rolling" automations (e.g. a daily translations or dependency-update job) that |
| 50 | + force-push the same head branch on every run: GitHub automatically refreshes the diff of the existing |
| 51 | + PR, so this action only needs to open a PR the first time. |
| 52 | + DETAILS |
| 53 | + end |
| 54 | + |
| 55 | + def self.authors |
| 56 | + ['Automattic'] |
| 57 | + end |
| 58 | + |
| 59 | + def self.return_type |
| 60 | + :string |
| 61 | + end |
| 62 | + |
| 63 | + def self.return_value |
| 64 | + 'The URL of the existing or newly-created Pull Request' |
| 65 | + end |
| 66 | + |
| 67 | + def self.available_options |
| 68 | + # Parameters we forward as-is from Fastlane's `create_pull_request` action |
| 69 | + forwarded_param_keys = %i[ |
| 70 | + api_url |
| 71 | + draft |
| 72 | + labels |
| 73 | + assignees |
| 74 | + reviewers |
| 75 | + team_reviewers |
| 76 | + milestone |
| 77 | + ].freeze |
| 78 | + |
| 79 | + forwarded_params = Fastlane::Actions::CreatePullRequestAction.available_options.select do |opt| |
| 80 | + forwarded_param_keys.include?(opt.key) |
| 81 | + end |
| 82 | + |
| 83 | + [ |
| 84 | + *forwarded_params, |
| 85 | + Fastlane::Helper::GithubHelper.github_token_config_item, # forwarded to `api_token` in the `create_pull_request` action |
| 86 | + FastlaneCore::ConfigItem.new( |
| 87 | + key: :repository, |
| 88 | + env_name: 'GHHELPER_REPOSITORY', |
| 89 | + description: 'The remote path of the GH repository on which we work, e.g. `wordpress-mobile/wordpress-ios`', |
| 90 | + optional: false, |
| 91 | + type: String |
| 92 | + ), |
| 93 | + FastlaneCore::ConfigItem.new( |
| 94 | + key: :title, |
| 95 | + description: 'The title of the Pull Request to create if none exists yet', |
| 96 | + optional: false, |
| 97 | + type: String |
| 98 | + ), |
| 99 | + FastlaneCore::ConfigItem.new( |
| 100 | + key: :body, |
| 101 | + description: 'The body of the Pull Request to create if none exists yet', |
| 102 | + optional: true, |
| 103 | + type: String |
| 104 | + ), |
| 105 | + FastlaneCore::ConfigItem.new( |
| 106 | + key: :head, |
| 107 | + description: 'The head branch of the Pull Request (the branch with the changes)', |
| 108 | + optional: false, |
| 109 | + type: String |
| 110 | + ), |
| 111 | + FastlaneCore::ConfigItem.new( |
| 112 | + key: :base, |
| 113 | + description: 'The base branch the Pull Request targets (e.g. `trunk`)', |
| 114 | + optional: false, |
| 115 | + type: String |
| 116 | + ), |
| 117 | + ] |
| 118 | + end |
| 119 | + |
| 120 | + def self.is_supported?(platform) |
| 121 | + true |
| 122 | + end |
| 123 | + end |
| 124 | + end |
| 125 | +end |
0 commit comments