Permalink
Browse files

Add initial attempt at manipulating tasks.

  • Loading branch information...
1 parent 95b2fd6 commit 8142677857e5324b7ea54002495f97de4f8d7242 @jdm committed Dec 17, 2012
Showing with 115 additions and 0 deletions.
  1. +32 −0 cgi-bin/oauth_redirect.cgi
  2. +17 −0 taskmanagement.html
  3. +66 −0 taskmanagement.js
View
@@ -0,0 +1,32 @@
+#!/usr/bin/env python
+
+# enable debugging
+import cgitb
+import cgi
+import urllib2
+import ConfigParser
+cgitb.enable()
+
+form = cgi.FieldStorage()
+url = urllib2.unquote(form.getfirst('url', ''))
+code = form.getfirst('code', '')
+
+config = ConfigParser.RawConfigParser()
+config.read('config')
+id = config.get('github', 'app_id')
+secret = config.get('github', 'app_secret')
+
+data = {'client_id': id, 'redirect_uri': url, 'client_secret': secret, 'code': code}
+
+req = urllib2.Request('https://github.com/login/oauth/access_token?' +
+ 'client_id=%s&redirect_uri=%s&client_secret=%s&code=%s' %
+ (id, url, secret, code))
+f = urllib2.urlopen(req)
+
+results = f.read()
+
+print 'Status: 303'
+print 'Location: %s?%s' % (url, results)
+print "Content-Type: text/html;charset=utf-8"
+print
+print "<html><body><a href=%s?%s>Click here if you're not redirected automatically</a></body></html>" % (url, results)
View
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+<script type="text/javascript" src="taskmanagement.js"></script>
+</head>
+<body>
+ <form action="">
+ <input id="tasktitle"><br>
+ <textarea id="taskbody"></textarea><br>
+ <button type="button"
+ onclick="submitTask(window.tasktitle.value, window.taskbody.value, function() { console.log('submitted'); }); return false;">
+ Submit task
+ </button>
+ </form>
+</body>
+</html>
View
@@ -0,0 +1,66 @@
+var githubToken;
+var config = {
+ owner: 'jdm',
+ repo: 'moztasks',
+ tag: 'task'
+};
+
+function check_query (name) {
+ return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(name).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
+}
+
+if (!check_query('access_token')) {
+ path = window.location.pathname;
+ path = path.substr(0, path.lastIndexOf('/') + 1);
+ path += 'cgi-bin/oauth_redirect.cgi?url=' + encodeURIComponent(window.location.protocol + '//' + window.location.host + window.location.pathname);
+ redirect = window.location.protocol + '//' + window.location.host + path;
+ clientID = location.hostname == "localhost" ? "73fd92744901e18828bb" : "7b9eaa72a4f18972fea5";
+ window.location = "https://github.com/login/oauth/authorize?scope=public_repo&client_id=" + clientID + "&redirect_uri=" + redirect;
+}
+
+window.onload = function() {
+ githubToken = check_query('access_token');
+};
+
+function _request(method, url, data, success, error) {
+ data['access_token'] = githubToken;
+ $.ajax({type: method,
+ url: url,
+ data: data,
+ beforeSend: function(xhr) {
+ xhr.setRequestHeader("Authorization", "token " + githubToken);
+ xhr.setRequestHeader("Accept", 'application/vnd.github.raw');
+ },
+ success: function(response) { callback(JSON.parse(response)); },
+ error: error(),
+ /*contentType: 'application/json',*/
+ contentType: 'application/x-www-form-urlencoded',
+ dataType: 'json'
+ });
+}
+
+/* success is a function that is passed the API response data specified at
+ * http://developer.github.com/v3/issues/#create-an-issue
+ */
+function submitTask(summary, body, success, error) {
+ var data = {
+ title: summary,
+ body: body,
+ labels: [config['tag']]
+ };
+ _request('POST', 'https://api.github.com/repos/' + config['owner'] + '/' + config['repo'] + '/issues',
+ data, success, error || function() { console.log('Error creating task'); });
+}
+
+/* success is a function that is passed the API response data specified at
+ * http://developer.github.com/v3/issues/#edit-an-issue
+ */
+function editTask(id, summary, body, success, error) {
+ var data = {
+ title: summary,
+ body: body,
+ labels: [config['tag']]
+ };
+ _request('PATCH', 'https://api.github.com/repos/' + config['owner'] + '/' + config['repo'] + '/issues/' + id,
+ data, success, error || function() { console.log('Error editing task'); });
+}

0 comments on commit 8142677

Please sign in to comment.