Skip to content

Commit 6544008

Browse files
committed
improve numbering of assets
1 parent 9fc6690 commit 6544008

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

index.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@ function downloadAssets(body, FOLDER, filename) {
129129
return new Promise(async (resolve, reject) => {
130130
try {
131131
const assets = body?.match(/["(]https:\/\/github\.com\/(.+)\/assets\/(.+)[)"]/g) || []
132-
for (const assetId in assets) {
133-
const targetFilename = filename.replace('{id}', assetId)
132+
for (let n = 0; n < assets.length; n++) {
133+
const targetFilename = filename.replace('{id}', (n+1).toString().padStart(assets.length.toString().length, '0'))
134134
const targetPath = FOLDER + '/' + targetFilename
135-
const sourceUrl = assets[assetId].replace(/^["(](.+)[)"]$/, '$1')
135+
const sourceUrl = assets[n].replace(/^["(](.+)[)"]$/, '$1')
136136
fs.ensureDirSync(FOLDER)
137137
const realTargetFilename = basename(await downloadFile(sourceUrl, targetPath))
138-
body = body.replace(`"${sourceUrl}"`, '"file://./assets/' + realTargetFilename + '"')
139-
body = body.replace(`(${sourceUrl})`, '(file://./assets/' + realTargetFilename + ')')
138+
body = body.replace(`"${sourceUrl}"`, '"./assets/' + realTargetFilename + '"')
139+
body = body.replace(`(${sourceUrl})`, '(./assets/' + realTargetFilename + ')')
140140
}
141141
return resolve(body)
142142
} catch (err) {
@@ -169,29 +169,29 @@ async function backup() {
169169
const issues = await requestAllWithRetry(`/repos/${USERNAME}/${repository.name}/issues?state=all`)
170170

171171
// Loop issues
172-
for (const issueId in issues) {
173-
172+
for (const issue of issues) {
173+
174174
// Download issue assets
175-
issues[issueId].body = await downloadAssets(
176-
issues[issueId].body,
175+
issue.body = await downloadAssets(
176+
issue.body,
177177
`${FOLDER}/repositories/${repository.name}/assets`,
178-
`issue_${issueId}_{id}`
178+
`issue_${issue.id}_{id}`
179179
)
180180

181181
// Get issue comments
182-
const comments = issues[issueId].comments !== 0 ? await requestAllWithRetry(issues[issueId].comments_url) : []
182+
const comments = issue.comments !== 0 ? await requestAllWithRetry(issue.comments_url) : []
183183

184184
// Add issue comments to issues JSON
185-
issues[issueId].comments = comments
185+
issue.comments = comments
186186

187187
// Loop issue comments
188-
for (const commentId in comments) {
188+
for (const comment of comments) {
189189

190190
// Download issue assets
191-
issues[issueId].comments[commentId].body = await downloadAssets(
192-
issues[issueId].comments[commentId].body,
191+
comment.body = await downloadAssets(
192+
comment.body,
193193
`${FOLDER}/repositories/${repository.name}/assets`,
194-
`issue_${issueId}_comment_${commentId}_{id}`
194+
`issue_${issue.id}_comment_${comment.id}_{id}`
195195
)
196196

197197
}

0 commit comments

Comments
 (0)