-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
117 lines (104 loc) · 4.25 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
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
/**
* @param {import('probot').Probot} app
*/
module.exports = (app) => {
async function findOrCreateStargazersIssue(context) {
const OWNER = context.payload.repository.owner.login;
const REPO = context.payload.repository.name;
const LABEL = "stargazers";
const { data: issues } = await context.octokit.issues.listForRepo({
repo: REPO,
owner: OWNER,
state: "open",
});
const stargazersIssue = issues.find((issue) =>
issue.labels.some((label) => label.name === LABEL)
);
if (stargazersIssue) {
return stargazersIssue.number;
} else {
const { data: issue } = await context.octokit.issues.create({
repo: REPO,
owner: OWNER,
body: "This issue tracks the stargazers and the runaway stargazers of this repo",
labels: [LABEL],
title: "Issue to track stargazers",
});
return issue.number;
}
}
app.on(["star.created", "star.deleted"], async (context) => {
const issueNumber = await findOrCreateStargazersIssue(context);
const OWNER = context.payload.repository.owner.login;
const REPO = context.payload.repository.name;
const STARGAZERS = context.payload.repository.stargazers_count;
const {
data: { login: USER },
} = await context.octokit.users.getByUsername({
username: context.payload.sender.login,
});
const totalStars = `${REPO} has ${
STARGAZERS > 1 ? `**${STARGAZERS} stars**` : `**${STARGAZERS} star**`
} now`;
const unsubscribe = `PS: don't forget to unsubscribe from this thread. \n If you don't — whenever anyone stars this repo, you'll get a mail, notifying you, because you were mentioned previously.`;
const responsesForWhenAStarIsAdded = [
{
message: `Thank you so much for starring this repo, @${USER} :pray:, this means a lot! \n\n ${totalStars} \n\n ${unsubscribe}`,
},
{
message: `Wow! Thanks for the star, @${USER}! :star2: \n\n ${totalStars} \n\n ${unsubscribe}`,
},
{
message: `Thank you for showing your support by starring this repository, @${USER}! :raised_hands: \n\n ${totalStars} \n\n ${unsubscribe}`,
},
{
message: `We appreciate your interest in this repository, @${USER}! Thank you for the star! :star: \n\n ${totalStars} \n\n ${unsubscribe}`,
},
{
message: `Thanks for the star, @${USER}! It means a lot to us! :pray: \n\n ${totalStars} \n\n ${unsubscribe}`,
},
{
message: `You just made our day, @${USER}! Thanks for the star! :heart_eyes: \n\n ${totalStars} \n\n ${unsubscribe}`,
},
];
const responsesForWhenAStarIsRemoved = [
{
message: `We're sad to see you go, @${USER}. Thanks for your support while you were here! :cry: \n\n ${totalStars} \n\n ${unsubscribe}`,
},
{
message: `Thanks for your interest in this repository, @${USER}. We hope you'll come back soon! :wave: \n\n ${totalStars} \n\n ${unsubscribe}`,
},
{
message: `Sorry to see you unstar the repository, @${USER}. We appreciate your support! :pray: \n\n ${totalStars} \n\n ${unsubscribe}`,
},
{
message: `Thanks for the time you spent with us, @${USER}. We hope to see you again soon! :smile: \n\n ${totalStars} \n\n ${unsubscribe}`,
},
{
message: `We'll miss you, @${USER}! Thanks for your support while you were here! :sob: \n\n ${totalStars} \n\n ${unsubscribe}`,
},
{
message: `@${USER} just unstarred this repository :sob: :sob: \n\n ${totalStars} \n\n ${unsubscribe}`,
},
];
const randomIsStarred = Math.floor(
Math.random() * responsesForWhenAStarIsAdded.length
);
const randomIsStarRemoved = Math.floor(
Math.random() * responsesForWhenAStarIsRemoved.length
);
const commentBody =
context.name === "star" && context.payload.action === "created"
? responsesForWhenAStarIsAdded[randomIsStarred].message
: responsesForWhenAStarIsRemoved[randomIsStarRemoved].message;
await context.octokit.issues.createComment({
owner: OWNER,
repo: REPO,
issue_number: issueNumber,
body: commentBody,
});
return context.octokit.activity.deleteThreadSubscription({
thread_id: issueNumber,
});
});
};