forked from cmcarthur/github-projects-story-points
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-issues.js
137 lines (120 loc) · 3.63 KB
/
github-issues.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
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
(function (d, w) {
'use strict';
const githubCredentials = new Promise(resolve => {
chrome.storage.sync.get({ githubUser: '', githubToken: '' }, items => resolve(items));
});
var debounce = function (func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
const moveTo = (card_id, position) => async (e) => {
e.preventDefault();
e.stopPropagation();
e.target.innerHTML = '⏳';
const { githubUser, githubToken } = await githubCredentials;
if (!githubUser || !githubToken) {
alert('please set github credentials in Github Projects Story Points settings');
return;
}
const res = await fetch(
`https://api.github.com/projects/columns/cards/${card_id}/moves`, {
headers: {
'Authorization': 'Basic ' + btoa(githubUser + ":" + githubToken),
'Accept': 'application/vnd.github.inertia-preview+json',
},
method: 'POST',
body: JSON.stringify({ position }),
}
);
if (!res.ok) {
console.error(`Github API error: ${res.statusTest}\n${await res.text()}`);
alert(`Github API error: ${res.statusTest}`);
} else {
e.target.innerHTML = '✔️';
}
return false;
}
const addProjectButtons = () => {
const projects = document.querySelectorAll(
'form[aria-label="Select projects"] div[data-url*="show_partial?card_id="]');
projects.forEach(project => {
if (project.querySelectorAll('.gpsp-project-buttons').length > 0) {
return;
}
const card_id = project.dataset.url.match(/show_partial\?card_id=(\d+)&/)[1];
const buttons = document.createElement('div');
buttons.classList.add('gpsp-project-buttons');
const addButton = (text, direction) => {
const button = document.createElement('div');
button.innerText = text;
button.addEventListener('click', moveTo(card_id, direction), {capture: true});
button.addEventListener('mousedown', e => { e.preventDefault(); }, {capture: true});
buttons.append(button);
};
addButton('↑', 'top');
addButton('↓', 'bottom');
project.prepend(buttons);
});
};
const hideBotChanges = () => {
document.querySelectorAll('.js-timeline-item .TimelineItem').forEach(el => {
if (el.classList.contains('gpsp-hidden')) {
return;
}
if (el.querySelector(
'.TimelineItem-body a.author[href="https://github.com/apps/cyberhaven-project-bot"]')) {
el.classList.add('gpsp-hidden');
}
});
};
const addStyle = () => {
const sheet = document.createElement('style');
sheet.innerHTML = `
.gpsp-project-buttons {
visibility: hidden;
position: absolute;
top: 0;
bottom: 0;
right: 4px;
display: flex;
flex-direction: column;
justify-content: center;
}
.gpsp-project-buttons div {
width: 16px;
text-align: center;
cursor: pointer;
}
form[aria-label="Select projects"] div[data-url] {
position: relative;
}
form[aria-label="Select projects"] div[data-url]:hover .gpsp-project-buttons div {
visibility: visible;
}
.gpsp-hidden {
display: none;
}
`;
document.body.appendChild(sheet);
};
addStyle();
addProjectButtons();
hideBotChanges();
const container = document.querySelector('.js-check-all-container');
if (container) {
container.addEventListener('DOMSubtreeModified', debounce(() => {
addProjectButtons();
hideBotChanges();
}, 50));
}
})();