This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.js
executable file
·58 lines (52 loc) · 1.92 KB
/
cache.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
// Copyright 2019 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause
const fs = require('fs-extra')
const path = require('path')
/**
* Get the cache path for this PR branch tag
* @param {number} repoID repository identifier
* @param {number} prID pull request identifier
* @param {string} app Optional: an app that calls the function. (default linter)
* It's needed because gosec uses GOPATH.
*/
function getBranchPath (repoID, prID, app = 'linter') {
if (app === 'gosec') {
return path.join('cache/go/src', repoID.toString(), prID.toString())
}
return path.join('cache', repoID.toString(), prID.toString())
}
/**
* Save a file to local PR cache, distinguish by revision
* @param {number} repoID unique repository identifier
* @param {number} prID unique pull request identifier
* @param {string} filePath relative file path
* @param {any} data file data
* @param {string} fileType Optional: file type so it knows where to save the file.
* It's needed because go files should be in the GOPATH.
*/
function saveFileToPRCache (repoID, prID, filePath, data, fileType) {
const appTag = (fileType === 'go') ? 'gosec' : undefined
const dir = getBranchPath(repoID, prID, appTag)
writeFileCreateDirs(path.join(dir, filePath), data)
}
/**
* Delete pr cache folder
* @param {number} repoID repository identifier
* @param {number} prID pull request identifier
*/
function clearPRCache (repoID, prID) {
fs.removeSync(getBranchPath(repoID, prID))
fs.removeSync(getBranchPath(repoID, prID, 'gosec'))
}
/**
* Save a file to a specific location and create required directories
* @param {string} filePath path to file relative to main directory
* @param {string} data file contents
*/
function writeFileCreateDirs (filePath, data) {
fs.mkdirpSync(path.dirname(filePath))
fs.writeFileSync(filePath, data)
}
module.exports.getBranchPath = getBranchPath
module.exports.saveFile = saveFileToPRCache
module.exports.clear = clearPRCache