Skip to content

Commit 8de95b7

Browse files
committed
Uploading to Gist, added DLog.js logger
1 parent 1becc7d commit 8de95b7

File tree

5 files changed

+373
-165
lines changed

5 files changed

+373
-165
lines changed

action.js

Lines changed: 87 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,96 @@
11
const core = require('@actions/core');
2-
const github = require('@actions/github');
3-
//const http = require('https');
2+
const { Octokit } = require("@octokit/core");
43
const fs = require('fs');
4+
const dlog = require("./dlog.js");
55

6-
console.log("GH Token: " + process.env.TOKEN);
6+
const getPersent = (file) => {
7+
const data = fs.readFileSync(file, 'utf8');
8+
const json = JSON.parse(data);
9+
json.sources.map(source => source.totalCount )
10+
const totalCount = json.sources.map(s => s.totalCount).reduce((p, c) => p + c, 0);
11+
const undocumentedCount = json.sources.map(s => s.undocumented.length).reduce((p, c) => p + c, 0);
12+
let persent = ((totalCount - undocumentedCount) / totalCount) * 100;
13+
return persent;
14+
};
715

8-
try {
9-
const data = fs.readFileSync('result.json', 'utf8');
10-
console.log(data);
16+
const updateGist = async (token, description, file, content) => {
17+
const octokit = new Octokit({
18+
auth: token
19+
});
20+
21+
// Get gists
22+
const payload = await octokit.request('GET /gists', {});
23+
24+
// Find gist by description
25+
let item = payload.data.find(e => e.description === description);
26+
if(item !== undefined) {
27+
dlog.debug("Update existing gist...");
28+
29+
const gist_id = item.id;
30+
await octokit.request(`PATCH /gists/${gist_id}`, {
31+
gist_id: gist_id,
32+
description: description,
33+
files: {
34+
[file]: {
35+
content: content
36+
}
37+
}
38+
});
39+
}
40+
else {
41+
dlog.debug("Create new gist...")
42+
43+
await octokit.request('POST /gists', {
44+
description: description,
45+
public: true,
46+
files: {
47+
[file]: {
48+
content: content
49+
}
50+
}
51+
});
52+
}
53+
dlog.debug('DONE');
1154
}
12-
catch (error) {
13-
core.setFailed(error.message);
55+
56+
const badgeColor = (persent) => {
57+
if(persent <= 50) {
58+
return 'red';
59+
}
60+
if(persent < 70) {
61+
return 'yellow';
62+
}
63+
if(persent < 80) {
64+
return 'yellowgreen';
65+
}
66+
if(persent < 90) {
67+
return 'green';
68+
}
69+
return 'brightgreen'
1470
}
1571

16-
/*
1772
try {
18-
// `who-to-greet` input defined in action metadata file
19-
const nameToGreet = core.getInput('who-to-greet');
20-
console.log(`Hello ${nameToGreet}!`);
21-
const time = (new Date()).toTimeString();
22-
core.setOutput("time", time);
23-
// Get the JSON webhook payload for the event that triggered the workflow
24-
const payload = JSON.stringify(github.context.payload, undefined, 2)
25-
console.log(`The event payload: ${payload}`);
26-
} catch (error) {
27-
core.setFailed(error.message);
73+
const file = process.env.FILE;
74+
const token = process.env.TOKEN;
75+
const repository = process.env.REPOSITORY;
76+
77+
if(file === undefined || token === undefined || repository === undefined) {
78+
throw new Error('No params.');
2879
}
29-
*/
80+
81+
dlog.info('File:', file);
82+
dlog.info('Token:', token);
83+
dlog.info('Repository:', repository);
84+
85+
const persent = getPersent(file);
86+
dlog.debug(`Coverage: ${persent.toFixed()}%`);
87+
const content = `{"schemaVersion": 1,"label":"swift-doc-coverage","message":"${persent.toFixed()}%","color":"${badgeColor(persent)}"}`;
88+
89+
const outputFile = repository.substring(repository.lastIndexOf('/') + 1) + ".json";
90+
dlog.debug('Output file:', outputFile);
91+
updateGist(token, 'Swift Doc Coverage', outputFile, content);
92+
}
93+
catch (error) {
94+
dlog.error(error.message);
95+
core.setFailed(error.message);
96+
}

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ runs:
4040
- shell: bash
4141
run: |
4242
swift-doc-coverage ${{ inputs.inputs }} -r json -o ./result.json
43-
TOKEN="${{ github.token }}" node ${{ github.action_path }}/action.js
43+
TOKEN="${{ inputs.token }}" FILE='result.json' REPOSITORY="${{ github.repository }}" node ${{ github.action_path }}/action.js
4444
4545
4646
branding:

dlog.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// DLog.js
2+
3+
Object.defineProperty(global, '__stack', {
4+
get: function() {
5+
var orig = Error.prepareStackTrace;
6+
Error.prepareStackTrace = function(_, stack) {
7+
return stack;
8+
};
9+
var err = new Error;
10+
Error.captureStackTrace(err, arguments.callee);
11+
var stack = err.stack;
12+
Error.prepareStackTrace = orig;
13+
return stack;
14+
}
15+
});
16+
17+
const log = (type, args) => {
18+
const filePath = __stack[2].getFileName();
19+
const fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
20+
const line = __stack[2].getLineNumber();
21+
22+
const r = new RegExp('T(.*)Z');
23+
const time = (new Date()).toISOString().match(r)[1];
24+
console.log('•', time, '[DLOG]', type, `<${fileName}:${line}>`, args.join(' '));
25+
}
26+
27+
const info = (...args) => {
28+
log('✅ [INFO]', args);
29+
};
30+
31+
const debug = (...args) => {
32+
log('▶️ [DEBUG]', args);
33+
};
34+
35+
const error = (...args) => {
36+
log('⚠️ [ERROR]', args);
37+
};
38+
39+
module.exports = {
40+
info,
41+
debug,
42+
error,
43+
};

0 commit comments

Comments
 (0)