-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
74 lines (57 loc) · 2.16 KB
/
main.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
//GitHub API Library
const { Octokit } = require('@octokit/rest');
//Insert your GitHub tokens here in next format ['token1', 'token2', 'token3']
const gitTokens = ['token1', 'token2']
//Insert your GitHub names here in next format ['name1', 'name2', 'name3']
const gitNames = ['name1', 'name2']
//Function to generate unique name for the file
function generateUniqueName() {
return `fact_${Math.floor(Math.random() * 150000000)}.txt`;
}
//Function to push random cat fact to the GitHub repo
async function pushRandomCatName(repoOwner, token) {
//API initialization
const octokit = new Octokit({ auth: token });
try {
//Get all files from the repo
const branchFilesData = await octokit.rest.repos.getContent({
owner: repoOwner,
repo: 'randomCatFacts',
branch: 'main',
});
console.log(branchFilesData, 'branchFilesData')
//Get random cat fact
const catsResponse = await fetch("https://catfact.ninja/fact");
const { fact } = await catsResponse.json();
//Generate unique name for the file
let randomName = generateUniqueName();
//Check if file name is unique in the repo
const isAlreadyFileNameExist = !!branchFilesData.data.find((user) => {
return user.path === randomName;
});
//If file name is not unique, generate new name
if (isAlreadyFileNameExist) {
pushRandomCatName(repoOwner, token);
}
// Push file to the repo
await octokit.rest.repos.createOrUpdateFileContents({
owner: repoOwner,
repo: 'randomCatFacts',
path: randomName,
message: 'Another great day with great cat fact',
content: Buffer.from(fact).toString('base64'),
branch: 'main',
});
console.log('Pushed fact to randomCatFacts');
} catch (error) {
console.error('Error pushing fact to randomCatFacts', error);
}
}
//Function to start execution
function startExecution() {
gitTokens.forEach((token, index) => {
pushRandomCatName(gitNames[index], token);
})
}
//Start execution
startExecution();