-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathgithub.js
72 lines (60 loc) · 1.78 KB
/
github.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
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
import process from 'node:process';
import {execa} from 'execa';
import got from 'got';
import gh from 'github-url-to-object';
import {lintRule} from 'unified-lint-rule';
const githubRule = lintRule('remark-lint:awesome-github', async (ast, file) => {
const {dirname} = file;
try {
const {stdout: gitBranch} = await execa('git', ['branch', '--show-current']);
let remoteName;
if (gitBranch) {
const {stdout} = await execa('git', ['config', '--get', `branch.${gitBranch}.remote`]);
remoteName = stdout;
} else {
const {stdout} = await execa('git', [
'config',
'--default',
'origin',
'--get',
'clone.defaultRemoteName',
]);
remoteName = stdout;
}
const {stdout: remoteUrl} = await execa('git', ['remote', 'get-url', '--push', remoteName], {cwd: dirname});
const githubUrls = gh(remoteUrl);
if (!githubUrls) {
file.message('Repository should be on GitHub');
return;
}
const headers = {
Accept: 'application/vnd.github.mercy-preview+json',
'User-Agent': 'awesome-lint',
};
if (process.env.github_token) {
headers.Authorization = `token ${process.env.github_token}`;
}
let data;
try {
data = await got(githubUrls.api_url, {headers}).json();
} catch {
// Handle HTTP errors
return;
}
if (!data.description) {
file.message('The repository should have a description');
}
if (!data.topics.includes('awesome')) {
file.message('The repository should have "awesome" as a GitHub topic');
}
if (!data.topics.includes('awesome-list')) {
file.message('The repository should have "awesome-list" as a GitHub topic');
}
if (!data.license) {
file.message('License was not detected by GitHub');
}
} catch {
file.message('Awesome list must reside in a valid git repository');
}
});
export default githubRule;