-
Notifications
You must be signed in to change notification settings - Fork 1
/
GithubProcess.js
151 lines (129 loc) · 4.79 KB
/
GithubProcess.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
// ==UserScript==
// @name GitOnDemand
// @version 1.1
// @description Displays TargetProcess infos in the pullrequests.
// @match https://github.com/*/*/pull*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js
// @copyright 2012+, Tristan Foureur
// @website https://github.com/Esya/GithubProcess
// ==/UserScript==
// --- Configuration, edit here ---
// Your targetprocess url
BASE_URL = 'https://FOOBAR.tpondemand.com';
// The prefix for your ids (For example, ID: will match ID:1733 and get story 1733)
ID_PREFIX = 'ID:';
// --- Do not edit below ---
/**
* Strip the HTML tags
* @param {string} input The input text
* @param {mixed} allowed The allowed tags
* @return {string} The tag-stripped text
*/
function strip_tags (input, allowed) {
if(input === null || input === "") return "";
allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
}
/**
* Count the number of testcases
* @param {array} Testcases The testcases, given by TP's api
* @return {int} The number of successful testcases
*/
function countOk(Testcases) {
Ok = 0;
for (var i = Testcases.length - 1; i >= 0; i--) {
if(Testcases[i].LastStatus === true) {
Ok++;
}
}
return Ok;
}
/**
* Displays the info for a Bug/Story
* @param {int} id Id of that bug/story
* @param {object} infos Contains the informations such as description, title, etc
* @param {string} type Either 'Bugs' or 'UserStories'
* @return {void}
*/
function showInfos(id,infos,type) {
if(type == 'Bugs') {
imgSrc = BASE_URL+'/img/Bug.gif';
link = BASE_URL+'/restui/tpview.aspx#bug/'+id;
} else {
imgSrc = BASE_URL+'/img/UserStory.gif';
link = BASE_URL+'/restui/tpview.aspx#userstory/'+id;
}
switch(infos.EntityState.Name) {
case 'Refused':
style_status = 'font-weight: bold; color: red';
break;
case 'Accepted':
style_status = 'font-weight: bold; color: green;';
break;
default:
style_status = 'font-weight: bold;';
break;
}
if(infos.Timespent === 0) style_spent = 'font-weight: bold; color: red;';
else style_spent = '';
if(infos.TimeRemain !== 0) style_remain = 'font-weight: bold; color: red;';
else style_remain = '';
base = '<img style="display: none;" src="http://www.gencodegenes.org/gencode/gfx/toggle.png" class="toggle-desc-'+id+'" data-id="'+id+'"/>'+
"<img src=\""+imgSrc+"\" /> <a href=\""+link+"\">ID:"+id+"</a><br />" +
'<span class="target-desc-'+id+'" style="display: none">'+strip_tags(infos.Description,"")+'<br /></span>'+
"Title : "+infos.Name+" <br />" +
'Status : <span style="'+style_status+'">'+infos.EntityState.Name+"</span><br />" +
'<span style="'+style_spent+'">Spent time : '+infos.TimeSpent+'h </span>/<span style="'+style_remain+'"> Remaining : '+infos.TimeRemain+"h </span><br />";
if(type == 'UserStories') {
ok = countOk(infos.TestCases.Items);
total = infos.TestCases.Items.length;
if(ok == total) style_tests = 'font-weight: bold; color: green;';
else style_tests = 'font-weight: bold;';
if(total > 0) {
base +='<span style="'+style_tests+'">Successful testcases : '+ok+"/"+total+"</span><br />";
} else {
base +='<span style="'+style_tests+'">No testcase!</span><br />';
}
}
before = $('.starting-comment .comment-body').html();
after = before.replace('ID:'+id,base);
$('.starting-comment .comment-body').html(after);
$('.starting-comment .comment-body').find('.toggle-desc-'+id).click(function() {
$('target-desc-'+id).toggle();
});
}
/**
* Tries to fetch the object on TP's api
* @param {int} id Id of the object to fetch
* @param {string} type 'Bugs' or 'UserStories'
* @return {void}
* @todo Do a single request for Bugs
*/
function getId(id,type) {
if(type == "Bugs") {
url = BASE_URL+"/api/v1/"+type+"/"+id+"?format=json&resultInclude=[Id,Description,EntityState,Name,TimeSpent,TimeRemain]";
} else {
url = BASE_URL+"/api/v1/"+type+"/"+id+"?format=json&resultInclude=[Id,Description,EntityState,Name,TimeSpent,TimeRemain,Testcases-count,Testcases[Name,LastStatus]]";
}
//Go for a story first.
GM_xmlhttpRequest({
method: "GET",
url: url,
onload: function(response) {
if(response.status == '404' && type != 'Bugs') {
getId(id,"Bugs");
} else {
json = JSON.parse(response.responseText);
showInfos(id,json,type);
}
}
});
}
ids = $('.starting-comment .comment-body').html().match(/ID:([0-9]*)/g);
for (var i = ids.length - 1; i >= 0; i--) {
getId(ids[i].substr(3),"UserStories");
}