-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (82 loc) · 2.96 KB
/
index.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
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 core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs')
try {
const context = github.context.payload;
const owner = context['repository']['owner']['login'];
const repo = context['repository']['name'];
const base = context['repository']['default_branch'];
var head;
if (github.context.eventName === 'pull_request') {
head = context['pull_request']['head']['ref'];
} else {
head = context['ref'].replace("refs/heads/", "");
}
const token = core.getInput('repo-token');
const octokit = github.getOctokit(token);
const depth = core.getInput('depth');
const buildAll = core.getInput('build-all');
const targetDirectory = core.getInput('target-directory')
if (targetDirectory && depth == 1) {
depth = 2;
}
if (buildAll) {
const directories = getDirectories(targetDirectory)
const result = buildMatrix(directories)
core.setOutput("is_empty", result['include'].length < 1)
core.setOutput("build_matrix", JSON.stringify(result));
} else {
octokit.repos.compareCommits({owner, repo, base, head})
.then(data => {
const changedDirectories = data['data']['files']
.map(file => file['filename'])
.filter(file => reduceFile(file, targetDirectory))
.map(file => parseDirectories(file, depth, targetDirectory))
.filter(directory => directory.length > 0);
const result = buildMatrix(changedDirectories)
core.setOutput("is_empty", result['include'].length < 1)
core.setOutput("build_matrix", JSON.stringify(result));
}).catch(error => {
console.log(error);
core.setFailed(error.message);
});
}
} catch (error) {
console.log(error);
core.setFailed(error.message);
}
function reduceFile(file, targetDirectory) {
if (targetDirectory) {
return file.startsWith(targetDirectory);
} else {
return file;
}
}
function parseDirectories(file, depth, targetDirectory) {
let parts = file.split("/");
var directory = "";
for (let i = 0; i < parts.length && i < depth; i++) {
directory = directory.concat(parts[i]);
if (i != parts.length - 1 && i != depth - 1) {
directory = directory.concat('/');
}
}
if (targetDirectory) {
directory = directory.replace(targetDirectory + '/', "");
}
return directory;
}
function buildMatrix(changedDirectories) {
let directories = [...new Set(changedDirectories)];
let include = []
for (directory of directories) {
include.push({'directory': directory});
}
return {'include': include};
}
function getDirectories(path) {
const isDirectory = fs.lstatSync(path).isDirectory()
return fs.readdirSync(path, { withFileTypes: true})
.filter(directory => fs.lstatSync(path + "/" + directory.name).isDirectory())
.map(row => row['name'])
}