forked from todogroup/repolinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest-practices-badge-present.js
44 lines (42 loc) · 1.5 KB
/
best-practices-badge-present.js
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
// Copyright 2022 TODO Group. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
const fetch = require('node-fetch')
const Result = require('../lib/result')
const fileContents = require('./file-contents')
const bestPracticesRegExp =
'https://bestpractices\\.coreinfrastructure\\.org(/\\w+)?/projects/\\d+'
module.exports = async function (fileSystem, options = {}, git) {
const readmeContainsBadge = await fileContents(
fileSystem,
{
globsAll: ['README*'],
content: bestPracticesRegExp,
nocase: true,
flags: 'i',
'fail-on-non-existent': true,
'human-readable-content': 'Best Practices Badge'
},
undefined,
undefined,
git
)
if (!readmeContainsBadge.passed || !options.minPercentage) {
return readmeContainsBadge
}
const readmePath = readmeContainsBadge.targets[0].path
const targets = [{ path: readmePath }]
const readmeContents = await fileSystem.getFileContents(readmePath)
const bestPracticesUrl = readmeContents.match(
new RegExp(bestPracticesRegExp, 'i')
)[0]
const bestPracticesResponse = await fetch(`${bestPracticesUrl}.json`)
if (!bestPracticesResponse.ok) {
return new Result('Invalid Best Practices Badge URL', targets, false)
}
const bestPracticesData = await bestPracticesResponse.json()
const passed = bestPracticesData.tiered_percentage >= options.minPercentage
const message = `Best Practices Badge ${
passed ? 'reached' : 'did not reach'
} minimum level`
return new Result(message, targets, passed)
}