Class: Fastlane::Actions::CreatePullRequestAction

Inherits:
Fastlane::Action show all
Defined in:
lib/fastlane/actions/create_pull_request.rb

Documentation collapse

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, authors, details, method_missing, other_action, output, return_value, sh, step_text

Class Method Details

.authorObject



91
92
93
# File 'lib/fastlane/actions/create_pull_request.rb', line 91

def self.author
  ["seei"]
end

.available_optionsObject



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
86
87
88
89
# File 'lib/fastlane/actions/create_pull_request.rb', line 48

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :api_token,
                                 env_name: "GITHUB_PULL_REQUEST_API_TOKEN",
                                 description: "Personal API Token for GitHub - generate one at https://github.com/settings/tokens",
                                 is_string: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :repo,
                                 env_name: "GITHUB_PULL_REQUEST_REPO",
                                 description: "The name of the repository you want to submit the pull request to",
                                 is_string: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :title,
                                 env_name: "GITHUB_PULL_REQUEST_TITLE",
                                 description: "The title of the pull request",
                                 is_string: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :body,
                                 env_name: "GITHUB_PULL_REQUEST_BODY",
                                 description: "The contents of the pull request",
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :head,
                                 env_name: "GITHUB_PULL_REQUEST_HEAD",
                                 description: "The name of the branch where your changes are implemented (defaults to the current branch name)",
                                 is_string: true,
                                 default_value: Actions.git_branch,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :base,
                                 env_name: "GITHUB_PULL_REQUEST_BASE",
                                 description: "The name of the branch you want your changes pulled into (defaults to `master`)",
                                 is_string: true,
                                 default_value: 'master',
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :api_url,
                                 env_name: "GITHUB_PULL_REQUEST_API_URL",
                                 description: "The URL of Github API - used when the Enterprise (default to `https://api.github.com`)",
                                 is_string: true,
                                 default_value: 'https://api.github.com',
                                 optional: true)
  ]
end

.descriptionObject



44
45
46
# File 'lib/fastlane/actions/create_pull_request.rb', line 44

def self.description
  "This will create a new pull request on GitHub"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/fastlane/actions/create_pull_request.rb', line 95

def self.is_supported?(platform)
  return true
end

.run(params) ⇒ Object



8
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/actions/create_pull_request.rb', line 8

def self.run(params)
  require 'excon'
  require 'base64'

  UI.message("Creating new pull request from '#{params[:head]}' to branch '#{params[:base]}' of '#{params[:repo]}'")

  url = "#{params[:api_url]}/repos/#{params[:repo]}/pulls"
  headers = { 'User-Agent' => 'fastlane-create_pull_request' }
  headers['Authorization'] = "Basic #{Base64.strict_encode64(params[:api_token])}" if params[:api_token]

  data = {
    'title' => params[:title],
    'head' => params[:head],
    'base' => params[:base]
  }

  data['body'] = params[:body] if params[:body]

  response = Excon.post(url, headers: headers, body: data.to_json)

  if response[:status] == 201
    body = JSON.parse(response.body)
    number = body['number']
    html_url = body['html_url']
    UI.success("Successfully created pull request ##{number}. You can see it at '#{html_url}'")

    Actions.lane_context[SharedValues::CREATE_PULL_REQUEST_HTML_URL] = html_url
  elsif response[:status] != 200
    UI.error("GitHub responded with #{response[:status]}: #{response[:body]}")
  end
end