-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (57 loc) · 1.5 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
require('dotenv').config()
const { Octokit } = require('@octokit/rest')
const checkInterval = 1000 // ms
const milestoneStep = 500
const emojis = [
':broken_heart:',
':heart:',
':orange_heart:',
':purple_heart:',
':yellow_heart:',
':heartpulse:',
':sparkling_heart:',
':gift_heart:',
':heartbeat:',
':two_hearts:',
':revolving_hearts:'
]
const getMilestone = (stars) => {
const emojiIndex = Math.floor(stars / milestoneStep)
return emojis[emojiIndex >= emojis.length ? emojis.length - 1 : emojiIndex]
}
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
})
const getRepoStars = () => {
return octokit.request('GET /repositories/:id', {
id: process.env.REPO_ID,
}).then(repo => repo.data.stargazers_count)
}
const renameRepo = (data) => {
return octokit.request('PATCH /repositories/:id', {
id: process.env.REPO_ID,
...data,
})
}
let lastStars = 0
const checkAndRenameRepo = async () => {
try {
const stars = await getRepoStars()
if (lastStars !== stars) {
await renameRepo({
name: `This-Repo-Has-${stars}-Stars`,
description: `Yes, it's true ${getMilestone(stars)}`
})
lastStars = stars
}
} catch (err) {
console.log(err)
}
setTimeout(checkAndRenameRepo, checkInterval)
}
getRepoStars()
.then(stars => {
lastStars = stars
checkAndRenameRepo()
})
.catch(console.log)