-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
71 lines (58 loc) · 2.63 KB
/
content.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
const isPullRequestPage = () => /https:\/\/github\.com\/.+\/.+\/pull\/\d+/.test(window.location.href);
const rerunFailedChecks = failedChecks => {
chrome.storage.local.get('githubToken', data => {
const token = data.githubToken;
if (!token) {
alert('GitHub token not set. Please set it in the extension popup.');
return;
}
failedChecks.forEach(check => {
const detailLink = new URL(Array.from(check.querySelectorAll('a')).find(link => link.innerText === 'Details').href);
const pathParts = detailLink.pathname.split('/');
const runIdIndex = pathParts.indexOf('runs');
const org = pathParts[1];
const repo = pathParts[2];
const runId = pathParts[runIdIndex + 1];
fetch(`https://api.github.com/repos/${org}/${repo}/actions/runs/${runId}/rerun-failed-jobs`, {
method: 'POST',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': `Bearer ${token}`,
},
});
});
});
};
const checkFailedChecks = button => {
const failedChecks = Array.from(document.querySelectorAll('.merge-status-item'))
.filter(item => item.querySelector('.label.Label--primary'))
.filter(item => item.querySelector('.color-fg-danger'))
.filter(item => item.querySelector('.label.Label--primary').innerText === 'Required');
if (failedChecks.length > 0) {
// Enable the button if there are failed checks
button.disabled = false;
// Add click event to rerun the checks
button.addEventListener('click', () => rerunFailedChecks(failedChecks));
}
};
const addReRunButton = () => {
const mergeButton = document.querySelector('.merge-message .select-menu');
if (mergeButton && !document.getElementById('rerun-failed-checks')) {
// Create a new button
const reRunButton = document.createElement('button');
reRunButton.id = 'rerun-failed-checks';
reRunButton.textContent = 'Rerun Failed Checks';
reRunButton.classList.add('btn');
reRunButton.style = 'margin-left: 10px;'
reRunButton.disabled = true; // Initially disabled
// Add the button to the DOM
mergeButton.parentElement.insertBefore(reRunButton, mergeButton.nextSibling);
// Check for failed checks and enable/disable the button accordingly
checkFailedChecks(reRunButton);
}
};
new MutationObserver(() => {
if (isPullRequestPage()) {
addReRunButton();
}
}).observe(document.body, { childList: true, subtree: true });