From 44bcb12cb09336899d910de4861726213d0d78e4 Mon Sep 17 00:00:00 2001 From: Martin Lowe <martin.lowe@eclipse-foundation.org> Date: Wed, 6 Apr 2022 14:25:00 -0400 Subject: [PATCH 1/3] Add hotfix for oniro to replace known bad email --- src/main/rb/eca.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/rb/eca.rb b/src/main/rb/eca.rb index f043bc38..f59257ec 100644 --- a/src/main/rb/eca.rb +++ b/src/main/rb/eca.rb @@ -12,11 +12,11 @@ def process_commit(sha) return { :author => { :name => `git show -s --format='%an' #{sha}`.force_encoding("utf-8"), - :mail => `git show -s --format='%ae' #{sha}`.force_encoding("utf-8"), + :mail => `git show -s --format='%ae' #{sha}`.force_encoding("utf-8").gsub(/nawab\.ahmad\.reshi\@huawei\.com/,'nawab.ahmad.reshi@huawei-partners.com'), }, :committer => { :name => `git show -s --format='%cn' #{sha}`.force_encoding("utf-8"), - :mail => `git show -s --format='%ce' #{sha}`.force_encoding("utf-8"), + :mail => `git show -s --format='%ce' #{sha}`.force_encoding("utf-8").gsub(/nawab\.ahmad\.reshi\@huawei\.com/,'nawab.ahmad.reshi@huawei-partners.com'), }, :body => `git show -s --format='%B' #{sha}`.force_encoding("utf-8"), :subject => `git show -s --format='%s' #{sha}`.force_encoding("utf-8"), -- GitLab From 332b38869469a2edbb4071c98e4d372ac8f656d3 Mon Sep 17 00:00:00 2001 From: Martin Lowe <martin.lowe@eclipse-foundation.org> Date: Fri, 8 Apr 2022 13:26:13 -0400 Subject: [PATCH 2/3] Update git diff command to use rev-list instead of cherry --- src/main/rb/debug.rb | 59 ++++++++++++++++++++++++++++++++++++++++++++ src/main/rb/eca.rb | 41 ++++++++---------------------- 2 files changed, 69 insertions(+), 31 deletions(-) create mode 100644 src/main/rb/debug.rb diff --git a/src/main/rb/debug.rb b/src/main/rb/debug.rb new file mode 100644 index 00000000..fa20b3d0 --- /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 f59257ec..0a5f2243 100644 --- a/src/main/rb/eca.rb +++ b/src/main/rb/eca.rb @@ -12,11 +12,11 @@ def process_commit(sha) return { :author => { :name => `git show -s --format='%an' #{sha}`.force_encoding("utf-8"), - :mail => `git show -s --format='%ae' #{sha}`.force_encoding("utf-8").gsub(/nawab\.ahmad\.reshi\@huawei\.com/,'nawab.ahmad.reshi@huawei-partners.com'), + :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").gsub(/nawab\.ahmad\.reshi\@huawei\.com/,'nawab.ahmad.reshi@huawei-partners.com'), + :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"), @@ -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 #{old_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 -- GitLab From 544d9c187793b979869262a96bc4a2d846266e83 Mon Sep 17 00:00:00 2001 From: Martin Lowe <martin.lowe@eclipse-foundation.org> Date: Tue, 12 Apr 2022 08:40:12 -0400 Subject: [PATCH 3/3] Fix bad variable used in one of the comparisons --- src/main/rb/eca.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/rb/eca.rb b/src/main/rb/eca.rb index 0a5f2243..8685f216 100644 --- a/src/main/rb/eca.rb +++ b/src/main/rb/eca.rb @@ -73,7 +73,7 @@ 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}` + diff_git_commits_raw = `git rev-list #{previous_head_commit}...#{new_head_commit}` end diff_git_commits = diff_git_commits_raw.split(/\n/) -- GitLab