-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpull.user.js
More file actions
273 lines (240 loc) · 10.8 KB
/
pull.user.js
File metadata and controls
273 lines (240 loc) · 10.8 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// ==UserScript==
// @name Pull Helper Script
// @namespace https://github.com/HuongNV13
// @version 1.3
// @description Moodle Pull Helper Script for Integrators
// @author Huong Nguyen
// @homepage https://github.com/HuongNV13/moodle-userscripts
// @downloadURL https://github.com/HuongNV13/moodle-userscripts/raw/main/pull.user.js
// @updateURL https://github.com/HuongNV13/moodle-userscripts/raw/main/pull.user.js
// @match https://moodle.atlassian.net/browse/*
// @match https://moodle.atlassian.net/issues/*
// @require https://code.jquery.com/jquery-3.6.0.min.js
// @grant none
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
const userScript = function() {
const selectors = {
pullRepo: 'div[data-testid*="customfield_10244"] a',
target: 'div[data-testid="issue.views.issue-base.context.status-and-approvals-wrapper.status-and-approval"]',
};
const branches = [
{
shortname: 'Main',
customField: '10221',
branchname: 'main',
},
{
shortname: '5.1',
customField: '11075',
branchname: 'MOODLE_501_STABLE',
},
{
shortname: '5.0',
customField: '10218',
branchname: 'MOODLE_500_STABLE',
},
{
shortname: '4.5',
customField: '10230',
branchname: 'MOODLE_405_STABLE',
},
{
shortname: '4.4',
customField: '10216',
branchname: 'MOODLE_404_STABLE',
},
{
shortname: '4.1',
customField: '10217',
branchname: 'MOODLE_401_STABLE',
},
];
const showMessage = (type, message) => {
// Try AJS flag first.
if (typeof AJS !== 'undefined' && AJS.flag) {
AJS.flag({
type: type,
body: message,
close: 'auto'
});
} else {
// Fallback to custom notification.
const notification = $(`
<div class="aui-message aui-message-${type}" style="position: fixed; top: 20px; right: 20px; z-index: 9999; max-width: 300px;">
<p class="title">
<strong>${type === 'success' ? 'Success!' : 'Error!'}</strong>
</p>
<p>${message}</p>
</div>
`);
$('body').append(notification);
// Auto-remove after 3 seconds.
setTimeout(() => {
notification.fadeOut(() => notification.remove());
}, 3000);
}
};
const handleMergeCommand = (branch, pullBranch) => {
console.log('Merge command for', branch.branchname, 'with pull branch:', pullBranch);
// Get the Github repository URL.
let gitRepo = $(selectors.pullRepo).attr('href');
if (!gitRepo || !gitRepo.length) {
console.error('Git repository URL not found');
return;
}
// Ensure proper git protocol.
gitRepo = gitRepo.replace('git://github.com', 'https://github.com');
// Generate git command.
const gitCommand = `git checkout ${branch.branchname} && git reset --hard origin/${branch.branchname} && git fetch ${gitRepo} ${pullBranch} && git merge --no-ff --no-edit FETCH_HEAD`;
console.log('Generated git command:', gitCommand);
// Copy to clipboard.
navigator.clipboard.writeText(gitCommand).then(() => {
console.log('Git command copied to clipboard');
showMessage('success', `Git merge command for ${branch.shortname} has been copied to clipboard.`);
}).catch(err => {
console.error('Failed to copy to clipboard:', err);
showMessage('error', 'Failed to copy git merge command to clipboard.');
});
};
const handlePushCommand = (branchesToPush) => {
console.log('Push command for branches:', branchesToPush);
// Generate git push command.
const gitPushCommand = `git push origin ${branchesToPush.join(' ')}`;
console.log('Generated git push command:', gitPushCommand);
// Copy to clipboard.
navigator.clipboard.writeText(gitPushCommand).then(() => {
console.log('Git push command copied to clipboard');
showMessage('success', 'Git push command has been copied to clipboard.');
}).catch(err => {
console.error('Failed to copy to clipboard:', err);
showMessage('error', 'Failed to copy git push command to clipboard.');
});
};
const updateView = () => {
// Get the Github repository URL.
let gitRepo = $(selectors.pullRepo).attr('href');
if (!gitRepo || !gitRepo.length) {
return;
}
// https://github.blog/2021-09-01-improving-git-protocol-security-github/#no-more-unauthenticated-git.
gitRepo = gitRepo.replace('git://github.com', 'https://github.com');
const githubRepoName = gitRepo.replace('moodle.git', 'moodle');
// Store pullBranch data for each branch
const branchData = [];
const availableBranches = [];
// Create content for branches
let branchContent = '<div style="padding-left: 8px; padding-right: 8px;"><h2 style="font-size: 1em;">Integration</h2>';
branches.forEach((branch, index) => {
// Get the pullBranch for this branch.
const pullBranchSelector = `div[data-testid="issue.views.field.single-line-text.read-view.customfield_${branch.customField}"]`;
const pullBranchNode = $(pullBranchSelector);
if (pullBranchNode.length) {
const remoteBranchName = pullBranchNode.text().trim();
// Store branch data with pullBranch.
branchData[index] = { ...branch, remoteBranchName };
availableBranches.push(branch.branchname);
console.log(`Pull branch for ${branch.shortname}:`, remoteBranchName);
branchContent += `
<div style="display: flex; margin-bottom: 4px; align-items: center;">
<div style="flex: 1; font-weight: bold;">${branch.shortname}</div>
<div style="flex: 1; display: flex; justify-content: center;">
<a href="${githubRepoName}/actions?query=branch%3A${remoteBranchName}">
<img src="${githubRepoName}/actions/workflows/push.yml/badge.svg?branch=${remoteBranchName}" alt="Build status badge for the ${remoteBranchName} branch"/>
</a>
</div>
<div style="flex: 1; display: flex; justify-content: flex-end;">
<button class="aui-button aui-button-secondary merge-btn" data-branch-index="${index}">Merge command</button>
</div>
</div>
`;
}
});
// Add push command section.
branchContent += `
<div style="display: flex; justify-content: space-between; margin-bottom: 4px; align-items: center; border-top: 1px solid rgba(128, 128, 128, 0.3); padding-top: 8px; margin-top: 8px;">
<div style="font-weight: bold;">Push command</div>
<button class="aui-button aui-button-primary push-btn">Push command</button>
</div>
`;
branchContent += '</div>';
// Create empty div with id userscript_moodle_merger.
const userscriptDiv = $('<div>', {
id: 'userscript_moodle_merger',
html: branchContent,
css: {
'border': '1px solid rgba(128, 128, 128, 0.3)',
'border-radius': '4px',
'margin-bottom': '8px'
}
});
// Insert it after selectors.target.
$(selectors.target).after(userscriptDiv);
// Bind click events to merge buttons
$('.merge-btn').on('click', function() {
const branchIndex = $(this).data('branch-index');
const branchWithPullBranch = branchData[branchIndex];
handleMergeCommand(branchWithPullBranch, branchWithPullBranch.remoteBranchName);
});
// Bind click event to push button.
$('.push-btn').on('click', function() {
handlePushCommand(availableBranches);
});
};
// Wait for custom field to be available.
const waitForCustomField = () => {
console.log("Checking for custom field...");
// Check if our injected element already exists
if ($('#userscript_moodle_merger').length > 0) {
console.log("Userscript element already exists, skipping injection");
return;
}
if ($(selectors.pullRepo).length > 0 && $(selectors.target).length > 0) {
console.log("Custom field found, calling updateView");
updateView();
} else {
setTimeout(waitForCustomField, 1000);
}
};
// Use AJS.toInit if available, otherwise fallback to polling
const startScript = () => {
if (typeof AJS !== 'undefined' && typeof AJS.toInit === 'function') {
console.log("AJS available, using AJS.toInit");
AJS.toInit(() => {
console.log("AJS ready, starting to wait for custom field");
waitForCustomField();
});
} else if (typeof AJS === 'undefined') {
console.log("AJS not loaded yet, retrying...");
setTimeout(startScript, 1000);
} else {
// AJS exists but toInit not available, proceed anyway
console.log("AJS loaded without toInit, starting to wait for custom field");
waitForCustomField();
}
};
// Initialize on page load
setTimeout(startScript, 2000);
// Watch for DOM changes (Jira SPA navigation)
const observer = new MutationObserver((mutations) => {
// Check if our injected element exists
if ($('#userscript_moodle_merger').length === 0) {
console.log("DOM changed and userscript element is missing, re-initializing...");
// Debounce: wait a bit before re-injecting to avoid multiple calls
clearTimeout(window.userscriptReinitTimeout);
window.userscriptReinitTimeout = setTimeout(() => {
waitForCustomField();
}, 500);
}
});
// Observe the entire document for changes
observer.observe(document.body, {
childList: true,
subtree: true
});
};
// Execute the script.
userScript();
})();