-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
184 lines (159 loc) · 6.22 KB
/
main.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
const apiVersion = "api-version=7.1";
const apiTop = 10000;
var organization = "";
var project = "";
var minTime = "";
var personalAccessToken = "";
var authHeader = "";
var headers = new Headers();
var contributionChartData = {};
var contributionChart = null;
const fetchData = async (url) => {
const response = await fetch(url, { headers });
return response.json();
};
const getRepos = async () => {
const url = `https://dev.azure.com/${organization}/${project}/_apis/git/repositories?${apiVersion}`;
const data = await fetchData(url);
return data.value || [];
};
const getCommits = async (repoId, page = 1) => {
const url = `https://dev.azure.com/${organization}/${project}/_apis/git/repositories/${repoId}/commits?searchCriteria.fromDate=${minTime}&$top=${apiTop}&$skip=${(page - 1) * apiTop}&${apiVersion}`;
const data = await fetchData(url);
return data;
};
const getPullRequests = async (repoId, page = 1) => {
const url = `https://dev.azure.com/${organization}/${project}/_apis/git/repositories/${repoId}/pullrequests?searchCriteria.status=completed&searchCriteria.minTime=${minTime}&$top=${apiTop}&$skip=${(page - 1) * apiTop}&${apiVersion}`;
const data = await fetchData(url);
return data;
};
const createUser = function (user) {
if (contributionChartData[user] === undefined) {
contributionChartData[user] = {
"commits": 0,
"pullRequestsCreated": 0,
"pullRequestsReviewed": 0
}
}
}
const aggregateDataByUser = async () => {
const repos = await getRepos();
for (const repo of repos) {
if (!repo.isDisabled) {
let page = 1;
let commits;
do {
commits = await getCommits(repo.id, page);
commits.value.forEach(commit => {
const author = commit.author.email;
createUser(author);
contributionChartData[author].commits = (contributionChartData[author].commits || 0) + 1;
});
page++;
} while (commits.value.length === apiTop); // Continue if page is full (potentially more pages)
page = 1;
let pullRequests;
do {
pullRequests = await getPullRequests(repo.id, page);
pullRequests.value.forEach(pr => {
const creator = pr.createdBy.uniqueName;
createUser(creator);
for (var i = 0; i < pr.reviewers.length; i++) {
const reviewer = pr.reviewers[i].uniqueName;
createUser(reviewer);
contributionChartData[reviewer].pullRequestsReviewed = (contributionChartData[reviewer].pullRequestsReviewed || 0) + 1;
}
contributionChartData[creator].pullRequestsCreated = (contributionChartData[creator].pullRequestsCreated || 0) + 1;
});
page++;
} while (pullRequests.value.length === apiTop); // Continue if page is full (potentially more pages)
}
}
console.log("Contributions graph data:", contributionChartData);
const labels = Object.keys(contributionChartData);
const commits = Object.values(contributionChartData).map(item => item.commits);
const pullRequestsCreated = Object.values(contributionChartData).map(item => item.pullRequestsCreated);
const pullRequestsReviewed = Object.values(contributionChartData).map(item => item.pullRequestsReviewed);
hideLoading();
destroyChart();
const ctx = document.getElementById('stackedBarChart').getContext('2d');
contributionChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [
{
label: 'Commits',
data: commits,
backgroundColor: 'rgba(75, 192, 192, 0.5)',
},
{
label: 'Pull Requests Created',
data: pullRequestsCreated,
backgroundColor: 'rgba(153, 102, 255, 0.5)',
},
{
label: 'Pull Requests Reviewed',
data: pullRequestsReviewed,
backgroundColor: 'rgba(255, 159, 64, 0.5)',
}
]
},
options: {
plugins: {
legend: {
position: 'top',
},
},
responsive: true,
scales: {
x: {
stacked: true,
},
y: {
stacked: true
}
}
}
});
};
const scanButtonClicked = function () {
project = document.getElementById("projectName").value;
personalAccessToken = document.getElementById("PATtoken").value;
organization = document.getElementById("organization").value;
minTime = document.getElementById("startDate").value;
if (organization !== "" && project !== "" && personalAccessToken !== "" && minTime != "") {
authHeader = 'Basic ' + btoa(':' + personalAccessToken);
headers = new Headers({
"Content-Type": "application/json",
"Authorization": authHeader
});
destroyChart();
showLoading();
aggregateDataByUser().catch(error => {
hideLoading();
console.error('Error:', error);
alert("Please check if the given Azure DevOps Project Name and Azure Personal Access Token are corrent and valid.");
});
} else {
alert("All the inputs are mandatory, please check if you provided correct values.");
}
}
const showLoading = function () {
var x = document.getElementById("loading");
x.style.display = "block";
}
const hideLoading = function () {
var x = document.getElementById("loading");
x.style.display = "none";
}
const destroyChart = function () {
contributionChartData = {};
const ctx = document.getElementById('stackedBarChart').getContext('2d');
if (contributionChart) {
contributionChart.destroy();
}
}
window.onload = (event) => {
document.getElementById("scanButton").addEventListener("click", (scanButtonClicked));
};