-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (115 loc) · 4.6 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const core = require('@actions/core');
const request = require('sync-request');
const fse = require('fs-extra');
// extract inputs
const filePaths = core.getInput('file-path').split("\n");
const requestOptions = { headers: {'Authorization': "token " + core.getInput('github-token'), 'Accept': 'application/vnd.github.v3+json', 'User-Agent': 'checkout-file-action'} };
const repo = core.getInput('repo');
const branch = core.getInput('branch') ? ('refs/heads/' + core.getInput('branch')) : (repo == process.env.GITHUB_REPOSITORY ? process.env.GITHUB_REF : "master")
// retrieve each file
batchRetrieveFiles(retrieveFileInfos(filePaths));
function retrieveFile(info) {
try {
const url = 'https://api.github.com/repos/' + repo + '/git/blobs/' + info.sha;
const response = request('GET', url, requestOptions);
if (response.statusCode != 200) {
handleNon200(response, info.path);
return;
}
const body = response.body.toString();
const json = JSON.parse(body);
const encodedContent = json.content;
if (!encodedContent) {
core.setFailed("JSON is missing expected key 'content'. Unable to continue.\n\n" + JSON.stringify(json));
return;
}
// remove any newlines from the string and then decode the base64 contents
const buffer = new Buffer.from(encodedContent.replace('\n', ''), 'base64');
content = buffer.toString('ascii');
// save the file to disk
fse.outputFileSync(info.path, content);
core.info('Successfully retrieved ' + info.path);
} catch (error) {
core.setFailed(`Failed in retrieveFile(info). Error: ${error}`);
}
}
function batchRetrieveFiles(fileInfos) {
try {
const binaryObjects = fileInfos.filter(info => info.isBinary)
const textObjects = fileInfos.filter(info => binaryObjects.indexOf(info) === -1)
const graphObjects = textObjects.map((info, index) => 'file' + index + ': object(expression: "' + info.sha + '") { ... on Blob { text } }').join(',\n');
const query = '{ files: repository(owner: "' + repo.split("/").join('", name: "') + '") { ' + graphObjects + ' } }'
const url = 'https://api.github.com/graphql';
const requestOptions = {
headers: {'Authorization': "token " + core.getInput('github-token'), 'User-Agent': 'checkout-file-action'},
json: {'query': query }
};
const response = request('POST', url, requestOptions);
if (response.statusCode != 200) {
handleNon200(response);
return;
}
const body = response.body.toString();
const json = JSON.parse(body);
const fileContents = json.data.files;
fileInfos.forEach((info, index) => {
fse.outputFileSync(info.path, fileContents["file" + index].text)
core.info('Successfully retrieved ' + info.path);
});
// retrieve all of the binary files
textObjects.every(retrieveFile)
} catch (error) {
core.setFailed(`Failed in batchRetrieveFiles(fileInfos). Error: ${error}`);
}
}
function retrieveFileInfos(matching) {
try {
const url = 'https://api.github.com/repos/' + repo + '/git/trees/' + branch + '?recursive=true';
const response = request('GET', url, requestOptions);
if (response.statusCode != 200) {
handleNon200(response);
return;
}
const body = response.body.toString();
const json = JSON.parse(body);
return json.tree.filter(info => info.type == "blob" && pathsContain(matching, info.path)).sort((a, b) => b.size - a.size)
} catch (error) {
core.setFailed(`Failed in retrieveAllFiles(matching). Error: ${error}`);
}
}
function pathsContain(paths, subpath) {
return paths.find(rootPath => rootPath == "./" || rootPath == subpath || subpath.startsWith(rootPath + '/')) != null
}
function handleNon200(response, filePath) {
try {
const body = response.body.toString();
const json = JSON.parse(body);
if (json.message == 'Not Found' && filePath) {
checkForAccessToRepo()
core.setFailed('The file "' + filePath + '" does not exist. Exiting.');
} else if (json.message == 'Bad credentials') {
core.setFailed("The provided GitHub token doesn't have access. Exiting.")
} else {
checkForAccessToRepo()
core.setFailed('Invalid response code: ' + response.statusCode + '\n\n' + body);
}
} catch (error) {
checkForAccessToRepo()
core.setFailed('Invalid response code: ' + response.statusCode + '\n\n' + body);
}
process.exit(1)
}
function checkForAccessToRepo() {
try {
const url = 'https://api.github.com/repos/' + repo;
const response = request('GET', url, requestOptions);
if (response.statusCode != 200) {
core.setFailed('Unable to access ' + repo + ". Either the repository doesn't exist or the provided GitHub token doesn't have access. Exiting.");
process.exit(1)
}
return true
} catch (error) {
core.setFailed(error);
process.exit(1)
}
}