-
Notifications
You must be signed in to change notification settings - Fork 0
/
githubsyncadaptor.js
121 lines (101 loc) · 2.96 KB
/
githubsyncadaptor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*\
title: $:/plugins/kpe/githubsync/githubsyncadaptor.js
type: application/javascript
module-type: syncadaptor
A sync adaptor module for GitHub.
\*/
/**
* File: githubsyncadaptor.js
* Created by kpe on 21-May-2014 at 4:19 PM.
*/
(function () {
/*jslint node:true, browser:true */
/*global $tw: false*/
'use strict';
var CONFIG_REPO_TIDDLER = "$:/config/githubsync/repo";
var DEFAULT_REPO_TIDDLER = "$username/$repo";
/**
*
* @param syncer
* @constructor
*/
function GitHubAdaptor(syncer) {
this.syncer = syncer;
this.repo = this.getRepo();
this.recipe = undefined;
this.logger = new $tw.utils.Logger("GitHubAdaptor");
}
GitHubAdaptor.prototype.getRepo = function () {
var result = null;
var text = this.syncer.wiki.getTiddlerText(CONFIG_REPO_TIDDLER, DEFAULT_REPO_TIDDLER);
var match = /(\w+)+\/(\w+)+]/.exec(text);
if(match) {
result = {username: match[1], repo: match[2]};
} else {
result = {username: 'kpe', repo: 'test'};
}
return result;
};
GitHubAdaptor.prototype.getTiddlerInfo = function (tiddler) {
return {
bag: tiddler.fields["bag"]
};
};
/**
* Get the current status of the GitHub connection
*/
GitHubAdaptor.prototype.getStatus = function (callback) {
var self = this;
var wiki = self.syncer.wiki;
var githubAToken = null;
if(localStorage){
githubAToken = localStorage['githubsync-access-token'];
}
if(!githubAToken) {
callback(new Error('No GitHub Access Token available.'));
}
this.logger.log('Testing GitHub Connection');
var Github = require('$:/plugins/kpe/githubsync/lib/github.js');
var github = new Github({auth:'oauth', token: githubAToken});
var repo = github.getRepo(this.repo.username, this.repo.repo);
repo.show(function(err, repo){
if(err) {
return callback(err);
}
self.logger.log('repo',repo);
var isLoggedIn = true;
if(callback) {
callback(null, isLoggedIn, self.repo.username);
}
});
};
GitHubAdaptor.prototype.login = function (username, password, callback) {
var githubAToken = null;
if(localStorage){
githubAToken = localStorage['githubsync-access-token'];
}
if(!githubAToken) {
callback(new Error('No GitHub Access Token available.'));
}
var Github = require('$:/plugins/kpe/githubsync/lib/github.js');
var github = new Github({auth:'oauth',token:githubAToken});
this.logger.log('About to log in with GitHub');
var repo = github.getRepo(this.repo.username, this.repo.repo);
repo.listBranches(function(err,data){
callback(err);
});
};
GitHubAdaptor.prototype.logout = function (callback) {
};
GitHubAdaptor.prototype.getSkinnyTiddlers = function (callback) {
};
GitHubAdaptor.prototype.saveTiddler = function (callback) {
};
GitHubAdaptor.prototype.loadTiddler = function (callback) {
};
GitHubAdaptor.prototype.deleteTiddler = function (callback) {
};
if($tw.browser && document.location.protocol.substr(0,4) === "http" ) {
exports.adaptorClass = GitHubAdaptor;
}
})();