Auto Merge Dependabot PRs

Automatically clicks the merge button on Dependabot PRs and "Done" button on the notification bar

2025-01-30 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Auto Merge Dependabot PRs
// @namespace    typpi.online
// @version      1.4
// @description  Automatically clicks the merge button on Dependabot PRs and "Done" button on the notification bar
// @author       Nick2bad4u
// @match        https://github.com/*/*/pull/*
// @grant        none
// @icon         https://www.google.com/s2/favicons?sz=64&domain=github.com
// @license      UnLicense
// ==/UserScript==

(function () {
	'use strict';

	let lastCheck = 0;
	const CHECK_INTERVAL = 1000;
	let observer;

	function checkAndMerge() {
		const now = Date.now();
		if (now - lastCheck < CHECK_INTERVAL) return;
		lastCheck = now;

		console.log('checkAndMerge function called');

		const authorElement = document.querySelector('.author');
		if (
			authorElement &&
			/dependabot(\[bot\])?|Nick2bad4u/.test(authorElement.textContent.trim())
		) {
			setTimeout(() => {
				const mergeButton = document.querySelector('.btn.btn-sm');
				if (mergeButton && !mergeButton.disabled) {
					console.log('Merge button is enabled, clicking it');
					mergeButton.click();
					if (observer) observer.disconnect();
					observeDoneButton();
				} else {
					console.log('Merge button is disabled or not found');
				}
			}, 500);
		} else {
			console.log('PR is not created by dependabot');
		}
	}

	function observeDoneButton() {
		console.log('Observing for Done button...');
		const notificationBar = document.querySelector('.js-flash-container'); // Adjust as necessary

		if (!notificationBar) {
			console.log('Notification bar not found');
			return;
		}

		const doneButtonObserver = new MutationObserver(() => {
			const doneButton = document.querySelector(
				'button[aria-label="Done"].btn.btn-sm',
			);
			if (doneButton) {
				console.log('Done button found, clicking it');
				doneButton.click();
				doneButtonObserver.disconnect(); // Stop observing after clicking
			}
		});

		doneButtonObserver.observe(notificationBar, {
			childList: true,
			subtree: true,
		});
	}

	globalThis.addEventListener(
		'load',
		function () {
			console.log('Page loaded');

			const targetNode = document.querySelector('.gh-header-meta');
			if (!targetNode) {
				console.log('Target node for observation not found');
				return;
			}

			observer = new MutationObserver(() => {
				console.log('Relevant DOM mutation detected');
				checkAndMerge();
			});

			observer.observe(targetNode, {
				childList: true,
				subtree: true,
			});
			checkAndMerge();
		},
		false,
	);
})();