forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-r-package.service.js
More file actions
93 lines (82 loc) · 2.6 KB
/
Copy pathgithub-r-package.service.js
File metadata and controls
93 lines (82 loc) · 2.6 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
'use strict'
const Joi = require('joi')
const { renderVersionBadge } = require('../version')
const { InvalidResponse } = require('..')
const { ConditionalGithubAuthV3Service } = require('./github-auth-service')
const { fetchRepoContent } = require('./github-common-fetch')
const { documentation } = require('./github-helpers')
const queryParamSchema = Joi.object({
filename: Joi.string(),
}).required()
const versionRegExp = /^Version:[\s]*(.+)$/m
module.exports = class GithubRPackageVersion extends (
ConditionalGithubAuthV3Service
) {
static category = 'version'
static route = {
base: 'github/r-package/v',
pattern: ':user/:repo/:branch*',
queryParamSchema,
}
static examples = [
{
title: 'GitHub R package version',
pattern: ':user/:repo',
namedParams: { user: 'mixOmicsTeam', repo: 'mixOmics' },
staticPreview: this.render({ version: '6.10.9' }),
documentation,
},
{
title: 'GitHub R package version (branch)',
pattern: ':user/:repo/:branch',
namedParams: { user: 'mixOmicsTeam', repo: 'mixOmics', branch: 'master' },
staticPreview: this.render({ version: '6.10.9', branch: 'master' }),
documentation,
},
{
title: 'GitHub R package version (subdirectory of monorepo)',
pattern: ':user/:repo',
namedParams: { user: 'mixOmicsTeam', repo: 'mixOmics' },
queryParams: { filename: 'subdirectory/DESCRIPTION' },
staticPreview: this.render({ version: '6.10.9' }),
documentation,
},
{
title: 'GitHub R package version (branch & subdirectory of monorepo)',
pattern: ':user/:repo/:branch',
namedParams: { user: 'mixOmicsTeam', repo: 'mixOmics', branch: 'master' },
queryParams: { filename: 'subdirectory/DESCRIPTION' },
staticPreview: this.render({ version: '6.10.9', branch: 'master' }),
documentation,
},
]
static defaultBadgeData = { label: 'R' }
static render({ version, branch }) {
return renderVersionBadge({
version,
tag: branch,
defaultLabel: 'R',
})
}
static transform(content, filename) {
const match = versionRegExp.exec(content)
if (!match) {
throw new InvalidResponse({
prettyMessage: `Version missing in ${filename}`,
})
}
return {
version: match[1],
}
}
async handle({ user, repo, branch }, { filename = 'DESCRIPTION' }) {
const content = await fetchRepoContent(this, {
user,
repo,
branch,
filename,
})
const { version } = this.constructor.transform(content, filename)
return this.constructor.render({ version, branch })
}
}