refactor(GitHubCommitActivity): switch to v4/GraphQL API (#6959)

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
This commit is contained in:
Caleb Cartwright
2021-08-29 12:59:36 -05:00
committed by GitHub
parent b02471ff42
commit 76ca283775
3 changed files with 161 additions and 50 deletions

View File

@@ -1,22 +1,28 @@
import gql from 'graphql-tag'
import Joi from 'joi'
import { InvalidResponse } from '../index.js'
import { metric } from '../text-formatters.js'
import { nonNegativeInteger } from '../validators.js'
import { GithubAuthV3Service } from './github-auth-service.js'
import { errorMessagesFor, documentation } from './github-helpers.js'
import { GithubAuthV4Service } from './github-auth-service.js'
import { transformErrors, documentation } from './github-helpers.js'
const schema = Joi.array()
.items(
Joi.object({
total: nonNegativeInteger,
})
)
.required()
const schema = Joi.object({
data: Joi.object({
repository: Joi.object({
object: Joi.object({
history: Joi.object({
totalCount: nonNegativeInteger,
}).required(),
}),
}).required(),
}).required(),
}).required()
export default class GithubCommitActivity extends GithubAuthV3Service {
export default class GitHubCommitActivity extends GithubAuthV4Service {
static category = 'activity'
static route = {
base: 'github/commit-activity',
pattern: ':interval(y|m|4w|w)/:user/:repo',
pattern: ':interval(y|m|4w|w)/:user/:repo/:branch*',
}
static examples = [
@@ -29,6 +35,20 @@ export default class GithubCommitActivity extends GithubAuthV3Service {
keywords: ['commits'],
documentation,
},
{
title: 'GitHub commit activity (branch)',
// Override the pattern to omit the deprecated interval "4w".
pattern: ':interval(y|m|w)/:user/:repo/:branch*',
namedParams: {
interval: 'm',
user: 'badges',
repo: 'squint',
branch: 'main',
},
staticPreview: this.render({ interval: 'm', commitCount: 5 }),
keywords: ['commits'],
documentation,
},
]
static defaultBadgeData = { label: 'commit activity', color: 'blue' }
@@ -46,47 +66,67 @@ export default class GithubCommitActivity extends GithubAuthV3Service {
}
}
static transform({ interval, weekData }) {
const weekTotals = weekData.map(({ total }) => total)
if (interval === 'm') {
// To approximate the value for the past month, get the sum for the last
// four weeks and add a weighted value for the fifth week.
const fourWeeksValue = weekTotals
.slice(-4)
.reduce((sum, weekTotal) => sum + weekTotal, 0)
const fifthWeekValue = weekTotals.slice(-5)[0]
const averageWeeksPerMonth = 365 / 12 / 7
return (
fourWeeksValue + Math.round((averageWeeksPerMonth - 4) * fifthWeekValue)
)
}
let wantedWeekData
switch (interval) {
case 'y':
wantedWeekData = weekTotals
break
case '4w':
wantedWeekData = weekTotals.slice(-4)
break
case 'w':
wantedWeekData = weekTotals.slice(-2, -1)
break
default:
throw Error('Unhandled case')
}
return wantedWeekData.reduce((sum, weekTotal) => sum + weekTotal, 0)
async fetch({ interval, user, repo, branch = 'HEAD' }) {
const since = this.constructor.getIntervalQueryStartDate({ interval })
return this._requestGraphql({
query: gql`
query (
$user: String!
$repo: String!
$branch: String!
$since: GitTimestamp!
) {
repository(owner: $user, name: $repo) {
object(expression: $branch) {
... on Commit {
history(since: $since) {
totalCount
}
}
}
}
}
`,
variables: {
user,
repo,
branch,
since,
},
schema,
transformErrors,
})
}
async handle({ interval, user, repo }) {
const weekData = await this._requestJson({
url: `/repos/${user}/${repo}/stats/commit_activity`,
schema,
errorMessages: errorMessagesFor(),
})
const commitCount = this.constructor.transform({ interval, weekData })
static transform({ data }) {
const {
repository: { object: repo },
} = data
if (!repo) {
throw new InvalidResponse({ prettyMessage: 'invalid branch' })
}
return repo.history.totalCount
}
static getIntervalQueryStartDate({ interval }) {
const now = new Date()
if (interval === 'y') {
now.setUTCFullYear(now.getUTCFullYear() - 1)
} else if (interval === 'm' || interval === '4w') {
now.setUTCDate(now.getUTCDate() - 30)
} else {
now.setUTCDate(now.getUTCDate() - 7)
}
return now.toISOString()
}
async handle({ interval, user, repo, branch }) {
const json = await this.fetch({ interval, user, repo, branch })
const commitCount = this.constructor.transform(json)
return this.constructor.render({ interval, commitCount })
}
}