Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
123 lines (107 loc) · 2.08 KB
/
Copy pathmain.js
File metadata and controls
123 lines (107 loc) · 2.08 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
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
const listFilesFromPR = async (options) => {
const {
client,
owner,
repo,
prNumber,
} = options;
let firstOrHasNext = true;
let files = [];
let per_page = 100;
let page = 1;
while (firstOrHasNext) {
const { data } = await client.pulls.listFiles({
owner,
repo,
pull_number: prNumber,
per_page,
page,
});
if (data.length > 0) {
files.push(...data);
page += 1;
} else {
firstOrHasNext = false;
}
}
return files;
};
const listFiles = async (options) => {
const {
client,
owner,
repo,
prNumber,
} = options;
const files = await listFilesFromPR({
client,
owner,
repo,
prNumber,
});
return files
.filter(({ status }) => status !== 'removed')
.filter(({ filename }) => filename.endsWith('index.md'))
.map(({ filename }) => filename);
};
const downloadFile = async (options) => {
const {
client,
owner,
repo,
ref,
filename,
} = options;
const { data: file } = await client.repos.getContents({
owner,
repo,
path: filename,
ref
});
return {
content: Buffer.from(file.content, file.encoding).toString(),
filename: file.path
}
};
const downloadFiles = async (options) => {
const {
client,
owner,
repo,
ref,
prNumber,
log
} = options;
const filenames = await listFiles({ client, owner, repo, prNumber, log });
console.log('[ filenames ]', filenames)
return Promise.all(
filenames.map(filename => downloadFile({ client, owner, repo, ref, filename, log }))
);
};
const listRemovedFiles = async (options) => {
const {
client,
owner,
repo,
prNumber,
} = options;
const files = await listFilesFromPR({
client,
owner,
repo,
prNumber,
});
return files
.filter(({ status }) => status === 'removed')
.map(({ filename }) => filename);
};
const arrayToBase64 = (array) => {
return Buffer.from(array.join(',')).toString('base64');
};
module.exports = {
listFiles,
downloadFile,
downloadFiles,
listRemovedFiles,
arrayToBase64,
};