Export GitHub Issues

Can export issues by state and label

  1. // ==UserScript==
  2. // @name Export GitHub Issues
  3. // @namespace http://userscripts.org/users/tim
  4. // @description Can export issues by state and label
  5. // @include http://github.com/*/issues*
  6. // @license MIT (See file header)
  7. // @copyright (c) 2011 Tim Smart
  8. // @version 0.0.1.20140425034615
  9. // ==/UserScript==
  10.  
  11. // The MIT License
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining a copy
  14. // of this software and associated documentation files (the "Software"), to deal
  15. // in the Software without restriction, including without limitation the rights
  16. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. // copies of the Software, and to permit persons to whom the Software is
  18. // furnished to do so, subject to the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be included in
  21. // all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. // THE SOFTWARE.
  30.  
  31. (function() {
  32.  
  33.  
  34. const URL_EXPRESSION = /^.*\/(.*?)\/(.*?)\/issues.*$/;
  35. const GITHUB_API_URL = 'http://github.com/api/v2/json';
  36.  
  37. var url = top.location.href.match(URL_EXPRESSION);
  38. if (!url)
  39. return;
  40.  
  41. [, user, repo] = url;
  42.  
  43. // Now for the github api goodness
  44. var GithubRequest = function() {
  45. arguments = Array.prototype.slice.call(arguments);
  46. this._options = arguments;
  47. };
  48. GithubRequest.prototype = {
  49. send: function(callback) {
  50. var self = this;
  51.  
  52. GM_xmlhttpRequest({
  53. method: 'GET',
  54. url: GITHUB_API_URL + '/' + this._makeApiParams(),
  55. onload: function(xhr) {
  56. var response = JSON.parse(xhr.responseText);
  57. callback.call(self, response);
  58. }
  59. });
  60. },
  61. _makeApiParams: function() {
  62. return this._options.join('/');
  63. }
  64. };
  65.  
  66. // Formatter
  67. var formatOutput = function(source) {
  68. var output = '';
  69.  
  70. for (var i = 0, issue; issue = source[i]; i++) {
  71. // Title
  72. output += "Title: " + issue.title + "\n";
  73. // State
  74. output += "State: " + issue.state + "\n";
  75. // Labels
  76. output += "Labels: " + issue.labels.join(', ') + "\n";
  77. // Created
  78. output += "Created On: " + issue.created_at + "\n";
  79. // Updated
  80. output += "Last Updated: " + issue.updated_at + "\n";
  81. // Body
  82. //output += "Body:\n" + issue.body + "\n";
  83.  
  84. output += "\n\n";
  85. }
  86.  
  87. return output;
  88. };
  89.  
  90. // Make the callbacks
  91. var menuCallback = function() {
  92. var state = prompt('What state should the issues be? ("open", "closed")', 'closed');
  93. state = state.toLowerCase();
  94. if ('closed' !== state && 'open' !== state) {
  95. alert('Invalid state!');
  96. return;
  97. }
  98.  
  99. var labels = prompt('What labels should the issues have? (Seperated by ",")');
  100. labels = labels.toLowerCase();
  101. if ('' === labels)
  102. labels = false;
  103. else
  104. labels = labels.split(',');
  105.  
  106. new GithubRequest('issues', 'list', user, repo, state).send(function(response) {
  107. if ("object" === typeof response.issues)
  108. var issues = response.issues;
  109. else {
  110. alert('Bad response!');
  111. return;
  112. }
  113.  
  114. if (false !== labels) {
  115. issues = issues.filter(function(issue) {
  116. if (0 >= issue.labels.length)
  117. return false;
  118.  
  119. for (var i = 0, label; label = issue.labels[i]; i++) {
  120. if (-1 !== labels.indexOf(label.toLowerCase()))
  121. return true;
  122. }
  123. });
  124. }
  125.  
  126. issues = formatOutput(issues);
  127. GM_openInTab("data:text/plain;charset=utf-8," + encodeURIComponent(issues));
  128. });
  129. };
  130.  
  131. GM_registerMenuCommand('Export Github Issues', menuCallback);
  132.  
  133. })();