Add github auto update

This commit is contained in:
2021-11-10 19:01:09 +01:00
parent 6ec19a5e2e
commit 573efea8b0
6 changed files with 326 additions and 129 deletions

View File

@@ -2,6 +2,7 @@ import Logger from '@ioc:Adonis/Core/Logger'
import Env from '@ioc:Adonis/Core/Env'
import axios from 'axios'
import DevelopmentHour from 'App/Models/DevelopmentHour'
import { UpdateGithubReadme } from 'App/Utils/UpdateGithubReadme'
const MS = 1000 * 5 * 60 // 5 min
let taskId
@@ -32,6 +33,8 @@ async function getDevelopmentHours(): Promise<void> {
seconds: data.seconds,
})
}
await UpdateGithubReadme()
}
}

View File

@@ -44,9 +44,9 @@ export async function getDevelopmentHours(start: string, end: string): Promise<T
development_time.forEach(item => total += item.seconds)
return {
total_hours: Math.floor(total / 3600),
total_minutes: Math.floor(total / 60),
total_seconds: Math.floor(total),
total_hours: Number((total / 3600).toFixed(2)),
total_minutes: Number((total / 60).toFixed(2)),
total_seconds: Number(total.toFixed(2)),
}
}

View File

@@ -0,0 +1,52 @@
import axios from 'axios'
import Env from '@ioc:Adonis/Core/Env'
import Logger from '@ioc:Adonis/Core/Logger'
import {
fetchDailyStatistics,
fetchMonthlyStatistics,
fetchStatistics,
fetchWeeklyStatistics,
} from 'App/Utils/StatsUtils'
export async function UpdateGithubReadme(): Promise<void> {
const daily_stats = await fetchDailyStatistics()
const weekly_stats = await fetchWeeklyStatistics()
const monthly_stats = await fetchMonthlyStatistics()
const total_stats = await fetchStatistics()
const response = await axios.get<{ content: string; sha: string }>(`https://api.github.com/repos/${Env.get('GITHUB_USERNAME')}/${Env.get('GITHUB_USERNAME')}/readme`, {
headers: {
authorization: `Bearer ${Env.get('GITHUB_TOKEN')}`,
},
})
if (response.status === 200) {
const content = Buffer.from(response.data.content, 'base64').toString()
const old_table = content.split('<!-- Start Table -->')[1].split('<!-- End Table -->')[0]
const new_table = `
| Title | Daily | Weekly | Monthly | Total |
| :------------------------------------------ | ----------: | ----------: | -----------: | -----------: |
| :hourglass_flowing_sand: Hours Spent Coding | **${daily_stats.development_time.total_hours}hrs** | **${weekly_stats.development_time.total_hours}hrs** | **${monthly_stats.development_time.total_hours}hrs** | **${total_stats.development_time.total_hours}hrs** |
| :computer: Terminal Commands | **${daily_stats.commands_ran}** | **${weekly_stats.commands_ran}** | **${monthly_stats.commands_ran}** | **${total_stats.commands_ran}** |
| :hammer: Docker Builds | **${daily_stats.builds_ran}** | **${weekly_stats.builds_ran}** | **${monthly_stats.builds_ran}** | **${total_stats.builds_ran}** |`
const new_content = content.replace(old_table, new_table)
const update = await axios.put(`https://api.github.com/repos/${Env.get('GITHUB_USERNAME')}/${Env.get('GITHUB_USERNAME')}/contents/README.md`,
{
message: '📊 Updated Statistics - Athena',
content: Buffer.from(new_content, 'utf8').toString('base64'),
sha: response.data.sha,
author: {
name: 'Athena - API Automation',
email: 'api@arthurdanjou.fr',
},
},
{
headers: {
authorization: `Bearer ${Env.get('GITHUB_TOKEN')}`,
},
})
if (update.status !== 200)
Logger.error('Error with updating statistics')
}
}