diff --git a/src/main/rb/debug.rb b/src/main/rb/debug.rb new file mode 100644 index 0000000000000000000000000000000000000000..fa20b3d06c6bf2038ee8b69e5dbfe0df70bb6d05 --- /dev/null +++ b/src/main/rb/debug.rb @@ -0,0 +1,59 @@ +#!/usr/bin/env ruby +# encoding: UTF-8 + +## Simplified version of eca.rb, made to help debug what is being output by + +require 'json' +require 'httparty' +require 'multi_json' + +## Process the commit into a hash object that will be posted to the ECA validation service +def process_commit(sha) + commit_parents_raw = `git show -s --format='%P' #{sha}` + commit_parents = commit_parents_raw.split(/\s/) + return { + :author => { + :name => `git show -s --format='%an' #{sha}`.force_encoding("utf-8"), + :mail => `git show -s --format='%ae' #{sha}`.force_encoding("utf-8"), + }, + :committer => { + :name => `git show -s --format='%cn' #{sha}`.force_encoding("utf-8"), + :mail => `git show -s --format='%ce' #{sha}`.force_encoding("utf-8"), + }, + :body => `git show -s --format='%B' #{sha}`.force_encoding("utf-8"), + :subject => `git show -s --format='%s' #{sha}`.force_encoding("utf-8"), + :hash => `git show -s --format='%H' #{sha}`, + :parents => commit_parents + } +end + +## Read in the arguments passed from GitLab and split them to an arg array +stdin_raw = ARGF.read; +stdin_args = stdin_raw.split(/\s+/) + +## Set the vars for the commit hashes of current pre-receive event +previous_head_commit = stdin_args[0] +new_head_commit = stdin_args[1] + +## Get all new commits for branch, relative to itself for existing branch, relative to tree for new +diff_git_commits_raw = '' +if (previous_head_commit =~ /0+/) then + ## This isn't perfect as its relative to fork, but should be huge improvement + diff_git_commits_raw = `git rev-list #{new_head_commit} --not --branches=*` +else + diff_git_commits_raw = `git rev-list #{old_head_commit}...#{new_head_commit}` +end +diff_git_commits = diff_git_commits_raw.split(/\n/) + +processed_git_data = [] +diff_git_commits.each do |commit| + processed_git_data.push(process_commit(commit)) +end + +## Create the JSON payload +json_data = { + :repoUrl => '', + :provider => 'gitlab', + :commits => processed_git_data +} +puts MultiJson.dump(json_data).gsub(/(\\n|\\r)/, '') diff --git a/src/main/rb/eca.rb b/src/main/rb/eca.rb index f043bc38721063b18bc3e343ec0b81c677f97266..8685f216c79de86f8f4781e8c5ddc5482730a568 100644 --- a/src/main/rb/eca.rb +++ b/src/main/rb/eca.rb @@ -67,39 +67,18 @@ else project_url = project_json_data['web_url'] end -## required for proper cherry-picking of new commits -default_branch = project_json_data['default_branch'] -if (default_branch.nil? || default_branch.empty?) then - puts "Could not find default branch, assuming new project and bypassing ECA check." - exit 0 +## Get all new commits for branch, relative to itself for existing branch, relative to tree for new +diff_git_commits_raw = '' +if (previous_head_commit =~ /0+/) then + ## This isn't perfect as its relative to fork, but should be huge improvement + diff_git_commits_raw = `git rev-list #{new_head_commit} --not --branches=*` +else + diff_git_commits_raw = `git rev-list #{previous_head_commit}...#{new_head_commit}` end -default_branch_head_ref = "refs/heads/#{default_branch}" - -## Get all commits visible relative to default branch (anything new and not merged in) -## If merging into a non-default branch this will check more than necessary, but not the whole history (which it was previously) -diff_git_commits_raw = `git cherry #{default_branch_head_ref} #{new_head_commit}` diff_git_commits = diff_git_commits_raw.split(/\n/) -counter = 0 -git_commits = [] -previous_head_idx = 0 -diff_git_commits.each do |commit| - if (commit.match(/\+ ?(\S{2,})/)) then - ## gsub will return no data for no match, as + indicates new changes and - indicates tracked changes - cleaned_commit_sha = commit.gsub(/\+ ?(\S{2,})/, '\1') - if (!cleaned_commit_sha.empty?) then - git_commits.push(cleaned_commit_sha) - end - if (cleaned_commit_sha == previous_head_commit) then - found_previous_head = true - previous_head_idx = counter - end - counter = counter + 1 - end -end - processed_git_data = [] -git_commits[previous_head_idx...].each do |commit| +diff_git_commits.each do |commit| processed_git_data.push(process_commit(commit)) end