1
+ const child_process = require ( 'child_process' ) ;
2
+ const crypto = require ( 'crypto' ) ;
3
+ const fs = require ( 'fs' ) ;
4
+ const os = require ( 'os' ) ;
5
+ const path = require ( 'path' ) ;
6
+
7
+ const cache = require ( '@actions/cache' ) ;
1
8
const core = require ( '@actions/core' ) ;
2
9
const exec = require ( '@actions/exec' ) ;
3
10
const github = require ( '@actions/github' ) ;
4
11
const tr = require ( '@actions/exec/lib/toolrunner' ) ;
5
12
13
+ function hashString ( content ) {
14
+ const sha256 = crypto . createHash ( 'sha256' ) ;
15
+ return sha256 . update ( content ) . digest ( 'hex' ) ;
16
+ }
17
+
18
+ function getPythonVersion ( ) {
19
+ const args = [ '-c' , 'import sys;print(sys.executable+"\\n"+sys.version)' ] ;
20
+ const res = child_process . spawnSync ( 'python' , args ) ;
21
+ if ( res . status !== 0 ) {
22
+ throw 'python version check failed' ;
23
+ }
24
+ return res . stdout . toString ( ) ;
25
+ }
26
+
27
+ function hashFile ( filePath ) {
28
+ return hashString ( fs . readFileSync ( filePath ) . toString ( ) ) ;
29
+ }
30
+
6
31
function addToken ( url , token ) {
7
32
return url . replace ( / ^ h t t p s : \/ \/ / , `https://x-access-token:${ token } @` ) ;
8
33
}
@@ -22,7 +47,14 @@ async function main() {
22
47
const token = core . getInput ( 'token' ) ;
23
48
const pr = github . context . payload . pull_request ;
24
49
const push = ! ! token && ! ! pr ;
50
+
51
+ const cachePaths = [ path . join ( os . homedir ( ) , '.cache' , 'pre-commit' ) ] ;
52
+ const py = getPythonVersion ( ) ;
53
+ const cacheKey = `pre-commit-2-${ hashString ( py ) } -${ hashFile ( '.pre-commit-config.yaml' ) } ` ;
54
+ await cache . restoreCache ( cachePaths , cacheKey ) ;
25
55
const ret = await exec . exec ( 'pre-commit' , args , { ignoreReturnCode : push } ) ;
56
+ await cache . saveCache ( cachePaths , cacheKey ) ;
57
+
26
58
if ( ret && push ) {
27
59
// actions do not run on pushes made by actions.
28
60
// need to make absolute sure things are good before pushing
0 commit comments