/
github.js
177 lines (137 loc) · 5.84 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
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
function addRepoPageItems(){
if( $( "h1 svg.octicon-repo ~ strong[itemprop='name'] + a.cci, h1 svg.octicon-lock ~ strong[itemprop='name'] + a.cci" ).length == 0 ){
// Start with a grey status indicator until we determine the project's build status
$("h1 svg.octicon-repo ~ strong[itemprop='name'], h1 svg.octicon-lock ~ strong[itemprop='name']").after('<a class="cci status-dot" href="" title="This project is not built on CircleCI"><span class="default-build-status"></span></a>');
}
if( $( ".cci.btn" ).length == 0 ){
// start with a follow button until we determine if we're actually following this project
$("ul.pagehead-actions").prepend(genPageAction);
$( ".cci.btn" ).click(function( e ){
if( apiToken.length == 0 ){
alert( "Your CircleCI API token must be set in Pointless' Options page to use this feature." );
return
}
const followConfirm = "Are you sure you want to follow this project on CircleCI?";
const unFollowConfirm = "Are you sure you want to stop following this project? If you are the last person following, the project will stop building. Continue?";
const firstFollowConfirm = "This will start building this project on CircleCI with you following the project. Are you sure?";
const cantFollowAlert = "You do not have the proper permissions to follow this project.";
if( project.following ){
if( confirm( unFollowConfirm ) == true ){
$.post( apiURL + "project/github/" + project.org + "/" + project.repo + "/unfollow?circle-token=" + apiToken, function( data ){
$( ".cci.btn span" ).text( "Follow" );
$( ".cci.btn" ).attr( "title", "Follow this project on CircleCI." );
alert( "You have successfully unfollowed " + project.org + "/" + project.repo + "." );
});
}
} else{
if( project.writable ){
if( confirm( followConfirm ) == true ){
$.post( apiURL + "project/github/" + project.org + "/" + project.repo + "/follow?circle-token=" + apiToken, function( data ){
$( ".cci.btn span" ).text( "Unfollow" );
$( ".cci.btn" ).attr( "title", "Unfollow this project on CircleCI." );
alert( "You have successfully followed " + project.org + "/" + project.repo + "." );
});
}
} else{
alert( cantFollowAlert );
}
}
e.preventDefault();
});
}
}
function genPageAction(){
return `
<li>
<a class="cci btn btn-sm" href="#follow-ci" title="Follow this project on CircleCI">
<img class="circleci-icon" src="${ chrome.runtime.getURL("circleci-icon.svg")}" /> <span>Follow</span>
</a>
</li>`;
}
function getAPIToken(){
port = chrome.runtime.connect( {name: "github"} );
port.postMessage( {request: "api-token"} );
port.onMessage.addListener( function( msg ){
if( msg.request == "api-token"){
apiToken = msg.response;
initGitHub();
}
});
}
function initGitHub(){
addRepoPageItems();
$.getJSON( apiURL + "project/github/" + project.org + "/" + project.repo + "/settings?circle-token=" + apiToken, function( data, httpStatus ){
project.defaultBranch = data.default_branch;
project.following = data.following;
// The below line is broken. Temp removing it.
//project.writable = data.scopes.includes("write-settings");
project.building = data.has_usable_key;
project.foss = data.oss;
// Get build status if project is building on CircleCI
if( project.building ){
// Choose branch based on contex
var curBranch = "";
// if we're on the main page, viewing a non-default branch
if( window.location.pathname.split("/")[3] == "tree" ){
curBranch = window.location.pathname.split("/")[4];
// base branch detection for PRs is causing problems with GitHub's new UI. For now, we'll disable and just use the default.
//}else if( window.location.pathname.split("/")[3] == "pull" ){
// curBranch = $( "div.TableObject-item--primary a.author + span.commit-ref span.css-truncate-target" ).text();
}else{
curBranch = project.defaultBranch;
}
$.getJSON( apiURL + "project/github/" + project.org + "/" + project.repo + "/tree/" + curBranch + "?circle-token=" + apiToken, function( data ){
lastBuild = data[0].status;
// check build status of last build that completed
var classStatus;
switch( lastBuild ){
case "success":
case "fixed":
classStatus = "passed";
break;
case "failed":
case "timedout":
classStatus = "failed";
break;
case "running":
classStatus = "running";
break;
default:
classStatus = "error";
}
$( "h1 svg.octicon-repo ~ a.cci span, h1 svg.octicon-lock ~ a.cci span" ).addClass( classStatus );
$( "h1 svg.octicon-repo ~ a.cci, h1 svg.octicon-lock ~ a.cci" ).attr({
"href": "https://app.circleci.com/pipelines/github/" + project.org + "/" + project.repo + "?branch=" + curBranch,
"title": curBranch + " branch build status: " + classStatus + ". Click to visit project on CircleCI."
});
});
}
// update CircleCI pagehead-action with following status if needed
if( project.following ){
$( ".cci.btn span" ).text( "Unfollow" );
$( ".cci.btn" ).attr( "title", "Unfollow this project on CircleCI." );
}else if( !project.writable && !project.foss ){
$( ".cci.btn" ).attr( "title", "You do not have permission to follow this project on CircleCI." );
}else{
$( ".cci.btn" ).attr( "title", "Follow this project on CircleCI." );
}
})
}
apiURL = "https://circleci.com/api/v1.1/";
project = {
org: window.location.pathname.split("/")[1],
repo: window.location.pathname.split("/")[2],
defaultBranch: "master", // asume master until told otherwise
following: false,
writable: false, // whether the user has write permission to the repo
building: true, // whether the project is building on CircleCI
foss: false
};
var port;
var apiToken;
$(document).ready(function(){
getAPIToken();
$(document).on('pjax:end', function (t) {
initGitHub();
});
});