feat(plugins/calendar): add new plugin (#1013) [skip ci]

This commit is contained in:
Simon Lecoq
2022-04-24 03:05:19 +02:00
committed by GitHub
parent 9d1083f263
commit fa66157a24
9 changed files with 198 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
<!--header-->
<!--/header-->
## ➡️ Available options
<!--options-->
<!--/options-->
## Examples workflows
<!--examples-->
<!--/examples-->

View File

@@ -0,0 +1,16 @@
- name: Current year calendar
uses: lowlighter/metrics@latest
with:
filename: metrics.plugin.calendar.svg
token: ${{ secrets.METRICS_TOKEN }}
base: ""
plugin_calendar: yes
- name: Full history calendar
uses: lowlighter/metrics@latest
with:
filename: metrics.plugin.calendar.full.svg
token: ${{ secrets.METRICS_TOKEN }}
base: ""
plugin_calendar: yes
plugin_calendar_limit: 0

View File

@@ -0,0 +1,56 @@
//Setup
export default async function({login, q, data, imports, graphql, queries, account}, {enabled = false} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!enabled)||(!q.calendar))
return null
//Load inputs
let {limit} = imports.metadata.plugins.calendar.inputs({data, account, q})
//Compute boundaries
const end = new Date().getFullYear()
const start = new Date(limit ? end-limit+1 : data.user.createdAt, 0).getFullYear()
//Load contribution calendar
console.debug(`metrics/compute/${login}/plugins > calendar > processing years ${start} to ${end}`)
const calendar = {years:[]}
for (let year = start; year <= end; year++) {
console.debug(`metrics/compute/${login}/plugins > calendar > processing year ${year}`)
const weeks = []
const newyear = new Date(year, 0, 1)
const endyear = (year === end) ? new Date() : new Date(year, 11, 31)
for (let from = new Date(newyear); from < endyear;) {
//Set date range and ensure we start on sundays
let to = new Date(from)
to.setUTCHours(+4 * 7 * 24)
if (to.getUTCDay())
to.setUTCHours(-to.getUTCDay() * 24)
if (to > endyear)
to = endyear
//Ensure that date ranges are not overlapping by setting it to previous day at 23:59:59.999
const dto = new Date(to)
dto.setUTCHours(-1)
dto.setUTCMinutes(59)
dto.setUTCSeconds(59)
dto.setUTCMilliseconds(999)
//Fetch data from api
console.debug(`metrics/compute/${login}/plugins > calendar > loading calendar from "${from.toISOString()}" to "${dto.toISOString()}"`)
const {user:{calendar:{contributionCalendar}}} = await graphql(queries.isocalendar.calendar({login, from:from.toISOString(), to:dto.toISOString()}))
weeks.push(...contributionCalendar.weeks)
//Set next date range start
from = new Date(to)
}
calendar.years.unshift({year, weeks})
}
//Results
return calendar
}
//Handle errors
catch (error) {
throw {error:{message:"An error occured", instance:error}}
}
}

View File

@@ -0,0 +1,22 @@
name: "📆 Calendar"
category: github
description: This plugin displays your commit calendar across several years
examples:
+current year: https://github.com/lowlighter/metrics/blob/examples/metrics.plugin.calendar.svg
full history: https://github.com/lowlighter/metrics/blob/examples/metrics.plugin.calendar.full.svg
supports:
- user
scopes:
- public_access
inputs:
plugin_calendar:
description: Enable calendar plugin
type: boolean
default: no
plugin_calendar_limit:
description: Years to display
type: number
default: 1
zero: disable

View File

@@ -0,0 +1,15 @@
query CalendarDefault {
user(login: "$login") {
calendar:contributionsCollection(from: "$from", to: "$to") {
contributionCalendar {
weeks {
contributionDays {
contributionCount
color
date
}
}
}
}
}
}