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, output, return_value, sh, step_text

Class Method Details

.authorObject



87
88
89
# File 'lib/fastlane/actions/create_pull_request.rb', line 87

def self.author
  ["seei"]
end

.available_optionsObject



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

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)
  ]
end

.descriptionObject



46
47
48
# File 'lib/fastlane/actions/create_pull_request.rb', line 46

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

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


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

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
39
40
# File 'lib/fastlane/actions/create_pull_request.rb', line 8

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

  Helper.log.info "Creating new pull request from '#{params[:head]}' to branch '#{params[:base]}' of '#{params[:repo]}'"

  url = "https://api.github.com/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']
    Helper.log.info "Successfully created pull request ##{number}. You can see it at '#{html_url}'".green

    Actions.lane_context[SharedValues::CREATE_PULL_REQUEST_HTML_URL] = html_url
  else
    if response[:status] != 200
      Helper.log.error "GitHub responded with #{response[:status]}: #{response[:body]}".red
    end
  end
end