Skip to content
Snippets Groups Projects

Add some extra empty checks and add helper for checking vars

Merged Martin Lowe requested to merge (removed):malowe/master/repo-url-missing into master
+ 22
12
@@ -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"
@@ -47,19 +51,19 @@ stdin_args = stdin_raw.split(/\s+/)
previous_head_commit = stdin_args[0]
new_head_commit = stdin_args[1]
## Get the project ID from env var, extracting from pattern 'project-###'
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
puts "Cannot retrieve project path, likely a group. Skipping validation"
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
## 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
puts "Repository is a project wiki and not bound by ECA, skipping"
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]
## Get data about project from API
project_response = HTTParty.get("https://gitlab.eclipse.org/api/v4/projects/#{project_id}",
@@ -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
Loading