-
-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for GitLab merge requests (MRs) #175
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4e80b0e
Extend Git ignores
mhutter 1a5cf9f
Add GitLab merge request (MR) support
bittner 9a0b374
Refactor PR/MR creation
mhutter a77b968
Cleanup: move config validation out of processing
mhutter bf27fd8
Get rid of constants for PR tokens
mhutter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
tmp/ | ||
modules/ | ||
*.gem | ||
Gemfile.lock | ||
.bundle/ | ||
.ruby-version | ||
.vscode/ | ||
Gemfile.lock | ||
bin/bundle | ||
bin/rspec | ||
modules/ | ||
tmp/ | ||
vendor/ |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'octokit' | ||
require 'modulesync/util' | ||
|
||
module ModuleSync | ||
module PR | ||
# GitHub creates and manages pull requests on github.com or GitHub | ||
# Enterprise installations. | ||
class GitHub | ||
def initialize(token, endpoint) | ||
Octokit.configure do |c| | ||
c.api_endpoint = endpoint | ||
end | ||
@api = Octokit::Client.new(:access_token => token) | ||
end | ||
|
||
def manage(namespace, module_name, options) | ||
repo_path = File.join(namespace, module_name) | ||
head = "#{namespace}:#{options[:branch]}" | ||
|
||
pull_requests = @api.pull_requests(repo_path, :state => 'open', :base => 'master', :head => head) | ||
if pull_requests.empty? | ||
pr = @api.create_pull_request(repo_path, 'master', options[:branch], options[:pr_title], options[:message]) | ||
$stdout.puts "Submitted PR '#{options[:pr_title]}' to #{repo_path} - merges #{options[:branch]} into master" | ||
else | ||
# Skip creating the PR if it exists already. | ||
$stdout.puts "Skipped! #{pull_requests.length} PRs found for branch #{options[:branch]}" | ||
end | ||
|
||
# PR labels can either be a list in the YAML file or they can pass in a comma | ||
# separated list via the command line argument. | ||
pr_labels = ModuleSync::Util.parse_list(options[:pr_labels]) | ||
|
||
# We only assign labels to the PR if we've discovered a list > 1. The labels MUST | ||
# already exist. We DO NOT create missing labels. | ||
return if pr_labels.empty? | ||
$stdout.puts "Attaching the following labels to PR #{pr['number']}: #{pr_labels.join(', ')}" | ||
@api.add_labels_to_an_issue(repo_path, pr['number'], pr_labels) | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'gitlab' | ||
require 'modulesync/util' | ||
|
||
module ModuleSync | ||
module PR | ||
# GitLab creates and manages merge requests on gitlab.com or private GitLab | ||
# installations. | ||
class GitLab | ||
def initialize(token, endpoint) | ||
@api = Gitlab::Client.new( | ||
:endpoint => endpoint, | ||
:private_token => token | ||
) | ||
end | ||
|
||
def manage(namespace, module_name, options) | ||
repo_path = File.join(namespace, module_name) | ||
|
||
head = "#{namespace}:#{options[:branch]}" | ||
merge_requests = @api.merge_requests(repo_path, | ||
:state => 'opened', | ||
:source_branch => head, | ||
:target_branch => 'master') | ||
if merge_requests.empty? | ||
mr_labels = ModuleSync::Util.parse_list(options[:pr_labels]) | ||
mr = @api.create_merge_request(repo_path, options[:pr_title], | ||
:source_branch => options[:branch], | ||
:target_branch => 'master', | ||
:labels => mr_labels) | ||
$stdout.puts "Submitted MR '#{options[:pr_title]}' to #{repo_path} - merges #{options[:branch]} into master" | ||
return if mr_labels.empty? | ||
$stdout.puts "Attached the following labels to MR #{mr.iid}: #{mr_labels.join(', ')}" | ||
else | ||
# Skip creating the MR if it exists already. | ||
$stdout.puts "Skipped! #{merge_requests.length} MRs found for branch #{options[:branch]}" | ||
end | ||
end | ||
end | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'spec_helper' | ||
require 'modulesync/pr/github' | ||
|
||
describe ModuleSync::PR::GitHub do | ||
context '::manage' do | ||
before(:each) do | ||
@git_repo = 'test/modulesync' | ||
@namespace, @repo_name = @git_repo.split('/') | ||
@options = { | ||
:pr => true, | ||
:pr_title => 'Test PR is submitted', | ||
:branch => 'test', | ||
:message => 'Hello world', | ||
:pr_auto_merge => false, | ||
} | ||
|
||
@client = double() | ||
allow(Octokit::Client).to receive(:new).and_return(@client) | ||
@it = ModuleSync::PR::GitHub.new('test', 'https://api.github.com') | ||
end | ||
|
||
it 'submits PR when --pr is set' do | ||
allow(@client).to receive(:pull_requests).with(@git_repo, :state => 'open', :base => 'master', :head => "#{@namespace}:#{@options[:branch]}").and_return([]) | ||
expect(@client).to receive(:create_pull_request).with(@git_repo, 'master', @options[:branch], @options[:pr_title], @options[:message]).and_return({"html_url" => "http://example.com/pulls/22"}) | ||
expect { @it.manage(@namespace, @repo_name, @options) }.to output(/Submitted PR/).to_stdout | ||
end | ||
|
||
it 'skips submitting PR if one has already been issued' do | ||
pr = { | ||
"title" => "Test title", | ||
"html_url" => "https://example.com/pulls/44", | ||
"number" => "44" | ||
} | ||
|
||
expect(@client).to receive(:pull_requests).with(@git_repo, :state => 'open', :base => 'master', :head => "#{@namespace}:#{@options[:branch]}").and_return([pr]) | ||
expect { @it.manage(@namespace, @repo_name, @options) }.to output(/Skipped! 1 PRs found for branch test/).to_stdout | ||
end | ||
|
||
it 'adds labels to PR when --pr-labels is set' do | ||
@options[:pr_labels] = "HELLO,WORLD" | ||
|
||
allow(@client).to receive(:create_pull_request).and_return({"html_url" => "http://example.com/pulls/22", "number" => "44"}) | ||
allow(@client).to receive(:pull_requests).with(@git_repo, :state => 'open', :base => 'master', :head => "#{@namespace}:#{@options[:branch]}").and_return([]) | ||
|
||
expect(@client).to receive(:add_labels_to_an_issue).with(@git_repo, "44", ["HELLO", "WORLD"]) | ||
expect { @it.manage(@namespace, @repo_name, @options) }.to output(/Attaching the following labels to PR 44: HELLO, WORLD/).to_stdout | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can avoid these constants and only use them inline since they're only used in a single place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, totally missed it. Will adjust accordingly