Created
December 27, 2016 07:49
-
-
Save yimingtang/b65c58503443b0d252f37ce71323dbf0 to your computer and use it in GitHub Desktop.
A fastlane action to create a new merge request on GitLab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Fastlane | |
module Actions | |
module SharedValues | |
CREATE_MERGE_REQUEST_WEB_URL = :CREATE_MERGE_REQUEST_WEB_URL | |
end | |
class CreateMergeRequestAction < Action | |
def self.run(params) | |
require 'excon' | |
target_project_id = params[:target_project_id] ? params[:target_project_id] : params[:project_id] | |
UI.message("Creating new merge request from #{params[:source]} of project #{params[:project_id]} to branch #{params[:target]} of project #{target_project_id}") | |
# Build HTTP request | |
url = "#{params[:api_url]}/projects/#{params[:project_id]}/merge_requests" | |
headers = { | |
'User-Agent' => 'fastlane-create_merge_request', | |
'Content-Type' => 'application/json', | |
} | |
headers['PRIVATE-TOKEN'] = "#{params[:api_token]}" if params[:api_token] | |
data = { | |
'title' => params[:title], | |
'source_branch' => params[:source], | |
'target_branch' => params[:target], | |
} | |
data['description'] = params[:body] if params[:body] | |
data['target_project_id'] = target_project_id | |
# Post it | |
response = Excon.post(url, headers: headers, body: data.to_json) | |
if response[:status] == 201 | |
body = JSON.parse(response.body) | |
id = body['iid'] | |
web_url = body['web_url'] | |
Actions.lane_context[SharedValues::CREATE_MERGE_REQUEST_WEB_URL] = web_url | |
UI.success("Successfully created pull request ##{id}. You can see it at '#{web_url}'") | |
elsif response[:status] != 200 | |
UI.error("GitLab responded with #{response[:status]}: #{response[:body]}") | |
end | |
end | |
##################################################### | |
# @!group Documentation | |
##################################################### | |
def self.description | |
"Create a new merge request on GitLab" | |
end | |
def self.available_options | |
[ | |
FastlaneCore::ConfigItem.new(key: :api_token, | |
env_name: "GITLAB_MERGE_REQUEST_API_TOKEN", | |
description: "Personal API Access Token for GitLab", | |
sensitive: true, | |
default_value: ENV["GITLAB_API_TOKEN"], | |
is_string: true, | |
optional: false), | |
FastlaneCore::ConfigItem.new(key: :project_id, | |
env_name: "GITLAB_MERGE_REQUEST_PROJECT_ID", | |
description: "The id of the project", | |
is_string: true, | |
optional: false), | |
FastlaneCore::ConfigItem.new(key: :target_project_id, | |
env_name: "GITLAB_MERGE_REQUEST_PROJECT_TARGET_PROJECT_ID", | |
description: "The id of the project you want to submit the merge request to", | |
is_string: true, | |
optional: true), | |
FastlaneCore::ConfigItem.new(key: :title, | |
env_name: "GITLAB_MERGE_REQUEST_TITLE", | |
description: "The title of the merge request", | |
is_string: true, | |
optional: false), | |
FastlaneCore::ConfigItem.new(key: :body, | |
env_name: "GITLAB_MERGE_REQUEST_BODY", | |
description: "The contents of the pull request", | |
is_string: true, | |
optional: true), | |
FastlaneCore::ConfigItem.new(key: :source, | |
env_name: "GITLAB_MERGE_REQUEST_SOURCE_BRANCH", | |
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: :target, | |
env_name: "GITLAB_MERGE_REQUEST_TARGET_BRANCH", | |
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: "GITLAB_MERGE_REQUEST_API_URL", | |
description: "The URL of GitLab API", | |
is_string: true, | |
default_value: 'https://gitlab.com/api/v3', | |
optional: true) | |
] | |
end | |
def self.output | |
[ | |
['CREATE_MERGE_REQUEST_WEB_URL', 'Web url of the merge request that we created.'] | |
] | |
end | |
def self.return_value | |
# If you method provides a return value, you can describe here what it does | |
end | |
def self.authors | |
['Yiming Tang'] | |
end | |
def self.is_supported?(platform) | |
return true | |
end | |
def self.example_code | |
[ | |
'create_merge_request( | |
api_token: ENV["GITLAB_TOKEN"], | |
project_id: "100", | |
target_project_id: "90" | |
title: "Amazing new feature", | |
source: "my-feature", # optional, defaults to current branch name | |
target: "master", # optional, defaults to "master" | |
body: "Please pull this in!", # optional | |
api_url: "http://yourdomain/api/v3" # optional | |
)' | |
] | |
end | |
def self.category | |
:source_control | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment