Github Deployments and Repository Dispatch Trigger

Trigger deployment or repository_dispatch event for Github Actions

As of 2019-12-25. See the latest version.

  1. // ==UserScript==
  2. // @name Github Deployments and Repository Dispatch Trigger
  3. // @version 0.1
  4. // @description Trigger deployment or repository_dispatch event for Github Actions
  5. // @author Texot
  6. // @namespace https://github.com/tete1030/github-repo-dispatcher
  7. // @homepage https://github.com/tete1030/github-repo-dispatcher
  8. // @require https://code.jquery.com/jquery-latest.min.js
  9. // @match https://github.com/*/*
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_xmlhttpRequest
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. function ask_parameter(varid, prompt, save, allow_empty, skip) {
  19. if (skip && GM_getValue(varid, null) !== null) return GM_getValue(varid);
  20. let value = window.prompt(prompt, GM_getValue(varid, ""));
  21. if (value===null) throw null;
  22. if (!allow_empty && !value) {
  23. window.alert("Value should not be empty");
  24. throw null;
  25. }
  26. if (save) GM_setValue(varid, value);
  27. return value;
  28. }
  29.  
  30. function trigger_repo_dispatch() {
  31. let pathArray = window.location.pathname.split('/');
  32. if (pathArray.length < 3) return;
  33. let token = ask_parameter("token", "Token", true, false);
  34. let type = ask_parameter("repo_dis_type", "Type", true);
  35. let payload = ask_parameter("repo_dis_payload", "Client Payload (in JSON)", true, true);
  36. let data = {event_type: type};
  37. try {
  38. if (payload) data.client_payload = JSON.parse(payload);
  39. } catch (e) {
  40. window.alert("JSON parse failed: " + e.message);
  41. return;
  42. }
  43. GM_xmlhttpRequest({
  44. method: "POST",
  45. url: "https://api.github.com/repos/" + pathArray[1] + "/" + pathArray[2] + "/dispatches",
  46. headers: {"Authorization": "token " + token, "Accept": "application/vnd.github.everest-preview+json"},
  47. data: JSON.stringify(data),
  48. onload: (resp) => {
  49. if (resp.status == 204 || resp.status == 200) {
  50. console.log("Repo dispatch succeed: " + resp.responseText);
  51. window.alert("Repo dispatch succeed" + (resp.responseText?(": " + resp.responseText):""));
  52. } else {
  53. var message = resp.responseText;
  54. try {
  55. message = JSON.parse(message).message;
  56. } catch (e) {}
  57. console.log("Repo dispatch failed: " + resp.responseText);
  58. window.alert("Repo dispatch failed: " + message);
  59. }
  60. },
  61. onerror: (e) => {
  62. console.log("Repo dispatch failed: " + e);
  63. window.alert("Repo dispatch failed: " + e.message);
  64. }
  65. });
  66. }
  67.  
  68. function trigger_deployments() {
  69. let pathArray = window.location.pathname.split('/');
  70. if (pathArray.length < 3) return;
  71. let token = ask_parameter("token", "Token", true, false);
  72. let ref = ask_parameter("deploy_ref", "Ref (Branch, Tag, Commit)", true);
  73. let task = ask_parameter("deploy_task", "Task", true);
  74. let payload = ask_parameter("deploy_payload", "Payload (in JSON)", true, true);
  75. let data = {
  76. ref: ref,
  77. auto_merge: false,
  78. task: task,
  79. required_contexts: [],
  80. description: "Created by Github Deployments and Repository Dispatch Trigger"
  81. };
  82. try {
  83. if (payload) data.payload = JSON.parse(payload);
  84. } catch (e) {
  85. window.alert("JSON parse failed: " + e.message);
  86. return;
  87. }
  88. GM_xmlhttpRequest({
  89. method: "POST",
  90. url: "https://api.github.com/repos/" + pathArray[1] + "/" + pathArray[2] + "/deployments",
  91. headers: {"Authorization": "token " + token, "Accept": "application/vnd.github.ant-man-preview+json"},
  92. data: JSON.stringify(data),
  93. onload: (resp) => {
  94. if (resp.status == 201) {
  95. console.log("Create deployments succeed: " + resp.responseText);
  96. window.alert("Create deployments succeed" + (resp.responseText?(": " + resp.responseText):""));
  97. } else {
  98. var message = resp.responseText;
  99. try {
  100. message = JSON.parse(message).message;
  101. } catch (e) {}
  102. console.log("Create deployments failed: " + resp.responseText);
  103. window.alert("Create deployments failed: " + message);
  104. }
  105. },
  106. onerror: (e) => {
  107. console.log("Create deployments failed: " + e);
  108. window.alert("Create deployments failed: " + e.message);
  109. }
  110. });
  111. }
  112.  
  113. $(document).ready(() => {
  114. let repo_dis_btn = $("<a class='btn btn-sm'>Repo Dispatch</a>").on("click", trigger_repo_dispatch);
  115. let deployments_btn = $("<a class='btn btn-sm'>Deploy</a>").on("click", trigger_deployments);
  116. $(".pagehead-actions").eq(0).prepend($("<li></li>").append(repo_dis_btn)).prepend($("<li></li>").append(deployments_btn));
  117. });
  118.  
  119. })();