Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
evghen1 committed Jul 25, 2023
2 parents 88373fe + 893277d commit f3f8f6f
Showing 1 changed file with 53 additions and 54 deletions.
107 changes: 53 additions & 54 deletions nodejs/src/modules/fetch-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { Octokit } = require("@octokit/rest");
const https = require('https');
const fs = require('fs');
const GITHUB_DELAY = process.env['GITHUB_DELAY'];
function delay(time) {
async function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
module.exports = async function (repoUrl, GITHUB_TOKEN) {
Expand All @@ -15,66 +15,65 @@ module.exports = async function (repoUrl, GITHUB_TOKEN) {
let loadNextPage = true;
console.log("Fetching all releases from " + owner + "/" + repo);
while (loadNextPage) {
console.log("Fetching page " + pageNumber);
console.log(new Date().toString() + "Fetching page " + pageNumber);
pageNumber++;
if(GITHUB_DELAY) {
await delay(parseInt(GITHUB_DELAY));
}
await octokit.repos.
listReleases({
owner: owner,
repo: repo,
page: pageNumber,
per_page: 100,
})
.then(({ data }) => {
if (data.length == 0) {
loadNextPage = false;
} else {
data.forEach(async (releaseData) => {
let assetUrl = releaseData.assets[0].url;
let assetFileName = releaseData.assets[0].name;
console.log("Downloading " + assetFileName);
const options = {
method: 'GET',
headers: {
Accept: 'application/octet-stream',
Authorization: 'token ' + GITHUB_TOKEN,
"X-GitHub-Api-Version": "2022-11-28",
"User-Agent": 'Helm-Plugin-Github'
}
};
if(GITHUB_DELAY) {
await delay(parseInt(GITHUB_DELAY));
}
const req = https.request(assetUrl, options, (res) => {

if (res.statusCode == 302) {
const redReq = https.request(res.headers.location, (res) => {
let releases = [];
await octokit.repos.listReleases({
owner: owner,
repo: repo,
page: pageNumber,
per_page: 100,
})
.then(({ data }) => {
if (data.length == 0) {
loadNextPage = false;
} else {
releases = [...releases, ...data];
}
});

res.on('data', (d) => {
fs.appendFileSync(assetFileName, d);
});
});
for (let releaseData of releases) {
let assetUrl = releaseData.assets[0].url;
let assetFileName = releaseData.assets[0].name;
const options = {
method: 'GET',
headers: {
Accept: 'application/octet-stream',
Authorization: 'token ' + GITHUB_TOKEN,
"X-GitHub-Api-Version": "2022-11-28",
"User-Agent": 'Helm-Plugin-Github'
}
};
if(GITHUB_DELAY) {
await delay(parseInt(GITHUB_DELAY));
}
console.log(new Date().toString() + " Downloading " + assetFileName);
const req = https.request(assetUrl, options, (res) => {

redReq.on('error', (e) => {
process.stderr.write(e);
});
redReq.end();
} else {
res.on('data', (d) => {
fs.appendFileSync(assetFileName, d);
});
}
});
if (res.statusCode == 302) {
const redReq = https.request(res.headers.location, (res) => {

req.on('error', (e) => {
process.stderr.write(e);
res.on('data', (d) => {
fs.appendFileSync(assetFileName, d);
});
req.end();
});

})
redReq.on('error', (e) => {
process.stderr.write(e);
});
redReq.end();
} else {
res.on('data', (d) => {
fs.appendFileSync(assetFileName, d);
});
}
});

req.on('error', (e) => {
process.stderr.write(e);
});
req.end();

};
}
}

0 comments on commit f3f8f6f

Please sign in to comment.