forked from PhilBrockman/Magic8Ball
-
Notifications
You must be signed in to change notification settings - Fork 0
155 lines (145 loc) · 5.96 KB
/
magic8ball.yaml
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
name: Ask Magic 8 Ball
on:
push:
branches:
- "**"
jobs:
interact-with-pr:
if: contains(github.event.head_commit.message, '/magic8ball')
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Automated PR Interaction
uses: actions/github-script@v6
with:
script: |
const { data: pullRequests } = await github.rest.repos.listPullRequestsAssociatedWithCommit({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: context.sha,
})
const decisionTypes = {
closing: {
icon: "❌",
messages: [
"Let's keep that approach in our back pocket!",
"Signs are indicating that we should do it another (better) way!",
"Not a chance!",
"Absolutely not, try again!",
],
action: async (prNumber) => {
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
state: "closed",
});
},
},
merging: {
icon: "🎉",
messages: [
"Absolutely!",
"Definitely go for it!",
"Outlook is looking bright!",
"Signs are pointing to a yes!",
"Without a doubt, go for it!",
"LGTM"
],
action: async (prNumber) => {
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
},
},
neutral: {
icon: "🤔",
messages: [
"Outlook is a bit cloudy...",
"Can't predict now, try later!",
"Focus and ask again!",
"Response is unclear, try again!",
"Ask again at a later time!",
],
action: async () => {/* No action needed for neutral responses */ },
},
review: {
icon: "👀",
messages: [
"Let's get some eyes on this!",
"We need an outside opinion!",
"Let's get a new set of eyes here!",
"We need a review!",
],
action: async (prNumber) => {
const prDetails = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
const prAuthor = prDetails.data.user.login;
// Fetch collaborators with write or admin permissions
const collaborators = await github.rest.repos.listCollaborators({
owner: context.repo.owner,
repo: context.repo.repo,
affiliation: 'direct',
});
// Filter out the PR author from the list of potential reviewers
const reviewers = collaborators.data
.filter(collaborator => (collaborator.permissions.push || collaborator.permissions.admin) && collaborator.login !== prAuthor)
.map(collaborator => collaborator.login);
if (reviewers.length > 0) {
const randomReviewer = reviewers[Math.floor(Math.random() * reviewers.length)];
const res = await github.rest.pulls.requestReviewers({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
reviewers: [randomReviewer],
});
console.log('Requested a review from:', randomReviewer);
console.log('Request review response:', res);
} else {
console.log('No eligible reviewers found that are not the PR author.');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: "Awkward... no reviewers are available now, so I'll hop in. Processing. Processing. ⌛️",
});
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: "LGTM 👍",
});
}
},
}
};
const postCommentAndAct = async (prNumber, type) => {
const decision = decisionTypes[type];
const message = decision.messages[Math.floor(Math.random() * decision.messages.length)] + " " + decision.icon;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: message,
});
await decision.action(prNumber);
};
if (pullRequests.length > 0) {
for (const pr of pullRequests) {
const type = Object.keys(decisionTypes)[
Math.floor(
Math.random() *
Object.keys(decisionTypes).length
)
];
await postCommentAndAct(pr.number, type);
}
} else {
console.log('No pull requests associated with the commit.');
}