[GitHubGistStars] add GitHub Gist Stars (#8471)
* add schema and fetch method to service class * add transform, render and handle method to service class * add documentation and examples * add tester * add suggeseted changes Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
This commit is contained in:
113
services/github/github-gist-stars.service.js
Normal file
113
services/github/github-gist-stars.service.js
Normal file
@@ -0,0 +1,113 @@
|
||||
import gql from 'graphql-tag'
|
||||
import Joi from 'joi'
|
||||
import { metric } from '../text-formatters.js'
|
||||
import { NotFound } from '../index.js'
|
||||
import { GithubAuthV4Service } from './github-auth-service.js'
|
||||
import { documentation as commonDocumentation } from './github-helpers.js'
|
||||
|
||||
const schema = Joi.object({
|
||||
data: Joi.object({
|
||||
viewer: Joi.object({
|
||||
gist: Joi.object({
|
||||
stargazerCount: Joi.number().required(),
|
||||
url: Joi.string().required(),
|
||||
owner: Joi.object({
|
||||
login: Joi.string().required(),
|
||||
}).required(),
|
||||
name: Joi.string().required(),
|
||||
}).allow(null),
|
||||
}).required(),
|
||||
}).required(),
|
||||
}).required()
|
||||
|
||||
const documentation = `${commonDocumentation}
|
||||
<p>This badge shows the number of stargazers for a gist. Gist id is accepted as input and 'gist not found' is returned if the gist is not found for the given gist id.
|
||||
</p>`
|
||||
|
||||
export default class GithubGistStars extends GithubAuthV4Service {
|
||||
static category = 'social'
|
||||
|
||||
static route = {
|
||||
base: 'github/stars/gists',
|
||||
pattern: ':gistId',
|
||||
}
|
||||
|
||||
static examples = [
|
||||
{
|
||||
title: 'Github Gist stars',
|
||||
namedParams: { gistId: '47a4d00457a92aa426dbd48a18776322' },
|
||||
staticPreview: {
|
||||
label: this.defaultBadgeData.label,
|
||||
message: metric(29),
|
||||
style: 'social',
|
||||
},
|
||||
documentation,
|
||||
},
|
||||
]
|
||||
|
||||
static defaultBadgeData = {
|
||||
label: 'Stars',
|
||||
color: 'blue',
|
||||
namedLogo: 'github',
|
||||
}
|
||||
|
||||
static render({ stargazerCount, url, stargazers }) {
|
||||
return { message: metric(stargazerCount), link: [url, stargazers] }
|
||||
}
|
||||
|
||||
async fetch({ gistId }) {
|
||||
const data = await this._requestGraphql({
|
||||
query: gql`
|
||||
query ($gistId: String!) {
|
||||
viewer {
|
||||
gist(name: $gistId) {
|
||||
stargazerCount
|
||||
url
|
||||
name
|
||||
owner {
|
||||
login
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
gistId,
|
||||
},
|
||||
schema,
|
||||
})
|
||||
return data
|
||||
}
|
||||
|
||||
static transform({ data }) {
|
||||
const {
|
||||
data: {
|
||||
viewer: { gist },
|
||||
},
|
||||
} = data
|
||||
|
||||
if (!gist) {
|
||||
throw new NotFound({ prettyMessage: 'gist not found' })
|
||||
}
|
||||
|
||||
const {
|
||||
stargazerCount,
|
||||
url,
|
||||
name,
|
||||
owner: { login },
|
||||
} = gist
|
||||
|
||||
const stargazers = `https://gist.github.com/${login}/${name}/stargazers`
|
||||
|
||||
return { stargazerCount, url, stargazers }
|
||||
}
|
||||
|
||||
async handle({ gistId }) {
|
||||
const data = await this.fetch({ gistId })
|
||||
const { stargazerCount, url, stargazers } =
|
||||
await this.constructor.transform({
|
||||
data,
|
||||
})
|
||||
return this.constructor.render({ stargazerCount, url, stargazers })
|
||||
}
|
||||
}
|
||||
25
services/github/github-gist-stars.tester.js
Normal file
25
services/github/github-gist-stars.tester.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import { createServiceTester } from '../tester.js'
|
||||
import { isMetric } from '../test-validators.js'
|
||||
|
||||
export const t = await createServiceTester()
|
||||
|
||||
t.create('Gist Total Stars')
|
||||
.get('/47a4d00457a92aa426dbd48a18776322.json')
|
||||
.expectBadge({
|
||||
label: 'Stars',
|
||||
message: isMetric,
|
||||
color: 'blue',
|
||||
link: [
|
||||
'https://gist.github.com/47a4d00457a92aa426dbd48a18776322',
|
||||
'https://gist.github.com/maratori/47a4d00457a92aa426dbd48a18776322/stargazers',
|
||||
],
|
||||
})
|
||||
|
||||
t.create('Gist Total Stars (Not Found)')
|
||||
.get('/invalid-gist-id.json')
|
||||
.expectBadge({
|
||||
label: 'Stars',
|
||||
message: 'gist not found',
|
||||
color: 'red',
|
||||
link: [],
|
||||
})
|
||||
Reference in New Issue
Block a user