Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 4 commits
  • 1 file changed
  • 0 comments
  • 1 contributor

Showing 1 changed file with 116 additions and 14 deletions. Show diff stats Hide diff stats

  1. 130  github.js
130  github.js
@@ -22,24 +22,126 @@ var GITHUB_API_URL = 'https://api.github.com';
22 22
 // TODO: this should be obtained from a configuration dialog.
23 23
 var API_ACCESS_KEY = 'YOUR_API_ACCESS_KEY_HERE!';
24 24
 
  25
+var linkexp=/<[^>]*>\s*(\s*;\s*[^\(\)<>@,;:"\/\[\]\?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*")))*(,|$)/g;
  26
+var paramexp=/[^\(\)<>@,;:"\/\[\]\?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*"))/g;
  27
+
25 28
 var GitHub = (function() {
26 29
 
27  
-  function GitHub() {
  30
+  // base_uri and api_key could be specified at initialization time
  31
+  function GitHub(base_uri, api_key) {
  32
+    this.base_uri = base_uri || GITHUB_API_URL;
  33
+    this.api_key = api_key || API_ACCESS_KEY;
28 34
   }
29 35
   
30  
-  GitHub.prototype.getCommits = function (user, project) {
31  
-    // TODO: Sanity check!
32  
-    
33  
-    var uri = GITHUB_API_URL + '/repos/' + user + '/' + project + '/commits';
34  
-    
35  
-    var response = UrlFetchApp.fetch(uri);
36  
-    
37  
-    var object = eval(response.getContentText());
38  
-    
39  
-    Logger.log(response.getContentText());
40  
-    Logger.log(object[0].commit.message);
  36
+  // Parse linkHeader whiich contains the 'next page uri' of a request in Github
  37
+  var parseLinkHeader = function (value) {
  38
+    var matches = value.match(linkexp);
  39
+    var rels = new Object();
  40
+    var titles = new Object();
41 41
     
42  
-    return object;
  42
+    for (i = 0; i < matches.length; i++) {
  43
+      var split = matches[i].split('>');
  44
+      var href = split[0].substring(1);
  45
+      var ps = split[1];
  46
+      var link = new Object();
  47
+      link.href = href;
  48
+      var s = ps.match(paramexp);
  49
+      
  50
+      function unquote(value) {
  51
+        if (value.charAt(0) == '"' && value.charAt(value.length - 1) == '"') return value.substring(1, value.length - 1);
  52
+        return value;
  53
+      }
  54
+      
  55
+      for (j = 0; j < s.length; j++) {
  56
+        var p = s[j];
  57
+        var paramsplit = p.split('=');
  58
+        var name = paramsplit[0];
  59
+        link[name] = unquote(paramsplit[1]);
  60
+      }
  61
+      
  62
+      if (link.rel != undefined) {
  63
+        rels[link.rel] = link;
  64
+      }
  65
+      if (link.title != undefined) {
  66
+        titles[link.title] = link;
  67
+      }
  68
+    }
  69
+    var linkheader = new Object();
  70
+    linkheader.rels = rels;
  71
+    linkheader.titles = titles;
  72
+    return linkheader;
  73
+  }
  74
+
  75
+  // Github paginates requests that return multiple items
  76
+  // See http://developer.github.com/v3/#pagination
  77
+  // This allows to retrieve all items of a multiple item request
  78
+  GitHub.prototype.retrieveAll = function (api_call) {
  79
+    var page = 0;
  80
+    var per_page = 100;
  81
+
  82
+    // First request
  83
+    var uri = this.base_uri + api_call + '?page=' + page + '&per_page=' + per_page + '&key=' + this.api_key;
  84
+    try {
  85
+      var response = UrlFetchApp.fetch(uri);
  86
+      var content = Utilities.jsonParse(response.getContentText());
  87
+      var headers = response.getHeaders();
  88
+    } catch (err) {
  89
+      return [];
  90
+    }
  91
+      
  92
+    if (!('Link' in headers)) {
  93
+      // No more pages to retrieve
  94
+      return content;
  95
+    } 
  96
+    else {
  97
+      // Get the next page uri from header
  98
+      var link_headers = parseLinkHeader(headers['Link']);
  99
+
  100
+      var results = [];
  101
+      do {
  102
+        results = results.concat(content);
  103
+        var uri = link_headers['rels'].next.href;
  104
+        var response = UrlFetchApp.fetch(uri);
  105
+        var content = Utilities.jsonParse(response.getContentText());
  106
+        var headers = response.getHeaders();
  107
+        var link_headers = parseLinkHeader(headers['Link']);
  108
+      } while (link_headers['rels'].next)
  109
+      results = results.concat(content);
  110
+      return results;
  111
+    }
  112
+  };
  113
+
  114
+  // Repos API http://developer.github.com/v3/repos/
  115
+  // /repos/:user/:repo/commits
  116
+  GitHub.prototype.getReposCommits = function (user, project) {   
  117
+    var api_call = '/repos/' + user + '/' + project + '/commits';
  118
+    return this.retrieveAll(api_call);
  119
+  };
  120
+
  121
+  // /repos/:user/:repo/forks
  122
+  GitHub.prototype.getReposForks = function (user, project) {
  123
+    var api_call = '/repos/' + user + '/' + project + '/forks';
  124
+    return this.retrieveAll(api_call);
  125
+  };
  126
+
  127
+  // Orgs API http://developer.github.com/v3/orgs/
  128
+  // /orgs/:org/members
  129
+  GitHub.prototype.getOrgMembers = function (org) {
  130
+    var api_call = '/orgs/' + org + '/members';
  131
+    return this.retrieveAll(api_call);
  132
+  };
  133
+
  134
+  // Users API http://developer.github.com/v3/users/
  135
+  // /users/:user
  136
+  GitHub.prototype.getUser = function (user) {
  137
+    var api_call = '/users/' + user;
  138
+    return this.retrieveAll(api_call);
  139
+  };
  140
+
  141
+  // /users/:user/repos
  142
+  GitHub.prototype.getUserRepos = function (user) {
  143
+    var api_call = '/users/' + user + '/repos';
  144
+    return this.retrieveAll(api_call);
43 145
   };
44 146
 
45 147
   return GitHub;
@@ -47,6 +149,6 @@ var GitHub = (function() {
47 149
 })();
48 150
 
49 151
 function stub() {
50  
-  gh = new GitHub();
  152
+  gh = new GitHub(GITHUB_API_URL);
51 153
   gh.getCommits('gloob', 'github-google_apps_scripts');
52 154
 }

No commit comments for this range

Something went wrong with that request. Please try again.