Skip to content

Instantly share code, notes, and snippets.

@aditya-gupta-sde
Created January 3, 2025 05:08
Show Gist options
  • Save aditya-gupta-sde/0aaa36a6ffa622c9946bae70a69fa6a7 to your computer and use it in GitHub Desktop.
Save aditya-gupta-sde/0aaa36a6ffa622c9946bae70a69fa6a7 to your computer and use it in GitHub Desktop.
This Tampermonkey script adds a custom "Run Tests" button to GitHub Pull Request (PR) pages. When clicked, it automatically adds a comment saying "run tests" to the PR. This is especially useful for teams using comment-based triggers for CI/CD workflows or testing pipelines.
// ==UserScript==
// @name Add "Run Tests" Button to GitHub PRs
// @namespace https://github.com/
// @version 1.0
// @description Adds a button to GitHub PRs to comment "run tests"
// @author Your Name
// @match https://github.com/*/pull/*
// @grant none
// @downloadURL https://gist.githubusercontent.com/<your-username>/<gist-id>/raw/run-tests-button.user.js
// @updateURL https://gist.githubusercontent.com/<your-username>/<gist-id>/raw/run-tests-button.user.js
// ==/UserScript==
(function() {
'use strict';
// Check if on a PR page
if (!document.querySelector('div[aria-label="Pull request"]')) return;
// Create the button
const runTestsButton = document.createElement('button');
runTestsButton.innerText = 'Run Tests';
runTestsButton.style = `
background-color: #2ea44f;
color: white;
border: none;
border-radius: 6px;
padding: 5px 10px;
cursor: pointer;
font-size: 14px;
margin-left: 10px;
`;
// Append button to the toolbar
const toolbar = document.querySelector('.gh-header-actions');
if (toolbar) {
toolbar.appendChild(runTestsButton);
}
// Add event listener to the button
runTestsButton.addEventListener('click', async () => {
const commentBox = document.querySelector('#new_comment_field');
const submitButton = document.querySelector('button[name="comment_and_close"]') || document.querySelector('button[data-edit-text="Comment"]');
if (commentBox && submitButton) {
commentBox.value = 'run tests'; // Add the comment
submitButton.click(); // Submit the comment
alert('Comment added: "run tests"');
} else {
alert('Could not find the comment box or submit button.');
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment