forked from heipei/github-commit-badge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
github-commit-badge.js
226 lines (193 loc) · 8.88 KB
/
github-commit-badge.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// github-commit-badge.js (c) 2011 by Johannes 'heipei' Gilger
//
// The source-code should be pretty self-explanatory. Also look at the
// style.css to customize the badge.
// for truncating the commit-id and commit-message in place
function truncate(string, length, truncation) {
length = length || 30;
truncation = (typeof truncation == 'undefined') ? '...' : truncation;
return string.length > length ?
string.slice(0, length - truncation.length) + truncation : string;
};
function parseDate(dateTime) { // thanks to lachlanhardy
var timeZone = 1; // TODO: This doesn't really work
dateTime = dateTime.substring(0,19) + "Z";
var theirTime = dateTime.substring(11,13);
var ourTime = parseInt(theirTime) + 7 + timeZone;
if (ourTime > 24) {
ourTime = ourTime - 24;
};
dateTime = dateTime.replace("T" + theirTime, "T" + ourTime);
return dateTime;
};
var DEFAULT_BRANCH_NAME = 'master';
var COMMIT_MSG_MAX_LENGTH = 120;
var COMMIT_DISPLAYED_ID_LENGTH = 10;
var SHOW_FILES_TXT = 'Show files';
var HIDE_FILES_TXT = 'Hide files';
var GRAVATAR_URL_PREFIX = 'http://www.gravatar.com/avatar/';
var GRAVATAR_IMG_SIZE = 60;
function mainpage () {
$.each(Badges, function(i, badgeData) {
var branchName = ((typeof badgeData.branch == 'undefined' || badgeData.branch.length == 0) ? DEFAULT_BRANCH_NAME : badgeData.branch);
var urlData = "http://github.com/api/v1/json/" + badgeData.username + "/" + badgeData.repo
+ "/commit/" + branchName + "?callback=?";
$.getJSON(urlData, function(data) {
var myUser = badgeData.username;
var myRepo = badgeData.repo.replace(/\./g, '-');;
var myEval = eval (data);
var myUser = badgeData["username"];
var myRepo = badgeData["repo"]
var myEval = eval ( data );
var added = myEval.commit.added || [];
var modified = myEval.commit.modified || [];
var removed = myEval.commit.removed || [];
// outline-class is used for the badge with the border
var myBadge = document.createElement("div");
myBadge.setAttribute("class","outline");
// the username/repo
var myUserRepo = document.createElement("div");
myUserRepo.setAttribute("class","username");
var myLink = document.createElement("a");
myLink.setAttribute("href","http://github.com/" + myUser + "/" + badgeData["repo"]);
myLink.appendChild(document.createTextNode(myUser + "/" + badgeData["repo"]));
myUserRepo.appendChild(myLink);
var request_url = "http://github.com/api/v2/json/repos/show/" + badgeData.username + "/" + badgeData.repo + "?callback=?"
$.getJSON(request_url, function(data) {
followers = document.createElement("span");
followers.setAttribute("class", "followers");
followers.innerHTML = " (" + data.repository.forks + " forks, " + data.repository.watchers + " watchers)";
myUserRepo.appendChild(followers);
});
// myDiffLine is the "foo committed xy on date" line
var myDiffLine = document.createElement("div");
myDiffLine.setAttribute("class", "diffline");
// the image-class uses float:left to sit left of the commit-message
var myImage = document.createElement("img");
myImage.setAttribute("src",GRAVATAR_URL_PREFIX + hex_md5(myEval.commit.committer.email) + "?s=" + GRAVATAR_IMG_SIZE);
myImage.setAttribute("class","gravatar");
myImage.setAttribute("alt",myUser);
myDiffLine.appendChild(myImage);
var myLink = document.createElement("a");
myLink.setAttribute("href","http://github.com" + myEval.commit.url);
myLink.setAttribute("class", "badge");
myLink.appendChild(document.createTextNode(" " + truncate(myEval.commit.id,COMMIT_DISPLAYED_ID_LENGTH,"")));
myDiffLine.appendChild(document.createTextNode(myEval.commit.committer.name + " committed "));
var myDate = document.createElement("span");
var dateTime = parseDate(myEval.commit.committed_date);
myDate.setAttribute("class", "text-date");
myDate.setAttribute("title", dateTime);
myDate.appendChild(document.createTextNode(dateTime));
myDiffLine.appendChild(myLink);
myDiffLine.appendChild(document.createTextNode(" about "));
myDiffLine.appendChild(myDate);
// myCommitMessage is the commit-message
var myCommitMessage = document.createElement("div");
myCommitMessage.setAttribute("class", "commitmessage");
myCommitMessage.appendChild(document.createTextNode('"' + truncate(myEval.commit.message.replace(/\n.*/g, "").replace(/\r.*/g, ""),COMMIT_MSG_MAX_LENGTH) + '"'));
// myDiffStat shows how many files were added/removed/changed
var myDiffStat = document.createElement("div");
myDiffStat.setAttribute("class", "diffstat");
myDiffStat.innerHTML = "(" + added.length + " <span class='diffadded'>added</span>, "
+ removed.length + " <span class='diffremoved'>removed</span>, "
+ modified.length + " <span class='diffchanged'>changed</span>) ";
// only show the "Show files" button if the commit actually added/removed/modified any files at all
if (added.length > 0 || removed.length > 0 || modified.length > 0) {
myDiffStat.innerHTML += "<a href='' class='showMoreLink' id='showMoreLink_" + myUser + "_" + myRepo + "'>" + SHOW_FILES_TXT + "</a>";
};
// myFileList lists addded/remove/changed files, hidden at startup
var myFileList = document.createElement("div");
myFileList.setAttribute("class", "filelist");
myFileList.setAttribute("id", myUser + '_' + myRepo);
var myAddedFileList = document.createElement("div");
myAddedFileList.innerHTML = "<span class='diffadded'>Added:</span>";
var myList = document.createElement("ul");
var myFile;
$.each(added, function(j, myAdded) {
myFile = document.createElement("li");
myFile.appendChild(document.createTextNode(myAdded.filename));
myList.appendChild(myFile);
});
myAddedFileList.appendChild(myList);
var myRemovedFileList = document.createElement("div");
myRemovedFileList.innerHTML = "<span class='diffremoved'>Removed:</span>";
myList = document.createElement("ul");
$.each(removed, function(j, myRemoved) {
myFile = document.createElement("li");
myFile.appendChild(document.createTextNode(myRemoved.filename));
myList.appendChild(myFile);
});
myRemovedFileList.appendChild(myList);
var myModifiedFileList = document.createElement("div");
myModifiedFileList.innerHTML = "<span class='diffchanged'>Changed:</span>";
myList = document.createElement("ul");
$.each(modified, function(j, myModified) {
myFile = document.createElement("li");
myFile.appendChild(document.createTextNode(myModified.filename));
myList.appendChild(myFile);
});
myModifiedFileList.appendChild(myList);
// add the 3 sections only if they have files in them
if (added.length > 0) {
myFileList.appendChild(myAddedFileList);
};
if (removed.length > 0) {
myFileList.appendChild(myRemovedFileList);
};
if (modified.length > 0) {
myFileList.appendChild(myModifiedFileList);
};
// throw everything into our badge
myBadge.appendChild(myUserRepo);
myBadge.appendChild(myDiffLine);
myBadge.appendChild(myCommitMessage);
myBadge.appendChild(myDiffStat);
myBadge.appendChild(myFileList);
// and then the whole badge into the container
$("#gcb-container")[0].appendChild(myBadge);
// initially hiding the file-list and the behaviour of the Show-files button
$("#" + myUser + '_' + myRepo).hide();
$("#showMoreLink_" + myUser + '_' + myRepo).click(function () {
$("#" + myUser + '_' + myRepo).toggle();
if ($(this).text() == "Show files") {
$(this).text("Hide files");
} else {
$(this).text(SHOW_FILES_TXT);
};
return false;
});
$(".text-date").humane_dates(); // works here (still, ugly!)
});
});
};
// libs we need (mind the order!) (probably obsolete now)
var myLibs = ["everything"];
// Getting the path/url by looking at our main .js already included in the web-page
var myScriptsDefs = document.getElementsByTagName("script");
for (var i=0; i < myScriptsDefs.length; i++) {
if (myScriptsDefs[i].src && myScriptsDefs[i].src.match(/github-commit-badge.js/)) {
this.path = myScriptsDefs[i].src.replace(/github-commit-badge.js/, '');
};
};
// Loading the libs
for (var i=0; i < myLibs.length; ++i) {
var myScript = document.createElement("script");
myScript.setAttribute("type","text/javascript");
if (document.URL.match(/^http/)) { // only serve the gzipped lib if we're serving from http
myScript.setAttribute("src", this.path + "lib/" + myLibs[i] + ".jsgz");
} else {
myScript.setAttribute("src", this.path + "lib/" + myLibs[i] + ".js");
};
if (i == myLibs.length-1) { // only load our main function after the lib has finished loading
//myScript.setAttribute("onload","mainpage();");
document.getElementsByTagName("body")[0].setAttribute("onload","mainpage();");
};
document.getElementById("gcb-container").appendChild(myScript);
};
// Write the stylesheet into the <head>
myHead = document.getElementsByTagName("head")[0];
myCSS = document.createElement("link");
myCSS.setAttribute("rel","stylesheet");
myCSS.setAttribute("type","text/css");
myCSS.setAttribute("href",this.path + "style.css");
myHead.appendChild(myCSS);