GitLab Reviewer Shortcut

Adds keyboard shortcut to quickly assign reviewers in GitLab MRs

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         GitLab Reviewer Shortcut
// @namespace    kayw
// @version      1.1
// @license      MIT
// @description  Adds keyboard shortcut to quickly assign reviewers in GitLab MRs
// @match        https://*.gitlab.com/*
// @match        http://*.gitlab.com/*
// @match        https://gitlab.*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Key for storing reviewer name in localStorage
    const REVIEWER_KEY = 'gitlab_default_reviewer';
  
    // Initialize or get saved reviewer - defaults to 'leo' if not set
    const defaultReviewer = localStorage.getItem(REVIEWER_KEY); // example @Jack
  
    // Register keyboard shortcut
    document.addEventListener('keydown', function(event) {
        // Ctrl+R to assign reviewer
        if ((event.ctrlKey || event.metaKey) && !event.shiftKey && event.key === 'r') {
            // Prevent browser refresh
            event.preventDefault();
      
            // Simulate pressing 'r' key to focus GitLab comment input
            const rKeyEvent = new KeyboardEvent('keydown', {
                key: 'r',
                code: 'KeyR',
                keyCode: 82,
                which: 82,
                bubbles: true
            });
            document.dispatchEvent(rKeyEvent);
      
            // Wait for input to get focus
            setTimeout(function() {
                // Find comment input field
                const commentInput = document.querySelector('.js-comment-input, .note-textarea');
        
                if (commentInput) {
                    // Focus the input
                    commentInput.focus();
          
                    // Set text to assign reviewer
                    commentInput.value = `/assign_reviewer ${defaultReviewer}`;
          
                    // Trigger input event for GitLab to recognize the change
                    commentInput.dispatchEvent(new Event('input', { bubbles: true }));
          
                    // Simulate Command+Enter to submit
                    setTimeout(function() {
                        const cmdEnterEvent = new KeyboardEvent('keydown', {
                            key: 'Enter',
                            code: 'Enter',
                            keyCode: 13,
                            which: 13,
                            metaKey: true,
                            bubbles: true
                        });
                        commentInput.dispatchEvent(cmdEnterEvent);
                    }, 100);
                }
            }, 200);
        }
    });
  
    // Add initialization message
    console.log(`GitLab reviewer shortcut initialized. Current reviewer: @${defaultReviewer}`);
    console.log('Use Ctrl+R (or Cmd+R) to assign reviewer');
    console.log('To change the reviewer, set localStorage.setItem("gitlab_default_reviewer", "username")');
})();