-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-pr-approvals-gm.js
112 lines (88 loc) · 2.93 KB
/
github-pr-approvals-gm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// ==UserScript==
// @name GitHub PR Approvals
// @namespace http://mattstow.com
// @version 0.2.1
// @description Require =Approved= before you can merge GitHub PRs
// @author Matt Stow (@stowball)
// @match https://github.com/*
// @grant none
// @require https://code.jquery.com/jquery-2.2.3.min.js
// @require https://greasyfork.org/scripts/1003-wait-for-key-elements/code/Wait%20for%20key%20elements.js?version=49342
// ==/UserScript==
/* jshint -W097 */
'use strict';
var requiredApprovals = 1;
var approvalString = '=Approved=';
/* DO NOT EDIT BELOW THIS LINE */
var css = '<style>z {} svg.octicon { pointer-events: none; }';
var style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(css));
document.getElementsByTagName('head')[0].appendChild(style);
function disableButton() {
var button = document.querySelector('.js-merge-branch-action');
if (button) {
button.disabled = true;
}
}
function enableButton() {
var button = document.querySelector('.js-merge-branch-action');
if (button) {
button.disabled = false;
}
}
function findCommentWrapper(el) {
while (el.parentNode) {
el = el.parentNode;
if (el.classList.contains('timeline-comment-wrapper')) {
return el;
}
}
return null;
}
function findCommitSiblings(el) {
while (el.nextElementSibling) {
el = el.nextElementSibling;
if (el.classList.contains('discussion-commits')) {
return el;
}
}
return null;
}
function lookForApproval(d) {
var delay = d || 1000;
setTimeout(function () {
var comments = document.querySelectorAll('.comment-body p');
var foundApprovals = 0;
for (var i = comments.length - 1; i >= 0; i--) {
if (comments[i].textContent === approvalString) {
var parent = findCommentWrapper(comments[i]);
if (parent) {
if (!findCommitSiblings(parent)) {
foundApprovals++;
if (foundApprovals === requiredApprovals) {
enableButton();
break;
}
}
}
}
}
if (foundApprovals < requiredApprovals) {
disableButton();
}
}, delay);
}
function checkListener(e) {
if (
(e.type === 'click' && (e.target.classList.contains('btn-primary') || e.target.classList.contains('delete-button'))) ||
(e.type === 'keydown' && (e.ctrlKey || e.metaKey) && e.target.classList.contains('js-comment-field'))
) {
lookForApproval();
}
}
waitForKeyElements('.js-merge-branch-action', function () {
document.addEventListener('click', checkListener, false);
document.addEventListener('keydown', checkListener, false);
lookForApproval();
});