forked from Technigo/project-github-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
154 lines (140 loc) · 4.41 KB
/
script.js
File metadata and controls
154 lines (140 loc) · 4.41 KB
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
import "./style.css";
import MainComp from "./components/main";
import HeaderComp from "./components/header";
import makeChart from "./technigoChart";
import LoadingStatusComp from "./components/spinner";
const root = document.getElementById("root");
const TOKEN = process.env.GITHUB_AUTH;
const USERNAME = "jiiinCho";
const spinner = LoadingStatusComp();
async function fetchGithubData(path) {
const myHeaders = new Headers();
myHeaders.append("Authorization", `Basic ${TOKEN}`);
const requestOptions = {
method: "GET",
headers: myHeaders,
redirect: "follow",
};
const response = await fetch(
`https://api.github.com/${path}`,
requestOptions
);
if (response.status === 403) {
throw new Error(
"API rate limit excceded, please visit again after an hour :)"
);
}
return await response.json();
}
async function init() {
//start by displaying loading state
root.appendChild(spinner);
try {
const allRepository = await fetchGithubData(`users/${USERNAME}/repos`);
//user info
const { avatar_url, html_url } = allRepository[0].owner;
//step 1. filter technigo projects
let projectNames = [];
let technigoRepos = [];
allRepository.forEach((repo) => {
if (repo.name.startsWith("project-")) {
technigoRepos.push(repo);
projectNames.push(repo.name);
}
});
const completedProjects = technigoRepos.length;
// step 2. fetch all required data
const baseData = await fetchAll(projectNames);
// step 4. sort by last commit date
baseData.sort(function (a, b) {
const currDate = new Date(a.latestCommitDate);
const nextDate = new Date(b.latestCommitDate);
return nextDate - currDate;
});
// step 5. create and inject html elements
const header = HeaderComp(avatar_url);
const main = MainComp(avatar_url, "jiiin✨", USERNAME, html_url, baseData);
return { header, main, completedProjects };
} catch (e) {
//error handling
console.error(e);
root.innerHTML = `
<div class="error-container">
<span class="error-message">${e.message}</span>
</div> `;
}
}
function getParentData(parentInfo) {
const name = parentInfo.name;
const defaultBranch = parentInfo.default_branch;
const repoURL = parentInfo.html_url;
const language = parentInfo.parent.language;
const forkedFrom = parentInfo.parent.full_name;
const forksCount = parentInfo.parent.forks_count;
const forksURL = parentInfo.parent.html_url;
return {
name,
defaultBranch,
repoURL,
language,
forkedFrom,
forksCount,
forksURL,
};
}
function getCommitInfo(commitInfo) {
const allCommitCount = commitInfo.length;
const latestCommit = commitInfo[0];
const latestCommitAuthor = latestCommit.commit.author.name;
const latestCommitDate = new Date(latestCommit.commit.author.date);
const latestCommitMessage = latestCommit.commit.message;
const latestCommitUrl = latestCommit.html_url;
return {
allCommitCount,
latestCommitAuthor,
latestCommitDate,
latestCommitMessage,
latestCommitUrl,
};
}
function getPullInfo(pullInfo) {
const myPull = pullInfo.find((pull) => pull.user.login === USERNAME);
const pullRequestTitle = myPull ? myPull.title : "";
const pullRequestMessage = myPull ? myPull.body : "";
const pullRequestUpdatedAt = myPull ? new Date(myPull.updated_at) : "";
const pullRequestURL = myPull ? myPull.html_url : "";
return {
pullRequestTitle,
pullRequestMessage,
pullRequestUpdatedAt,
pullRequestURL,
};
}
const fetchAll = async (projectNames) => {
return await Promise.all(
projectNames.map(async (projectName) => {
const parentInfo = await fetchGithubData(
`repos/${USERNAME}/${projectName}`
);
const commitInfo = await fetchGithubData(
`repos/${USERNAME}/${projectName}/commits`
);
const pullInfo = await fetchGithubData(
`repos/Technigo/${projectName}/pulls?per_page=60`
);
//stpe 3. process data and create data object
const parent = getParentData(parentInfo);
const commit = getCommitInfo(commitInfo);
const pull = getPullInfo(pullInfo);
return { ...parent, ...commit, ...pull };
})
);
};
init().then((result) => {
const { header, main, completedProjects } = result;
root.removeChild(spinner);
root.appendChild(header);
root.appendChild(main);
makeChart(completedProjects, "chart");
console.log("app is running!");
});