forked from probot/probot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprivate-key.js
38 lines (36 loc) · 1.06 KB
/
private-key.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
const fs = require('fs')
/**
* Finds a private key through various user-(un)specified methods.
* Order of precedence:
* 1. Explicit path (CLI option)
* 2. `PRIVATE_KEY` env var
* 3. `PRIVATE_KEY_PATH` env var
* 4. Any file w/ `.pem` extension in current working dir
* @param {string} [filepath] - Explicit, user-defined path to keyfile
* @returns {string} Private key
* @private
*/
function findPrivateKey (filepath) {
if (filepath) {
return fs.readFileSync(filepath)
}
if (process.env.PRIVATE_KEY) {
return process.env.PRIVATE_KEY.replace(/\\n/g, '\n')
}
if (process.env.PRIVATE_KEY_PATH) {
return fs.readFileSync(process.env.PRIVATE_KEY_PATH)
}
const foundPath = fs.readdirSync(process.cwd())
.find(path => path.endsWith('.pem'))
if (foundPath) {
return findPrivateKey(foundPath)
}
throw new Error(`Missing private key for GitHub App. Please use:
* \`--private-key=/path/to/private-key\` flag, or
* \`PRIVATE_KEY\` environment variable, or
* \`PRIVATE_KEY_PATH\` environment variable
`)
}
module.exports = {
findPrivateKey
}