From 18658076038ea5393bb08b20caf503d39c567748 Mon Sep 17 00:00:00 2001 From: Liam Morland Date: Mon, 16 Nov 2020 12:23:15 -0500 Subject: [PATCH 1/4] Coding standards --- devops/uw_devops.db.inc | 4 +++- devops/uw_devops.inc | 6 +++++- uw_wcms_tools.gitlab.inc | 7 ++++--- uw_wcms_tools.modules.inc | 13 +++++++++++-- uw_wcms_tools.rt.inc | 6 ++++-- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/devops/uw_devops.db.inc b/devops/uw_devops.db.inc index 3568c1a..f5488f3 100644 --- a/devops/uw_devops.db.inc +++ b/devops/uw_devops.db.inc @@ -30,7 +30,9 @@ function url_path_to_db_name($pool, $url_path, $drupal_major_version_number) { } $file_path = url_path_to_file_path($url_path); - return strtolower(substr(substr($pool, 0, 2) . $drupal_major_version_number . '_' . str_replace(array('-', '.'), '', $file_path), 0, 60)); + $database_prefix = substr($pool, 0, 2) . $drupal_major_version_number; + $file_path = str_replace(['-', '.'], '', $file_path); + return strtolower(substr($database_prefix . '_' . $file_path, 0, 60)); } /** diff --git a/devops/uw_devops.inc b/devops/uw_devops.inc index 7ad418a..aaaafd5 100644 --- a/devops/uw_devops.inc +++ b/devops/uw_devops.inc @@ -900,7 +900,11 @@ function get_site_theme($pool, $url_path) { function get_site_is_decoupled($pool, $url_path) { $theme = get_site_theme($pool, $url_path); if ($theme) { - return in_array($theme, array('uw_custom_frontend', 'uw_conference_frontend'), TRUE); + $decoupled_themes = [ + 'uw_custom_frontend', + 'uw_conference_frontend', + ]; + return in_array($theme, $decoupled_themes, TRUE); } } diff --git a/uw_wcms_tools.gitlab.inc b/uw_wcms_tools.gitlab.inc index 3138998..efc694d 100644 --- a/uw_wcms_tools.gitlab.inc +++ b/uw_wcms_tools.gitlab.inc @@ -141,7 +141,8 @@ function uw_wcms_tools_get_projects($namespaces = NULL) { // Limit to 100 pages. while ($page_data && $page < 100) { $page++; - $page_data = uw_wcms_tools_query_gitlab_api('projects', array('per_page' => 100, 'page' => $page)); + $query = ['per_page' => 100, 'page' => $page]; + $page_data = uw_wcms_tools_query_gitlab_api('projects', $query); $page_data = $page_data['body']; if (is_array($page_data)) { foreach ($page_data as $_) { @@ -429,8 +430,6 @@ function uw_wcms_tools_gitlab_project_list_hooks(int $id): array { /** * Create a GitLab project with desired settings. * - * @see https://git.uwaterloo.ca/help/api/projects.md#create-project - * * @param string $group * The group in which to create the project. * @param string $path @@ -438,6 +437,8 @@ function uw_wcms_tools_gitlab_project_list_hooks(int $id): array { * * @return array * The API reponse for the project creation. + * + * @see https://git.uwaterloo.ca/help/api/projects.md#create-project */ function uw_wcms_tools_gitlab_project_create(string $group, string $path): array { $group = uw_wcms_tools_get_group($group); diff --git a/uw_wcms_tools.modules.inc b/uw_wcms_tools.modules.inc index c26c9b6..f0a0829 100644 --- a/uw_wcms_tools.modules.inc +++ b/uw_wcms_tools.modules.inc @@ -170,13 +170,22 @@ function uw_wcms_tools_new_tag_confirm_submit(array $form, array &$form_state) { if ($last_commit->id === $form['commit_id']['#value']) { $result = uw_wcms_tools_create_tag($project->id, $last_commit->id, $form['tag_name']['#value']); if ($result['http_status'] === 201) { - drupal_set_message(t('Default branch of project %project tagged %tag_name.', array('%project' => $project->name_with_namespace, '%tag_name' => $form['tag_name']['#value'])), 'status'); + $args = [ + '%project' => $project->name_with_namespace, + '%tag_name' => $form['tag_name']['#value'], + ]; + drupal_set_message(t('Default branch of project %project tagged %tag_name.', $args), 'status'); } else { if (function_exists('dpm')) { + // @codingStandardsIgnoreLine dpm($result, 'Error object', 'error'); } - drupal_set_message(t('Failed to create tag %tag_name in %project.', array('%project' => $project->name_with_namespace, '%tag_name' => $form['tag_name']['#value'])), 'error'); + $args = [ + '%project' => $project->name_with_namespace, + '%tag_name' => $form['tag_name']['#value'], + ]; + drupal_set_message(t('Failed to create tag %tag_name in %project.', $args), 'error'); } } else { diff --git a/uw_wcms_tools.rt.inc b/uw_wcms_tools.rt.inc index 8c1b3d0..dd16fec 100644 --- a/uw_wcms_tools.rt.inc +++ b/uw_wcms_tools.rt.inc @@ -75,8 +75,10 @@ function uw_wcms_tools_rt_api_ticket_create(array $content) { */ function uw_wcms_tools_rt_api_ticket_view($ticket) { $rt = uw_wcms_tools_rt_api_get_connection(); - dsm($rt->getTicketProperties((int) $ticket), 'Ticket'); - dsm(uw_wcms_tools_rt_api_get_ticket_history_node((int) $ticket), 'First history node'); + // @codingStandardsIgnoreLine + dpm($rt->getTicketProperties((int) $ticket), 'Ticket'); + // @codingStandardsIgnoreLine + dpm(uw_wcms_tools_rt_api_get_ticket_history_node((int) $ticket), 'First history node'); return ''; } -- GitLab From aa0e022c48d8d31417785808069e45436ebf14f7 Mon Sep 17 00:00:00 2001 From: Liam Morland Date: Mon, 16 Nov 2020 11:15:11 -0500 Subject: [PATCH 2/4] ISTWCMS-4190: Enable caching in uw_wcms_tools_get_project() --- uw_wcms_tools.gitlab.inc | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/uw_wcms_tools.gitlab.inc b/uw_wcms_tools.gitlab.inc index efc694d..d0711aa 100644 --- a/uw_wcms_tools.gitlab.inc +++ b/uw_wcms_tools.gitlab.inc @@ -165,7 +165,9 @@ function uw_wcms_tools_get_projects($namespaces = NULL) { /** * Loader function for a project object, loading from the API Gitlab API. * - * This can load either by project ID or the namespace and name. + * This can load either by project ID or the namespace and name. If the project + * is found, it is cached and the same object returned on subsequent calls to + * load the same project. * * @param int|string $id_or_namespace * The ID of the project to load or its namespace if loading by name. @@ -182,8 +184,21 @@ function uw_wcms_tools_get_project($id_or_namespace, $project_name = NULL) { else { $query = (int) $id_or_namespace; } + + static $cache = []; + if (isset($cache[$query])) { + return $cache[$query]; + } + $project = uw_wcms_tools_query_gitlab_api('projects/' . $query); - return ($project['http_status'] === 200) ? $project['body'] : NULL; + + if ($project['http_status'] === 200) { + $project = $project['body']; + $cache[$project->id] = $project; + $cache[urlencode($project->path_with_namespace)] = &$cache[$project->id]; + return $project; + } + return NULL; } /** -- GitLab From e56079d1f34177af091e725167d52d181ce4d598 Mon Sep 17 00:00:00 2001 From: Liam Morland Date: Mon, 16 Nov 2020 11:19:03 -0500 Subject: [PATCH 3/4] ISTWCMS-4190: Enable CI for certain groups in _uw_wcms_tools_gitlab_project_features() Add $group param. --- uw_wcms_tools.gitlab.inc | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/uw_wcms_tools.gitlab.inc b/uw_wcms_tools.gitlab.inc index d0711aa..0763481 100644 --- a/uw_wcms_tools.gitlab.inc +++ b/uw_wcms_tools.gitlab.inc @@ -402,11 +402,14 @@ function uw_wcms_tools_gitlab_project_update_features(int $id): array { /** * Return an array of GitLab project settings. * + * @param string $group + * The group to return settings for. + * * @return array * GitLab project settings. */ -function _uw_wcms_tools_gitlab_project_features(): array { - return [ +function _uw_wcms_tools_gitlab_project_features(string $group = NULL): array { + $settings = [ // Project features. 'issues_access_level' => 'disabled', 'repository_access_level' => 'enabled', @@ -427,6 +430,22 @@ function _uw_wcms_tools_gitlab_project_features(): array { 'only_allow_merge_if_all_discussions_are_resolved' => FALSE, 'suggestion_commit_message' => NULL, ]; + + // GitLab groups for which to enable CI. + $ci_groups = [ + 'responsive-web-design', + 'wcms-architecture', + 'wcms-decoupled', + 'wcms-open-atrium', + 'wcms-openscholar', + 'wcms', + ]; + if (in_array($group, $ci_groups, TRUE)) { + $settings['builds_access_level'] = 'private'; + $settings['ci_config_path'] = 'wcms-ci.yml@wcms-automation/wcms-cicd'; + } + + return $settings; } /** -- GitLab From 7ead4c0a673ccb2535159a47bcd2c5ff646f72f9 Mon Sep 17 00:00:00 2001 From: Liam Morland Date: Mon, 16 Nov 2020 11:23:46 -0500 Subject: [PATCH 4/4] ISTWCMS-4190: Enable CI for GitLab projects in certain groups Use group param in calls to _uw_wcms_tools_gitlab_project_features(). --- uw_wcms_tools.gitlab.inc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/uw_wcms_tools.gitlab.inc b/uw_wcms_tools.gitlab.inc index 0763481..5e49c0c 100644 --- a/uw_wcms_tools.gitlab.inc +++ b/uw_wcms_tools.gitlab.inc @@ -395,7 +395,8 @@ function uw_wcms_tools_gitlab_project_ms_teams_integration_update(int $id): arra * The API response. */ function uw_wcms_tools_gitlab_project_update_features(int $id): array { - $update = ['id' => $id] + _uw_wcms_tools_gitlab_project_features(); + $project = uw_wcms_tools_get_project($id); + $update = ['id' => $id] + _uw_wcms_tools_gitlab_project_features($project->namespace->path); return uw_wcms_tools_query_gitlab_api('projects/' . $id, [], 'PUT', $update); } @@ -491,7 +492,7 @@ function uw_wcms_tools_gitlab_project_create(string $group, string $path): array 'visibility' => 'public', 'container_registry_enabled' => FALSE, ]; - $project += _uw_wcms_tools_gitlab_project_features(); + $project += _uw_wcms_tools_gitlab_project_features($group->path); $r = uw_wcms_tools_query_gitlab_api('projects', [], 'POST', $project); if ($r['http_status'] === 201) { -- GitLab