From 2b52cab27ee72fae1b3514aa6aca446d0b1e9070 Mon Sep 17 00:00:00 2001 From: Martin Lowe <martin.lowe@eclipse-foundation.org> Date: Thu, 21 Apr 2022 09:57:52 -0400 Subject: [PATCH 1/4] Add some extra empty checks and add helper for checking vars This should help to address issues with group wikis being unable to commit resources. --- src/main/rb/eca.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/rb/eca.rb b/src/main/rb/eca.rb index 1d3d20d0..b1e4b75c 100644 --- a/src/main/rb/eca.rb +++ b/src/main/rb/eca.rb @@ -26,6 +26,10 @@ def process_commit(sha) } end +def nil_or_empty(o) + return o.nil? || o.empty? +end + ## read in the access token from secret file if (!File.file?("/etc/gitlab/eca-access-token")) puts "GL-HOOK-ERR: Internal server error, please contact administrator. Error, secret not found" @@ -51,12 +55,12 @@ new_head_commit = stdin_args[1] project_id = ENV['GL_REPOSITORY'][8..-1] project_path = ENV['GL_PROJECT_PATH'] ## When pushing group wikis, project_path may be empty -if (project_path.nil?) then +if (nil_or_empty(project_path)) then puts "Cannot retrieve project path, likely a group. Skipping validation" exit 0 end ## Check if current repo is a project wiki (no project ID and ends in .wiki) -if (project_id.nil? && project_path =~ WIKI_REGEX_MATCH) then +if (nil_or_empty(project_id) && project_path =~ WIKI_REGEX_MATCH) then puts "Repository is a project wiki and not bound by ECA, skipping" exit 0 end @@ -68,18 +72,24 @@ project_response = HTTParty.get("https://gitlab.eclipse.org/api/v4/projects/#{pr }) ## Format data to be able to easily read and process it project_json_data = MultiJson.load(project_response.body) -if (project_json_data.nil? || project_json_data.class.name == 'Array') then +if (nil_or_empty(project_json_data) || project_json_data.class.name == 'Array') then puts "Couldn't load project data, assumed non-tracked project and skipping validation." exit 0 end ## Get the web URL, checking if project is a fork to get original project URL -if (!project_json_data['forked_from_project'].nil?) then +if (!nil_or_empty(project_json_data['forked_from_project'])) then puts "Non-Eclipse project repository detected: ECA validation will be skipped.\n\nNote that any issues with sign off or committer access will be flagged upon merging into the main project repository." exit 0 else project_url = project_json_data['web_url'] end +## This can happen for group wikis by inference from some production-only issues +if (nil_or_empty(project_url)) then + puts "Could not determine a web URL for project, likely not a fully-qualified project, skipping" + exit 0 +end + ## 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 -- GitLab From 52983fec7f5df55d3a3fd294a4c53e363206f489 Mon Sep 17 00:00:00 2001 From: Martin Lowe <martin.lowe@eclipse-foundation.org> Date: Thu, 21 Apr 2022 10:10:31 -0400 Subject: [PATCH 2/4] Add fix to check GL envvars more closely --- src/main/rb/eca.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/rb/eca.rb b/src/main/rb/eca.rb index b1e4b75c..35046070 100644 --- a/src/main/rb/eca.rb +++ b/src/main/rb/eca.rb @@ -51,8 +51,13 @@ stdin_args = stdin_raw.split(/\s+/) previous_head_commit = stdin_args[0] new_head_commit = stdin_args[1] +gl_repo = ENV['GL_REPOSITORY'] +if (nil_or_empty(gl_repo)) then + puts "No Gitlab repository set, likely dealing with non-repo commit, skipping" + exit 0 +end ## Get the project ID from env var, extracting from pattern 'project-###' -project_id = ENV['GL_REPOSITORY'][8..-1] +project_id = gl_repo[8..-1] project_path = ENV['GL_PROJECT_PATH'] ## When pushing group wikis, project_path may be empty if (nil_or_empty(project_path)) then @@ -60,7 +65,7 @@ if (nil_or_empty(project_path)) then exit 0 end ## Check if current repo is a project wiki (no project ID and ends in .wiki) -if (nil_or_empty(project_id) && project_path =~ WIKI_REGEX_MATCH) then +if (project_path =~ WIKI_REGEX_MATCH) then puts "Repository is a project wiki and not bound by ECA, skipping" exit 0 end -- GitLab From 79fe9a64b5f7335ffa0d18a1ad0c71f4149db898 Mon Sep 17 00:00:00 2001 From: Martin Lowe <martin.lowe@eclipse-foundation.org> Date: Thu, 21 Apr 2022 10:35:36 -0400 Subject: [PATCH 3/4] Add check to make sure GL_REPOSITORY starts with 'project-' --- src/main/rb/eca.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/rb/eca.rb b/src/main/rb/eca.rb index 35046070..bb10330f 100644 --- a/src/main/rb/eca.rb +++ b/src/main/rb/eca.rb @@ -55,6 +55,9 @@ gl_repo = ENV['GL_REPOSITORY'] if (nil_or_empty(gl_repo)) then puts "No Gitlab repository set, likely dealing with non-repo commit, skipping" exit 0 +elsif (gl_repo !~ /^project-/) then + puts "GL_REPOSITORY envvar is improperly set does not match expected format, cannot validate" + exit 1 end ## Get the project ID from env var, extracting from pattern 'project-###' project_id = gl_repo[8..-1] -- GitLab From 88c61bd41e4aae5ee16b227bebc91d619f51bf8e Mon Sep 17 00:00:00 2001 From: Martin Lowe <martin.lowe@eclipse-foundation.org> Date: Thu, 21 Apr 2022 11:06:54 -0400 Subject: [PATCH 4/4] Update envvar check to catch wiki commit earlier --- src/main/rb/eca.rb | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/main/rb/eca.rb b/src/main/rb/eca.rb index bb10330f..d04f3cb6 100644 --- a/src/main/rb/eca.rb +++ b/src/main/rb/eca.rb @@ -55,23 +55,15 @@ gl_repo = ENV['GL_REPOSITORY'] if (nil_or_empty(gl_repo)) then puts "No Gitlab repository set, likely dealing with non-repo commit, skipping" exit 0 +elsif (gl_repo =~ /^wiki-/) then + puts "Commit is associated with a wiki, and does not need to be validated. Skipping." + exit 0 elsif (gl_repo !~ /^project-/) then puts "GL_REPOSITORY envvar is improperly set does not match expected format, cannot validate" exit 1 end ## Get the project ID from env var, extracting from pattern 'project-###' project_id = gl_repo[8..-1] -project_path = ENV['GL_PROJECT_PATH'] -## When pushing group wikis, project_path may be empty -if (nil_or_empty(project_path)) then - puts "Cannot retrieve project path, likely a group. Skipping validation" - exit 0 -end -## Check if current repo is a project wiki (no project ID and ends in .wiki) -if (project_path =~ WIKI_REGEX_MATCH) then - puts "Repository is a project wiki and not bound by ECA, skipping" - exit 0 -end ## Get data about project from API project_response = HTTParty.get("https://gitlab.eclipse.org/api/v4/projects/#{project_id}", -- GitLab