1
1
const core = require ( '@actions/core' ) ;
2
- const github = require ( '@actions/github' ) ;
3
- //const http = require('https');
2
+ const { Octokit } = require ( "@octokit/core" ) ;
4
3
const fs = require ( 'fs' ) ;
4
+ const dlog = require ( "./dlog.js" ) ;
5
5
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
+ } ;
7
15
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' ) ;
11
54
}
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'
14
70
}
15
71
16
- /*
17
72
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.' ) ;
28
79
}
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
+ }
0 commit comments