-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathuserscript.js
119 lines (113 loc) · 5.14 KB
/
userscript.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
// ==UserScript==
// @name Github Deployments and Repository Dispatch Trigger
// @version 0.3
// @description Trigger deployment or repository_dispatch event for Github Actions
// @author Texot
// @namespace https://github.com/tete1030/github-repo-dispatcher
// @homepage https://github.com/tete1030/github-repo-dispatcher
// @require https://code.jquery.com/jquery-latest.min.js
// @match https://github.com/*/*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
function ask_parameter(varid, prompt, save, allow_empty, skip) {
if (skip && GM_getValue(varid, null) !== null) return GM_getValue(varid);
let value = window.prompt(prompt, GM_getValue(varid, ""));
if (value===null) throw null;
if (!allow_empty && !value) {
window.alert("Value should not be empty");
throw null;
}
if (save) GM_setValue(varid, value);
return value;
}
function trigger_repo_dispatch() {
let pathArray = window.location.pathname.split('/');
if (pathArray.length < 3) return;
let token = ask_parameter("token", "Token", true, false);
let type = ask_parameter("repo_dis_type", "Type", true);
let payload = ask_parameter("repo_dis_payload", "Client Payload (in JSON)", true, true);
let data = {event_type: type};
try {
if (payload) data.client_payload = JSON.parse(payload);
} catch (e) {
window.alert("JSON parse failed: " + e.message);
return;
}
GM_xmlhttpRequest({
method: "POST",
url: "https://api.github.com/repos/" + pathArray[1] + "/" + pathArray[2] + "/dispatches",
headers: {"Authorization": "token " + token, "Accept": "application/vnd.github.everest-preview+json"},
data: JSON.stringify(data),
onload: (resp) => {
if (resp.status == 204 || resp.status == 200) {
console.log("Repo dispatch succeed: " + resp.responseText);
window.alert("Repo dispatch succeed" + (resp.responseText?(": " + resp.responseText):""));
} else {
var message = resp.responseText;
try {
message = JSON.parse(message).message;
} catch (e) {}
console.log("Repo dispatch failed: " + resp.responseText);
window.alert("Repo dispatch failed: " + message);
}
},
onerror: (e) => {
console.log("Repo dispatch failed: " + e);
window.alert("Repo dispatch failed: " + e.message);
}
});
}
function trigger_deployments() {
let pathArray = window.location.pathname.split('/');
if (pathArray.length < 3) return;
let token = ask_parameter("token", "Token", true, false);
let ref = ask_parameter("deploy_ref", "Ref (Branch, Tag, Commit)", true);
let task = ask_parameter("deploy_task", "Task", true);
let payload = ask_parameter("deploy_payload", "Payload (in JSON)", true, true);
let data = {
ref: ref,
auto_merge: false,
task: task,
required_contexts: [],
description: "Created by Github Deployments and Repository Dispatch Trigger"
};
try {
if (payload) data.payload = JSON.parse(payload);
} catch (e) {
window.alert("JSON parse failed: " + e.message);
return;
}
GM_xmlhttpRequest({
method: "POST",
url: "https://api.github.com/repos/" + pathArray[1] + "/" + pathArray[2] + "/deployments",
headers: {"Authorization": "token " + token, "Accept": "application/vnd.github.ant-man-preview+json"},
data: JSON.stringify(data),
onload: (resp) => {
if (resp.status == 201) {
console.log("Create deployments succeed: " + resp.responseText);
window.alert("Create deployments succeed" + (resp.responseText?(": " + resp.responseText):""));
} else {
var message = resp.responseText;
try {
message = JSON.parse(message).message;
} catch (e) {}
console.log("Create deployments failed: " + resp.responseText);
window.alert("Create deployments failed: " + message);
}
},
onerror: (e) => {
console.log("Create deployments failed: " + e);
window.alert("Create deployments failed: " + e.message);
}
});
}
$(document).ready(() => {
let repo_dis_btn = $("<a class='btn btn-sm'>Repo Dispatch</a>").on("click", trigger_repo_dispatch);
let deployments_btn = $("<a class='btn btn-sm'>Deploy</a>").on("click", trigger_deployments);
$(".pagehead-actions").eq(0).prepend($("<li></li>").append(repo_dis_btn)).prepend($("<li></li>").append(deployments_btn));
});
})();