From c06ff37beeeda416b1e47365d121e949674cf0a1 Mon Sep 17 00:00:00 2001
From: Fran Garcia-Linares <14157-fjgarlin@users.noreply.drupalcode.org>
Date: Wed, 22 Nov 2023 17:47:35 +0000
Subject: [PATCH] Issue #3189125 by fjgarlin, drumm, xjm: Allow hiding issue
 fork branches

---
 drupalorg/drupalorg.module                |  8 +++++++
 drupalorg/drupalorg.pages.inc             | 29 +++++++++++++++++++++++
 drupalorg/includes/DrupalorgIssueFork.php | 17 +++++++++++++
 3 files changed, 54 insertions(+)

diff --git a/drupalorg/drupalorg.module b/drupalorg/drupalorg.module
index 0a1a6b2f6..44b8a8c27 100644
--- a/drupalorg/drupalorg.module
+++ b/drupalorg/drupalorg.module
@@ -535,6 +535,14 @@ function drupalorg_menu() {
     'file' => 'drupalorg.pages.inc',
   ];
 
+  $items['drupalorg_toggle_branch_visibility/%/%/%'] = [
+    'page callback' => '_drupalorg_toggle_branch_visibility',
+    'page arguments' => [1, 2, 3],
+    'access arguments' => ['edit any project_issue content'],
+    'type' => MENU_CALLBACK,
+    'file' => 'drupalorg.pages.inc',
+  ];
+
   return $items;
 }
 
diff --git a/drupalorg/drupalorg.pages.inc b/drupalorg/drupalorg.pages.inc
index 569b38b73..4723dd0f4 100644
--- a/drupalorg/drupalorg.pages.inc
+++ b/drupalorg/drupalorg.pages.inc
@@ -1342,3 +1342,32 @@ function drupalorg_org_owner_tools(stdClass $node) {
 
   return $content;
 }
+
+/**
+ * Callback function.
+ */
+function _drupalorg_toggle_branch_visibility($gitlab_project_id, $branch_name, $status) {
+  // Check token.
+  if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], 'drupalorg-toggle-branch-' . $gitlab_project_id . '-' . $branch_name . '-' . $status)) {
+    drupal_set_message(t('Invalid token.'), 'error');
+    drupal_access_denied();
+  }
+
+  // Check that the status value is valid.
+  if (!in_array($status, ['hidden', 'active'])) {
+    drupal_set_message(t('Status %status is not a valid option.', ['%status' => $status]), 'error');
+    drupal_goto();
+  }
+
+  // Set the visibility of the branch.  
+  db_merge('drupalorg_issue_fork_branches')
+    ->key([
+      'gitlab_project_id' => $gitlab_project_id,
+      'branch' => $branch_name,
+    ])
+    ->fields(['status' => $status])
+    ->execute();
+
+  drupal_set_message(t('Branch visibility has been toggled.'));
+  drupal_goto('', ['fragment' => 'main']);
+}
diff --git a/drupalorg/includes/DrupalorgIssueFork.php b/drupalorg/includes/DrupalorgIssueFork.php
index 6dca7f620..214695508 100644
--- a/drupalorg/includes/DrupalorgIssueFork.php
+++ b/drupalorg/includes/DrupalorgIssueFork.php
@@ -525,6 +525,23 @@ class DrupalorgIssueFork extends Entity {
           ],
         ];
       }
+
+      if (user_access('edit any project_issue content')) {
+        $toggle_text = ($row->status == 'hidden') ? t('show') : t('hide');
+        $new_status = ($row->status == 'hidden') ? 'active' : 'hidden';
+        $branch_output['operations']['toggle'] = [
+          '#prefix' => '<a class="toggle-branch" title="' . t('@toggle branch for everyone', [
+            '@toggle' => ucfirst($toggle_text),
+          ]) . '" href="' . url('drupalorg_toggle_branch_visibility/' . $this->gitlab_project_id . '/' . $row->branch . '/' . $new_status, [
+            'query' => [
+              'token' => drupal_get_token('drupalorg-toggle-branch-' . $this->gitlab_project_id . '-' . $row->branch . '-' . $new_status),
+              'destination' => current_path(),
+            ]
+          ]) . '">',
+          '#markup' => $toggle_text,
+          '#suffix' => '</a> ',
+        ];
+      }
       $output['branches'][$row->status][] = $branch_output;
     }
 
-- 
GitLab