-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.js
133 lines (121 loc) · 3.82 KB
/
github.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
122
123
124
125
126
127
128
129
130
131
132
133
var GitHub = function (repo) {
this.repo = repo;
this.memo = {
issues: {
value: [],
status: 'stale' // in ["fresh", "refreshing", "stale"]
},
milestones: {
value: [],
status: 'stale'
}
};
};
GitHub.prototype.fetchFromGithub = function(endpoint, fetchOptions) {
var authPromise = new Promise(function (resolve, reject) {
chrome.storage.sync.get('githubAuthorization', function (items) {
if (items.githubAuthorization) {
resolve(items.githubAuthorization.token);
} else {
reject('No authorization found!');
}
});
});
return authPromise.then(function (token) {
var options = $.extend({}, fetchOptions);
options.headers = $.extend({}, options.headers, {
'Authorization': 'token ' + token
});
var url;
if (endpoint.indexOf('https') == 0) {
url = endpoint;
} else {
url = 'https://api.github.com/repos/' + this.repo + endpoint;
}
return fetch(url, options);
}.bind(this));
};
GitHub.prototype.fetchIssues = function (url, tmpIssues) {
if (this.memo.issues.status === 'stale') {
this.memo.issues.status = 'refreshing';
return this.refreshIssues(url, tmpIssues);
} else {
return Promise.resolve(this.memo.issues.value);
}
};
GitHub.prototype.fetchMilestones = function () {
if (this.memo.milestones.status === 'stale') {
this.memo.milestones.status = 'refreshing';
return this.refreshMilestones();
} else {
return Promise.resolve(this.memo.milestones.value);
}
};
GitHub.prototype.refreshIssues = function (url, tmpIssues) {
var endpoint = '/issues?per_page=100&state=open&direction=asc';
return this.fetchFromGithub(endpoint).then(function (response) {
var links = {};
//check if there is a link, stops a crash when number of issues <=100
if (response.headers.get('Link') != null) {
response.headers.get('Link').split(', ').forEach(function(headLink){
var s = headLink.split('; ');
links[s[1]] = s[0].substr(1, s[0].length-2);
});
}
return response.json().then(function (data) {
var issues = (tmpIssues ? tmpIssues : []).concat(data);
if(links['rel="next"']) {
return this.refreshIssues(links['rel="next"'], issues);
}
else {
this.memo.issues.value = issues;
this.memo.issues.status = "fresh";
return Promise.resolve(issues);
}
}.bind(this));
}.bind(this));
};
GitHub.prototype.refreshMilestones = function () {
return this.fetchFromGithub('/milestones?per_page=100&state=open')
.then(function (response) {
return response.json();
}).then(function (data) {
this.memo.milestones.value = data;
this.memo.milestones.status = "fresh";
return Promise.resolve(this.memo.milestones.value);
}.bind(this));
};
GitHub.prototype.refresh = function (cb) {
for (var key in memo) {
if (memo.hasOwnProperty(key)) {
if (memo[key].status = "fresh") {
memo[key].status = "stale";
}
}
}
cb();
};
GitHub.prototype.fetchRepos = function () {
return this.fetchFromGithub('https://api.github.com/user/repos', {
headers: {'Accept': 'application/vnd.github.moondragon+json'}
}).then(function (response) {
return response.json();
}).then(function (repos) {
return repos;
});
};
GitHub.prototype.update_ms_due_on = function(milestoneId, due_on, cb) {
var url = baseUrl+"/milestones/"+milestoneId;
console.log(url);
return this.fetchFromGithub(url, {
method: 'PATCH',
body: JSON.stringify({
"due_on": due_on
})
}).then(function (response) {
return response.json();
}).then(function (data) {
console.log(data);
cb(null, null);
});
};