-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (81 loc) · 2.96 KB
/
index.js
File metadata and controls
89 lines (81 loc) · 2.96 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const fs = require("fs");
const path = require("path");
const core = require("@actions/core");
const github = require("@actions/github");
const { GitHub } = require("@actions/github/lib/utils");
const glob = core.getInput("glob");
const regexp = core.getInput("regexp");
const symbole = core.getInput("symbole");
const redact_all = core.getInput("redact-all");
const comma_list = core.getInput("comma-list");
const api_endpoint = core.getInput("api-endpoint");
const secrets_type =
comma_list ? "comma-list"
: regexp ? "regexp"
: api_endpoint ? "api_endpoint"
: undefined;
if (secrets_type === undefined) return;
(async function() {
try {
let blacklist;
const filePaths = [];
switch(secrets_type) {
case "comma-list":
blacklist = comma_list.split(",");
break;
case "regexp":
blacklist = new RegExp(regexp);
break;
case "api-endpoint":
// blacklist = await methodToGetRemoteBlacklist(api_endpoint);
break;
}
if (/true/i.test(redact_all)) {
// starting at the repository root, we find and push each .md file path into the filePaths[].
// use the glob pattern to select the right files.
console.log("redact_all", github.context.payload.commits);
} else {
// find and push all the .md file paths into the filePaths[].
// use the glob pattern to select the right files
const commits = github.context.payload.commits;
const octokit = github.getOctokit(process.env.token)
const commit = await octokit.request({
method: "GET",
url: `https://api.github.com/repos/mudlabs/test-redact.md/commits/${commits[0].id}`
});
const commit_files = commit.data.files;
console.log(
commit_files.filename,
commit_files.status,
"raw_url: The https web address of the raw file",
"contents_url: api address of the git file contents. This includes the files name, path, sha, git_url, encoded contents etc..."
);
}
if (filePaths.length > 0) {
// iterate over the filePaths[]
filePaths.forEach(async path => {
const file = await fs.promises.readFile(path, {encoding: "utf-8" });
const redacted = file.replace(blacklist, match => {
let replacement;
const count = match.length;
const replaceWithX = x => match.replace(/./g, x);
switch (symbole) {
case "classic":
replacement = replaceWithX(`<img src="images/redacted.png" width="13"/>`);
break;
case "whiteout":
replacement = `<code>${replaceWithX(` `)}</code>`;
break;
default:
replacement = replaceWithX(symbole);
break;
}
return replacement;
});
const done = await fs.promises.writeFile(path, redacted);
});
}
} catch(error) {
console.log(error);
}
})();