-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.js
96 lines (84 loc) · 2.48 KB
/
action.js
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
const core = require('@actions/core');
const { Octokit } = require("@octokit/core");
const fs = require('fs');
const dlog = require("./dlog.js");
const getPersent = (file) => {
const data = fs.readFileSync(file, 'utf8');
const json = JSON.parse(data);
json.sources.map(source => source.totalCount )
const totalCount = json.sources.map(s => s.totalCount).reduce((p, c) => p + c, 0);
const undocumentedCount = json.sources.map(s => s.undocumented.length).reduce((p, c) => p + c, 0);
let persent = ((totalCount - undocumentedCount) / totalCount) * 100;
return persent;
};
const updateGist = async (token, description, file, content) => {
const octokit = new Octokit({
auth: token
});
// Get gists
const payload = await octokit.request('GET /gists', {});
// Find gist by description
let item = payload.data.find(e => e.description === description);
if(item !== undefined) {
dlog.debug("Update existing gist...");
const gist_id = item.id;
await octokit.request(`PATCH /gists/${gist_id}`, {
gist_id: gist_id,
description: description,
files: {
[file]: {
content: content
}
}
});
}
else {
dlog.debug("Create new gist...")
await octokit.request('POST /gists', {
description: description,
public: true,
files: {
[file]: {
content: content
}
}
});
}
dlog.debug('DONE');
}
const badgeColor = (persent) => {
if(persent <= 50) {
return 'red';
}
if(persent < 70) {
return 'yellow';
}
if(persent < 80) {
return 'yellowgreen';
}
if(persent < 90) {
return 'green';
}
return 'brightgreen'
}
try {
const file = process.env.FILE;
const token = process.env.TOKEN;
const repository = process.env.REPOSITORY;
if(file === undefined || token === undefined || repository === undefined) {
throw new Error('No params.');
}
dlog.info('File:', file);
dlog.info('Token:', token.substring(20));
dlog.info('Repository:', repository);
const persent = getPersent(file);
dlog.debug(`Coverage: ${persent.toFixed()}%`);
const content = `{"schemaVersion": 1,"label":"swift-doc-coverage","message":"${persent.toFixed()}%","color":"${badgeColor(persent)}"}`;
const outputFile = repository.substring(repository.lastIndexOf('/') + 1) + ".json";
dlog.debug('Output file:', outputFile);
updateGist(token, 'Swift Doc Coverage', outputFile, content);
}
catch (error) {
dlog.error(error.message);
core.setFailed(error.message);
}