This commit is contained in:
Miguel Savignano 2019-11-24 14:23:33 +01:00
parent 8de3e1e005
commit fbe67681fc
3 changed files with 80 additions and 30 deletions

View File

@ -1,7 +1,6 @@
require 'json'
require 'octokit'
require_relative './labeled_adapter'
require_relative './push_adapter'
require_relative './merge_branch_service'
@event = JSON.parse(File.read(ENV['GITHUB_EVENT_PATH']))
@head_to_merge = ENV['GITHUB_SHA'] # or brach name
@ -11,33 +10,15 @@ require_relative './push_adapter'
@label_name = ENV['INPUT_LABEL_NAME']
@type = ENV['INPUT_TYPE'] || 'push' # labeled | push
def merge_to(base_branch_name)
service = MergeBrachService.new(
event: @event, type: @type, base_branch: @base_branch, label_name: @label_name
)
service_base_branch = service.base_branch
if service_base_branch
@client = Octokit::Client.new(access_token: @github_token)
@client.merge(@repository, base_branch_name, @head_to_merge)
puts "Finish merge brach #{base_branch_name}"
@client.merge(@repository, service_base_branch, @head_to_merge)
puts "Finish merge brach #{service_base_branch}"
else
puts 'Skip'
end
def build_adapter
case @type
when 'push'
PushAdapter.new(@event, @base_branch)
when 'labeled'
LabeledAdapter.new(@event, @base_branch, @label_name)
end
end
def run
adapter = build_adapter
unless adapter.valid?
puts 'Skip'
return 'Skip'
end
adapter.base_branch.tap do |base_branch_name|
raise 'Could not find branch name' unless base_branch_name
merge_to(base_branch)
end
end
run

View File

@ -0,0 +1,29 @@
require_relative './labeled_adapter'
require_relative './push_adapter'
class MergeBrachService
def initialize(inputs)
@inputs = inputs
end
def base_branch
adapter = build_adapter
return nil unless adapter.valid?
adapter.base_branch.tap do |base_branch|
raise 'Could not find branch name' unless base_branch || base_branch.empty?
end
end
private
def build_adapter
case @inputs[:type]
when 'push'
PushAdapter.new(@inputs[:event], @inputs[:base_branch])
when 'labeled'
LabeledAdapter.new(@inputs[:event], @inputs[:base_branch], @inputs[:label_name])
end
end
end

View File

@ -0,0 +1,40 @@
require_relative '../lib/merge_branch_service'
describe MergeBrachService do
context "with push" do
let(:base_branch) { 'develop' }
let(:inputs) {
{ type: 'push', event: {}, base_branch: base_branch }
}
it "#base_branch" do
service = MergeBrachService.new(inputs)
expect(service.base_branch).to eq('develop')
end
end
context "with labeled" do
let(:label_name) { 'merge in develop' }
let(:base_branch) { 'develop' }
let(:event) { { 'action' => 'labeled', 'label' => { 'name' => label_name } } }
let(:inputs) {
{ type: 'labeled', event: event, base_branch: base_branch, label_name: label_name }
}
context "match label" do
it "#base_branch" do
service = MergeBrachService.new(inputs)
expect(service.base_branch).to eq('develop')
end
end
context "not match label" do
let(:event) { { 'action' => 'labeled', 'label' => { 'name' => 'other label' } } }
it "#base_branch" do
service = MergeBrachService.new(inputs)
expect(service.base_branch).to be_nil
end
end
end
end