Github Repository Dispatch Trigger

Add repository_dispatch trigger for Github Actions

2019-12-22 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  1. // ==UserScript==
  2. // @name Github Repository Dispatch Trigger
  3. // @version 0.1
  4. // @description Add repository_dispatch trigger 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. $(document).ready(() => {
  19. let btn = $("<a class='btn btn-sm'>Repo Dispatch</a>");
  20. btn.on("click", () => {
  21. let token = GM_getValue("token", "");
  22. if (!token) {
  23. token = window.prompt("Token");
  24. }
  25. if (!token) return;
  26. GM_setValue("token", token);
  27. let pathArray = window.location.pathname.split('/');
  28. if (pathArray.length < 3) return;
  29. let type = window.prompt("Type", GM_getValue("type", ""));
  30. if (!type) return;
  31. GM_setValue("type", type);
  32. let payload = window.prompt("Client Payload", GM_getValue("payload", ""));
  33. if (payload === null) return;
  34. GM_setValue("payload", payload);
  35. let data = {event_type: type};
  36. try {
  37. if (payload) data.client_payload = JSON.parse(payload);
  38. } catch (e) {
  39. window.alert("JSON parse failed: " + e.message);
  40. return;
  41. }
  42. GM_xmlhttpRequest({
  43. method: "POST",
  44. url: "https://api.github.com/repos/" + pathArray[1] + "/" + pathArray[2] + "/dispatches",
  45. headers: {"Authorization": "token " + token, "Accept": "application/vnd.github.everest-preview+json"},
  46. data: JSON.stringify(data),
  47. onload: (resp) => {
  48. if (resp.status == 204 || resp.status == 200) {
  49. console.log("Repo dispatch succeed: " + resp.responseText);
  50. window.alert("Repo dispatch succeed" + (resp.responseText?(": " + resp.responseText):""));
  51. } else {
  52. var message = resp.responseText;
  53. try {
  54. message = JSON.parse(message).message;
  55. } catch (e) {}
  56. console.log("Repo dispatch failed: " + resp.responseText);
  57. window.alert("Repo dispatch failed: " + message);
  58. }
  59. },
  60. onerror: (e) => {
  61. console.log("Repo dispatch failed: " + e);
  62. window.alert("Repo dispatch failed: " + e.message);
  63. }
  64. });
  65. });
  66. $(".pagehead-actions").eq(0).prepend($("<li></li>").append(btn));
  67. });
  68.  
  69. })();