forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-hacktoberfest.service.js
More file actions
240 lines (224 loc) · 6.08 KB
/
Copy pathgithub-hacktoberfest.service.js
File metadata and controls
240 lines (224 loc) · 6.08 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
'use strict'
const gql = require('graphql-tag')
const Joi = require('joi')
const moment = require('moment')
const { metric, maybePluralize } = require('../text-formatters')
const { nonNegativeInteger } = require('../validators')
const { GithubAuthV4Service } = require('./github-auth-service')
const {
documentation: githubDocumentation,
transformErrors,
} = require('./github-helpers')
const documentation = `
<p>
This badge is designed for projects hosted on GitHub which are
participating in
<a href="https://hacktoberfest.digitalocean.com">Hacktoberfest</a>,
an initiative to encourage participating in open-source projects. The
badge can be added to the project readme to encourage potential
contributors to review the suggested issues and to celebrate the
contributions that have already been made.
The badge displays three pieces of information:
<ul>
<li>
The number of suggested issues. By default this will count open
issues with the <strong>hacktoberfest</strong> label, however you
can pick a different label (e.g.
<code>?suggestion_label=good%20first%20issue</code>).
</li>
<li>
The number of pull requests opened in October. This excludes any
PR with the <strong>invalid</strong> label.
</li>
<li>The number of days left of October.</li>
</ul>
</p>
${githubDocumentation}
`
const schema = Joi.object({
data: Joi.object({
repository: Joi.object({
issues: Joi.object({
totalCount: nonNegativeInteger,
}).required(),
}).required(),
search: Joi.object({
issueCount: nonNegativeInteger,
}).required(),
}).required(),
}).required()
const queryParamSchema = Joi.object({
suggestion_label: Joi.string(),
}).required()
module.exports = class GithubHacktoberfestCombinedStatus extends (
GithubAuthV4Service
) {
static category = 'issue-tracking'
static route = {
base: 'github/hacktoberfest',
pattern: ':year(2019|2020)/:user/:repo',
queryParamSchema,
}
static examples = [
{
title: 'GitHub Hacktoberfest combined status',
namedParams: {
year: '2020',
user: 'snyk',
repo: 'snyk',
},
staticPreview: this.render({
suggestedIssueCount: 12,
contributionCount: 8,
daysLeft: 15,
}),
documentation,
},
{
title: 'GitHub Hacktoberfest combined status (suggestion label override)',
namedParams: {
year: '2020',
user: 'tmrowco',
repo: 'tmrowapp-contrib',
},
queryParams: {
suggestion_label: 'help wanted',
},
staticPreview: this.render({
year: '2020',
suggestedIssueCount: 12,
contributionCount: 8,
daysLeft: 15,
}),
documentation,
},
]
static defaultBadgeData = { label: 'hacktoberfest', color: 'orange' }
static render({
suggestedIssueCount,
contributionCount,
daysLeft,
daysToStart,
year,
hasStarted = true,
}) {
if (!hasStarted) {
return {
message: `${daysToStart} ${maybePluralize(
'day',
daysToStart
)} till kickoff!`,
}
}
if (daysLeft === undefined) {
// The global cutoff time is 11/1 noon UTC.
// https://github.com/badges/shields/pull/4109#discussion_r330782093
// We want to show "1 day left" on the last day so we add 1.
daysLeft = moment(`${year}-11-01 12:00:00 Z`).diff(moment(), 'days') + 1
}
if (daysLeft < 0) {
return {
message: `is over! (${metric(contributionCount)} ${maybePluralize(
'PR',
contributionCount
)} opened)`,
}
}
const message =
[
suggestedIssueCount
? `${metric(suggestedIssueCount)} ${maybePluralize(
'open issue',
suggestedIssueCount
)}`
: '',
contributionCount
? `${metric(contributionCount)} ${maybePluralize(
'PR',
contributionCount
)}`
: '',
daysLeft > 0
? `${daysLeft} ${maybePluralize('day', daysLeft)} left`
: '',
]
.filter(Boolean)
.join(', ') || 'is over!'
return { message }
}
async fetch({ user, repo, year, suggestionLabel = 'hacktoberfest' }) {
const isValidOctoberPR = [
`repo:${user}/${repo}`,
'is:pr',
`created:${year}-10-01..${year}-10-31`,
`-label:invalid`,
]
.filter(Boolean)
.join(' ')
const {
data: {
repository: {
issues: { totalCount: suggestedIssueCount },
},
search: { issueCount: contributionCount },
},
} = await this._requestGraphql({
query: gql`
query(
$user: String!
$repo: String!
$suggestionLabel: String!
$isValidOctoberPR: String!
) {
repository(owner: $user, name: $repo) {
issues(states: [OPEN], labels: [$suggestionLabel]) {
totalCount
}
}
search(query: $isValidOctoberPR, type: ISSUE, last: 0) {
issueCount
}
}
`,
variables: {
user,
repo,
suggestionLabel,
isValidOctoberPR,
},
schema,
transformErrors,
})
return {
suggestedIssueCount,
contributionCount,
}
}
static getCalendarPosition(year) {
const daysToStart = moment(`${year}-10-01 00:00:00 Z`).diff(
moment(),
'days'
)
const isBefore = daysToStart > 0
return { daysToStart, isBefore }
}
async handle({ user, repo, year }, { suggestion_label: suggestionLabel }) {
const { isBefore, daysToStart } = this.constructor.getCalendarPosition(
+year
)
if (isBefore) {
return this.constructor.render({ hasStarted: false, daysToStart, year })
}
const { suggestedIssueCount, contributionCount } = await this.fetch({
user,
repo,
year,
suggestionLabel,
})
return this.constructor.render({
suggestedIssueCount,
contributionCount,
year,
})
}
}