From 36395715a5ad3c99893bb94143e108e991d41456 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Sun, 20 Sep 2020 13:40:47 -0500 Subject: [PATCH] [GitHubHacktoberfest] 2020 (and beyond) (#5549) * feat: GitHub Hacktoberfest 2020 (and beyond) * chore: prettify * refactor: convert to static fields * refactor: fix spelling in pre-start message Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com> --- .../github/github-hacktoberfest.service.js | 127 ++++++++++-------- services/github/github-hacktoberfest.spec.js | 7 + .../github/github-hacktoberfest.tester.js | 4 +- 3 files changed, 83 insertions(+), 55 deletions(-) diff --git a/services/github/github-hacktoberfest.service.js b/services/github/github-hacktoberfest.service.js index 0192793a39..b0067e1d1a 100644 --- a/services/github/github-hacktoberfest.service.js +++ b/services/github/github-hacktoberfest.service.js @@ -59,66 +59,71 @@ const queryParamSchema = Joi.object({ }).required() module.exports = class GithubHacktoberfestCombinedStatus extends GithubAuthV4Service { - static get category() { - return 'issue-tracking' + static category = 'issue-tracking' + static route = { + base: 'github/hacktoberfest', + pattern: ':year(2019|2020)/:user/:repo', + queryParamSchema, } - static get route() { - return { - base: 'github/hacktoberfest/2019', - pattern: ':user/:repo', - queryParamSchema, - } - } - - static get examples() { - return [ - { - title: 'GitHub Hacktoberfest combined status', - namedParams: { - user: 'snyk', - repo: 'snyk', - }, - staticPreview: this.render({ - suggestedIssueCount: 12, - contributionCount: 8, - daysLeft: 15, - }), - documentation, + static examples = [ + { + title: 'GitHub Hacktoberfest combined status', + namedParams: { + year: '2020', + user: 'snyk', + repo: 'snyk', }, - { - title: - 'GitHub Hacktoberfest combined status (suggestion label override)', - namedParams: { - user: 'tmrowco', - repo: 'tmrowapp-contrib', - }, - queryParams: { - suggestion_label: 'help wanted', - }, - staticPreview: this.render({ - suggestedIssueCount: 12, - contributionCount: 8, - daysLeft: 15, - }), - documentation, + 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 get defaultBadgeData() { - return { - label: 'hacktoberfest', - color: 'orange', + 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!`, + } } - } - - static render({ suggestedIssueCount, contributionCount, daysLeft }) { 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('2019-11-01 12:00:00 Z').diff(moment(), 'days') + 1 + daysLeft = moment(`${year}-11-01 12:00:00 Z`).diff(moment(), 'days') + 1 } if (daysLeft < 0) { return { @@ -152,11 +157,11 @@ module.exports = class GithubHacktoberfestCombinedStatus extends GithubAuthV4Ser return { message } } - async fetch({ user, repo, suggestionLabel = 'hacktoberfest' }) { + async fetch({ user, repo, year, suggestionLabel = 'hacktoberfest' }) { const isValidOctoberPR = [ `repo:${user}/${repo}`, 'is:pr', - 'created:2019-10-01..2019-10-31', + `created:${year}-10-01..${year}-10-31`, `-label:invalid`, ] .filter(Boolean) @@ -201,15 +206,31 @@ module.exports = class GithubHacktoberfestCombinedStatus extends GithubAuthV4Ser } } - async handle({ user, repo }, { suggestion_label: suggestionLabel }) { + static getCalendarPosition(year) { + const daysToStart = + moment(`${year}-10-01 12:00:00 Z`).diff(moment(), 'days') + 1 + 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, }) } } diff --git a/services/github/github-hacktoberfest.spec.js b/services/github/github-hacktoberfest.spec.js index 4f594f062e..01916b5a8a 100644 --- a/services/github/github-hacktoberfest.spec.js +++ b/services/github/github-hacktoberfest.spec.js @@ -18,5 +18,12 @@ describe('GitHubHacktoberfest', function () { }).expect({ message: '54 open issues, 27 PRs, 10 days left', }) + given({ + daysToStart: 5, + hasStarted: false, + year: 2020, + }).expect({ + message: '5 days till kickoff!', + }) }) }) diff --git a/services/github/github-hacktoberfest.tester.js b/services/github/github-hacktoberfest.tester.js index 51eb74c164..69138d33ed 100644 --- a/services/github/github-hacktoberfest.tester.js +++ b/services/github/github-hacktoberfest.tester.js @@ -20,7 +20,7 @@ const isHacktoberfestStatus = Joi.alternatives().try( ) t.create('GitHub Hacktoberfest combined status') - .get('/badges/shields.json') + .get('/2019/badges/shields.json') .expectBadge({ label: 'hacktoberfest', message: isHacktoberfestStatus, @@ -28,7 +28,7 @@ t.create('GitHub Hacktoberfest combined status') t.create('GitHub Hacktoberfest combined status (suggestion label override)') .get( - `/badges/shields.json?suggestion_label=${encodeURIComponent( + `/2019/badges/shields.json?suggestion_label=${encodeURIComponent( 'good first issue' )}` )