From 1e17c4e4d8def61fdc67a5402c97db61495e3e6e Mon Sep 17 00:00:00 2001
From: Martin Lowe <martin.lowe@eclipse-foundation.org>
Date: Fri, 4 Nov 2022 13:16:45 -0400
Subject: [PATCH] Iss #86 - Add support for enabling strictmode on GL ECA
 checks

Add ability to define root groups that will be validated against ECA
using strict mode which forces the commits to be validated as if it were
a project
---
 src/main/rb/eca.rb | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/main/rb/eca.rb b/src/main/rb/eca.rb
index b39e5c57..f1a45ab4 100644
--- a/src/main/rb/eca.rb
+++ b/src/main/rb/eca.rb
@@ -6,6 +6,7 @@ require 'httparty'
 require 'multi_json'
 WIKI_REGEX_MATCH = /.*\.wiki$/
 HOST_URL='https://gitlab.eclipse.org'
+API_URL='https://api.eclipse.org'
 # this should be removed as soon as Oniro is onside again
 ALLOW_LIST_PROJECTS = ['/eclipse/oniro-core/meta-ts',
 '/eclipse/oniro-core/meta-ledge-sesure',
@@ -21,6 +22,7 @@ ALLOW_LIST_PROJECTS = ['/eclipse/oniro-core/meta-ts',
 '/eclipse/oniro-core/meta-seco-intel',
 '/eclipse/oniro-core/meta-zephyr'
 ]
+STRICT_MODE_ROOT_GROUPS = ['eclipse-research-labs/']
 
 ## Track whether forked project for error reporting (failing vs non-failing)
 is_forked_project = false
@@ -45,6 +47,11 @@ def process_commit(sha)
   }
 end
 
+## Take in a project namespace, and see if it starts with any of the targeted root groups.
+def use_strict_mode(project_namespace)
+  return STRICT_MODE_ROOT_GROUPS.any? { |root_group| project_namespace.start_with?(root_group) }
+end
+
 def nil_or_empty(o)
   return o.nil? || o.empty?
 end
@@ -134,14 +141,16 @@ diff_git_commits.each do |commit|
   processed_git_data.push(process_commit(commit))
 end
 
+is_strict_enforced = use_strict_mode(project_json_data['path_with_namespace'])
 ## Create the JSON payload
 json_data = {
   :repoUrl => project_url,
   :provider => 'gitlab',
-  :commits => processed_git_data
+  :commits => processed_git_data,
+  :strictMode => is_strict_enforced
 }
 ## Generate request (use gsub to scrub any lingering \n constants)
-response = HTTParty.post("https://api.eclipse.org/git/eca", :body => MultiJson.dump(json_data).gsub(/(\\n|\\r)/, ''), 
+response = HTTParty.post("#{API_URL}/git/eca", :body => MultiJson.dump(json_data).gsub(/(\\n|\\r)/, ''), 
   :headers => { 
     'Content-Type' => 'application/json',
     'charset' => 'utf-8'
@@ -205,7 +214,7 @@ else
     puts "More information is available at https://api.eclipse.org/git/eca/status/#{parsed_response['fingerprint']}/ui"
   end
   ## after parsing 
-  if (!parsed_response.nil? && parsed_response['trackedProject'] == false) then
+  if (!parsed_response.nil? && parsed_response['trackedProject'] == false && !is_strict_enforced) then
     if (contained_warnings_errors) then
       puts "Errors or warnings were encountered while validating sign-off in current request for non-project repository.\n\nValidation is currently not required for non-project repositories, continuing."
     end
@@ -213,7 +222,7 @@ else
   end
 end
 ## If error, exit as status 1
-if (response.code == 403 && is_forked_project) then
+if (response.code == 403 && is_forked_project && !is_strict_enforced) then
   puts "Errors detected, but commits will be allowed for forked repository. Commit errors reported in this push will block merge into origin repository until resolved."
 elsif (response.code == 403) then
   exit 1
-- 
GitLab