Skip to content

Commit d76b7e2

Browse files
committed
Add new metrics and handle errors gracefully
- Handle plugins errors gracefully - Add issue comments and organization metrics
1 parent d744865 commit d76b7e2

File tree

9 files changed

+205
-76
lines changed

9 files changed

+205
-76
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,10 @@ Assuming your username is `my-github-user`, you can embed your metrics in your p
138138
<summary>💬 Restrictions and fair use</summary>
139139

140140
Since GitHub API has rate limitations and to avoid abuse, the shared instance has the following limitations :
141-
* Images are cached for 2 hours
141+
* Images are cached for 1 hour
142142
* Your generated metrics **won't** be updated during this amount of time
143-
* If you enable or disable plugins by changing the url parameters, you'll need to wait before changes are applied
144-
* You're limited to 3 requests per 2 hours
145-
* Restriction **does not** apply to already cached users metrics, including your own
143+
* If you enable or disable plugins in url parameters, you'll need to wait for cache expiration before these changes are applied
144+
* A rate limiter prevents new metrics generation when reached, but it **does not** affect already cached users metrics, including your own
146145
* Most of plugins are not available
147146
* PageSpeed plugin can be enabled by passing `?pagespeed=1`, but metrics generation can take up some time
148147

action/dist/index.js

Lines changed: 60 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/plugins/lines/index.mjs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,33 @@
1616
console.debug(`metrics/plugins/lines/${login} > started`)
1717

1818
//Plugin execution
19-
pending.push(new Promise(async solve => {
20-
//Get contributors stats from repositories
21-
const lines = {added:0, deleted:0}
22-
const response = await Promise.all(repositories.map(async repo => await rest.repos.getContributorsStats({owner:login, repo})))
23-
//Compute changed lines
24-
response.map(({data:repository}) => {
25-
//Check if data are available
26-
if (!Array.isArray(repository))
27-
return
28-
//Extract author
29-
const [contributor] = repository.filter(({author}) => author.login === login)
30-
//Compute editions
31-
if (contributor)
32-
contributor.weeks.forEach(({a, d}) => (lines.added += a, lines.deleted += d))
33-
})
34-
//Format values
35-
lines.added = format(lines.added)
36-
lines.deleted = format(lines.deleted)
37-
//Save results
38-
computed.plugins.lines = {...lines}
39-
console.debug(`metrics/plugins/lines/${login} > ${JSON.stringify(computed.plugins.lines)}`)
40-
solve()
19+
pending.push(new Promise(async (solve, reject) => {
20+
try {
21+
//Get contributors stats from repositories
22+
const lines = {added:0, deleted:0}
23+
const response = await Promise.all(repositories.map(async repo => await rest.repos.getContributorsStats({owner:login, repo})))
24+
//Compute changed lines
25+
response.map(({data:repository}) => {
26+
//Check if data are available
27+
if (!Array.isArray(repository))
28+
return
29+
//Extract author
30+
const [contributor] = repository.filter(({author}) => author.login === login)
31+
//Compute editions
32+
if (contributor)
33+
contributor.weeks.forEach(({a, d}) => (lines.added += a, lines.deleted += d))
34+
})
35+
//Format values
36+
lines.added = format(lines.added)
37+
lines.deleted = format(lines.deleted)
38+
//Save results
39+
computed.plugins.lines = {...lines}
40+
console.debug(`metrics/plugins/lines/${login} > ${JSON.stringify(computed.plugins.lines)}`)
41+
solve()
42+
}
43+
catch (error) {
44+
reject(error)
45+
}
4146
}))
4247
}
4348

src/plugins/pagespeed/index.mjs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,32 @@
1515
console.debug(`metrics/plugins/pagespeed/${login} > started`)
1616

1717
//Plugin execution
18-
pending.push(new Promise(async solve => {
19-
//Format url if needed
20-
if (!/^https?:[/][/]/.test(url))
21-
url = `https://${url}`
22-
//Load scores from API
23-
const scores = new Map()
24-
await Promise.all(["performance", "accessibility", "best-practices", "seo"].map(async category => {
25-
const {score, title} = (await axios.get(`https://www.googleapis.com/pagespeedonline/v5/runPagespeed?category=${category}&url=${url}&key=${token}`)).data.lighthouseResult.categories[category]
26-
scores.set(category, {score, title})
27-
}))
28-
//Save results
29-
computed.plugins.pagespeed = {url, scores:[scores.get("performance"), scores.get("accessibility"), scores.get("best-practices"), scores.get("seo")]}
30-
console.debug(`metrics/plugins/pagespeed/${login} > ${JSON.stringify(computed.plugins.pagespeed)}`)
31-
solve()
18+
pending.push(new Promise(async (solve, reject) => {
19+
try {
20+
//Format url if needed
21+
if (!/^https?:[/][/]/.test(url))
22+
url = `https://${url}`
23+
//Load scores from API
24+
const scores = new Map()
25+
await Promise.all(["performance", "accessibility", "best-practices", "seo"].map(async category => {
26+
const {score, title} = (await axios.get(`https://www.googleapis.com/pagespeedonline/v5/runPagespeed?category=${category}&url=${url}&key=${token}`)).data.lighthouseResult.categories[category]
27+
scores.set(category, {score, title})
28+
}))
29+
//Save results
30+
computed.plugins.pagespeed = {url, scores:[scores.get("performance"), scores.get("accessibility"), scores.get("best-practices"), scores.get("seo")]}
31+
console.debug(`metrics/plugins/pagespeed/${login} > ${JSON.stringify(computed.plugins.pagespeed)}`)
32+
solve()
33+
}
34+
catch (error) {
35+
//Thrown when token is incorrect
36+
if ((error.response)&&(error.response.status)) {
37+
computed.plugins.pagespeed = {url, error:`PageSpeed token error (code ${error.response.status})`}
38+
console.debug(`metrics/plugins/traffic/${login} > ${error.response.status}`)
39+
solve()
40+
return
41+
}
42+
console.log(error)
43+
reject(error)
44+
}
3245
}))
3346
}

src/plugins/traffic/index.mjs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,30 @@
1616
console.debug(`metrics/plugins/traffic/${login} > started`)
1717

1818
//Plugin execution
19-
pending.push(new Promise(async solve => {
20-
//Get views stats from repositories
21-
const views = {count:0, uniques:0}
22-
const response = await Promise.all(repositories.map(async repo => await rest.repos.getViews({owner:login, repo})))
23-
//Compute views
24-
response.filter(({data}) => data).map(({data:{count, uniques}}) => (views.count += count, views.uniques += uniques))
25-
//Format values
26-
views.count = format(views.count)
27-
views.uniques = format(views.uniques)
28-
//Save results
29-
computed.plugins.traffic = {views}
30-
console.debug(`metrics/plugins/traffic/${login} > ${JSON.stringify(computed.plugins.traffic)}`)
31-
solve()
19+
pending.push(new Promise(async (solve, reject) => {
20+
try {
21+
//Get views stats from repositories
22+
const views = {count:0, uniques:0}
23+
const response = await Promise.all(repositories.map(async repo => await rest.repos.getViews({owner:login, repo})))
24+
//Compute views
25+
response.filter(({data}) => data).map(({data:{count, uniques}}) => (views.count += count, views.uniques += uniques))
26+
//Format values
27+
views.count = format(views.count)
28+
views.uniques = format(views.uniques)
29+
//Save results
30+
computed.plugins.traffic = {views}
31+
console.debug(`metrics/plugins/traffic/${login} > ${JSON.stringify(computed.plugins.traffic)}`)
32+
solve()
33+
}
34+
catch (error) {
35+
//Thrown when token has unsufficient permissions
36+
if (error.status === 403) {
37+
computed.plugins.traffic = {error:`Insufficient token rights`}
38+
console.debug(`metrics/plugins/traffic/${login} > ${error.status}`)
39+
solve()
40+
return
41+
}
42+
reject(error)
43+
}
3244
}))
3345
}

src/query.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,11 @@ query Metrics {
8686
following {
8787
totalCount
8888
}
89+
issueComments {
90+
totalCount
91+
}
92+
organizations {
93+
totalCount
94+
}
8995
}
9096
}

src/style.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
margin: 0 8px;
4141
fill: #959da5;
4242
}
43+
.field.error {
44+
color: #cb2431;
45+
}
46+
.field.error svg {
47+
fill: #cb2431;
48+
49+
}
4350

4451
/* Displays */
4552
.row {

0 commit comments

Comments
 (0)