generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
119 lines (105 loc) · 3.51 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs');
const path = require('path');
const getOrCreateBranch = require('./getOrCreateBranch');
const createOrUpdateFile = require('./createOrUpdateFile');
const getOrCreatePullRequest = require('./getOrCreatePullRequest');
const getFilenamesFromEncodedArray = require('./getFilenamesFromEncodedArray');
const convertFile = require('./convertFile');
const deleteFile = require('./deleteFile');
const getFilenames = (dir) => {
const subdirs = fs.readdirSync(dir);
const files = subdirs.map(subdir => {
const res = path.join(dir, subdir);
return (fs.statSync(res)).isDirectory() ? getFilenames(res) : res;
});
return files.reduce((a, f) => a.concat(f), []);
};
async function run() {
try {
const token = core.getInput('token', { required: true });
const owner = core.getInput('owner');
const repo = core.getInput('repo');
const branch = core.getInput('branch') || github.context.ref;
const storagePath = core.getInput('storagePath', { required: true });
const prefixBranch = core.getInput('prefixBranch', { required: true });
const encodedRemovedFilenames = core.getInput('encodedRemovedFilenames') || [];
const prefixPathForRemovedFiles = core.getInput('prefixPathForRemovedFiles') || '';
const client = new github.GitHub(token);
const newBranch = `${prefixBranch}/${branch}`;
const prTitle = `[AUTOMATION] ${branch}`;
const removedFilenames = getFilenamesFromEncodedArray(encodedRemovedFilenames);
const { data: { default_branch: base_branch } } = await client.repos.get({
owner,
repo,
});
core.debug(`New branch: ${newBranch}`)
core.debug(`Base branch: ${base_branch}`)
const files = getFilenames(storagePath)
.map(filename => {
const content = fs.readFileSync(filename, 'binary');
return {
path: path.relative(storagePath, filename),
content: Buffer.from(content, 'binary').toString('base64'),
};
}
);
if (files.length === 0 && removedFilenames.length === 0) {
core.debug('No files to add or remove');
return;
}
await getOrCreateBranch({
client,
owner,
repo,
branch: newBranch,
base_branch,
log: (msg) => core.debug(msg),
});
core.debug(`Created branch: ${owner}/${repo}@${newBranch}`);
for (const file of files) {
await createOrUpdateFile({
client,
owner,
repo,
branch: newBranch,
file,
log: (msg) => core.debug(msg),
});
}
core.debug(`Commited ${files.length} files`);
const parsedFilenames = removedFilenames
.map(file => convertFile(prefixPathForRemovedFiles, file))
.flat();
core.debug(removedFilenames);
core.debug(parsedFilenames);
for (const filename of parsedFilenames) {
await deleteFile({
client,
owner,
repo,
path: filename,
branch: newBranch,
log: (msg) => core.debug(msg),
});
}
core.debug(`Deleted ${parsedFilenames.length} files`);
const pr = await getOrCreatePullRequest({
client,
owner,
repo,
branch: newBranch,
title: prTitle,
base_branch,
log: (msg) => core.debug(msg),
});
core.debug(`Created Pull Request #${pr.number} in ${owner}/${repo}`);
core.debug(`Pull Request link: ${pr.html_url}`);
core.setOutput('prUrl', pr.html_url);
}
catch (error) {
core.setFailed(error.message);
}
}
run();