1
|
|
|
import * as core from '@actions/core'; |
2
|
|
|
import {HttpClient} from '@actions/http-client'; |
3
|
|
|
import {Octokit} from '@octokit/rest'; |
4
|
|
|
import {config} from 'dotenv'; |
5
|
|
|
import {resolve} from 'path'; |
6
|
|
|
import formatLine from './formatLine'; |
7
|
|
|
|
8
|
|
|
config({path: resolve(__dirname, '../.env')}); |
9
|
|
|
|
10
|
|
|
const GH_TOKEN = core.getInput('GH_TOKEN', {required: true}); |
11
|
|
|
const WAKA_API_KEY = core.getInput('WAKA_API_KEY', {required: true}); |
12
|
|
|
// const GIST_ID = core.getInput('GIST_ID', {required: true}); |
13
|
|
|
const GIST_ID = 'test-on-github'; |
14
|
|
|
const MAX_RESULT = Number(core.getInput('MAX_RESULT', {required: true})); |
15
|
|
|
const DATE_RANGE = core.getInput('DATE_RANGE', {required: false}); |
16
|
|
|
const PRINT_SUMMARY = core.getBooleanInput('PRINT_SUMMARY', {required: true}); |
17
|
|
|
const USER_AGENT = 'WakaTime-Gist/1.3 +https://github.com/marketplace/actions/wakatime-gist'; |
18
|
|
|
const ACTION_URL = 'https://github.com/marketplace/actions/wakatime-gist'; |
19
|
|
|
|
20
|
|
|
const updateDate = new Date().toLocaleDateString('en-us', {day: 'numeric', year: 'numeric', month: 'short'}); |
21
|
|
|
const summaryTable: any[] = [[{data: 'Action', header: true}, {data: 'Result', header: true}]]; |
22
|
|
|
|
23
|
|
|
let range: string = DATE_RANGE; |
24
|
|
|
if (!['last_7_days', 'last_30_days', 'last_6_months', 'last_year'].includes(range)) range = 'last_7_days'; |
25
|
|
|
|
26
|
|
|
let title: string = 'Latest'; |
27
|
|
|
if (range === 'last_7_days') title = 'Weekly'; |
28
|
|
|
if (range === 'last_30_days') title = 'Monthly'; |
29
|
|
|
title = title + ' statistics [update ' + updateDate + ']'; |
30
|
|
|
|
31
|
|
|
(async () => { |
32
|
|
|
/** |
33
|
|
|
* Get statistics |
34
|
|
|
*/ |
35
|
|
|
const httpClient = new HttpClient(USER_AGENT); |
36
|
|
|
const response = await httpClient.getJson('https://wakatime.com/api/v1/users/current/stats/' + range, |
37
|
|
|
{Authorization: `Basic ${Buffer.from(WAKA_API_KEY).toString('base64')}`}) |
38
|
|
|
.catch(error => actionFail(error.message)); |
39
|
|
|
|
40
|
|
|
// @ts-ignore |
41
|
|
|
const languages: any[] = response.result.data.languages; |
42
|
|
|
if (languages) { |
43
|
|
|
summaryTable.push(['Statistics received', '✔']); |
44
|
|
|
} else { |
45
|
|
|
return actionFail('Empty response from wakatime.com'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Formatting |
50
|
|
|
*/ |
51
|
|
|
let otherTotalSeconds = 0; |
52
|
|
|
let otherPercent = 0; |
53
|
|
|
const lines = languages.reduce((prev: any[], cur: any) => { |
54
|
|
|
const {name, percent, total_seconds} = cur; |
55
|
|
|
const line = formatLine(name, total_seconds, percent); |
56
|
|
|
|
57
|
|
|
if (name == 'Other' || prev.length >= MAX_RESULT - 1) { |
58
|
|
|
otherTotalSeconds += total_seconds; |
59
|
|
|
otherPercent += percent; |
60
|
|
|
return prev; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return [...prev, line]; |
64
|
|
|
}, []); |
65
|
|
|
|
66
|
|
|
lines.push(formatLine('Other lang', otherTotalSeconds, otherPercent)); |
67
|
|
|
if (lines.length === 0) return actionInfo('No statistics for the last time period. Gist not updated'); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get gist filename |
71
|
|
|
*/ |
72
|
|
|
const octokit = new Octokit({auth: `token ${GH_TOKEN}`}); |
73
|
|
|
const gist = await octokit.gists.get({gist_id: GIST_ID}) |
74
|
|
|
.catch(error => actionFail('Gist ' + error.message)); |
75
|
|
|
if (!gist) return; |
76
|
|
|
|
77
|
|
|
const filename = Object.keys(gist.data.files || {})[0]; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Update gist |
81
|
|
|
*/ |
82
|
|
|
await octokit.gists.update({ |
83
|
|
|
gist_id: GIST_ID, |
84
|
|
|
files: { |
85
|
|
|
[filename]: { |
86
|
|
|
filename: title, |
87
|
|
|
content: lines.join('\n'), |
88
|
|
|
}, |
89
|
|
|
}, |
90
|
|
|
}).catch(error => actionFail('Gist ' + error.message)); |
91
|
|
|
|
92
|
|
|
summaryTable.push(['Gist updated', '✔']); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Print summary |
96
|
|
|
*/ |
97
|
|
|
await actionSuccess(); |
98
|
|
|
})(); |
99
|
|
|
|
100
|
|
|
async function actionSuccess() { |
101
|
|
|
const summary = core.summary |
102
|
|
|
.addHeading('Results') |
103
|
|
|
.addTable(summaryTable) |
104
|
|
|
.addBreak() |
105
|
|
|
.addLink('wakatime-gist', ACTION_URL); |
106
|
|
|
|
107
|
|
|
await printSummary(summary); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
async function actionInfo(mess: string) { |
111
|
|
|
const summary = core.summary |
112
|
|
|
.addHeading('Results') |
113
|
|
|
.addRaw(mess) |
114
|
|
|
.addBreak() |
115
|
|
|
.addLink('wakatime-gist', ACTION_URL); |
116
|
|
|
|
117
|
|
|
await printSummary(summary); |
118
|
|
|
core.notice(mess); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
async function actionFail(mess: string) { |
122
|
|
|
mess = 'Action failed with error: ' + mess; |
123
|
|
|
|
124
|
|
|
const summary = core.summary |
125
|
|
|
.addHeading('Results') |
126
|
|
|
.addRaw(mess) |
127
|
|
|
.addBreak() |
128
|
|
|
.addLink('Check README', ACTION_URL); |
129
|
|
|
|
130
|
|
|
await printSummary(summary); |
131
|
|
|
core.setFailed(mess); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
async function printSummary(summary: typeof core.summary) { |
135
|
|
|
if (PRINT_SUMMARY) { |
136
|
|
|
await summary.write(); |
137
|
|
|
} else { |
138
|
|
|
console.log(summary.stringify()); |
139
|
|
|
} |
140
|
|
|
} |