Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"execa": "^1.0.0",
"git-clone": "^0.1.0",
"github-slugger": "^1.2.0",
"github-url-to-object": "^4.0.4",
"globby": "^9.0.0",
"got": "^9.6.0",
"is-url-superb": "^2.0.0",
"mdast-util-to-string": "^1.0.4",
"meow": "^5.0.0",
Expand Down
61 changes: 61 additions & 0 deletions rules/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';
const execa = require('execa');
const got = require('got');
const gh = require('github-url-to-object');
const rule = require('unified-lint-rule');

module.exports = rule('remark-lint:awesome/github', async (ast, file) => {
const {dirname} = file;

try {
const remoteUrl = await execa.stdout('git', [
'config',
'--get',
'remote.origin.url'
], {
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}`;
}

const res = await got.get(githubUrls.api_url, {
headers,
json: true
});

const data = res.body;
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 (_) {
// Most likely not a Git repository
file.message('Awesome list must reside in a valid git repository');
}
});

// For stubbing
module.exports.execa = execa;
module.exports.got = got;
1 change: 1 addition & 0 deletions rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = [
require('./badge'),
require('./contributing'),
require('./git-repo-age'),
require('./github'),
require('./license'),
require('./list-item'),
require('./no-ci-badge'),
Expand Down
Empty file added test/fixtures/github/0.md
Empty file.
140 changes: 140 additions & 0 deletions test/rules/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import test from 'ava';
import sinon from 'sinon';
import lint from '../_lint';

const github = require('../../rules/github');

const config = {
plugins: [
github
]
};

let sandbox;

test.beforeEach(() => {
sandbox = sinon.createSandbox();
});

test.afterEach.always(() => {
sandbox.restore();
});

test.serial('github - error invalid git repo', async t => {
const execaStub = sandbox.stub(github.execa, 'stdout');

execaStub
.throws(new Error('"git" command not found'));

const messages = await lint({config, filename: 'test/fixtures/github/0.md'});
t.deepEqual(messages, [
{
ruleId: 'awesome/github',
message: 'Awesome list must reside in a valid git repository'
}
]);
});

test.serial('github - repo without description and license', async t => {
const execaStub = sandbox.stub(github.execa, 'stdout');
const gotStub = sandbox.stub(github.got, 'get');

execaStub
.withArgs('git', ['config', '--get', 'remote.origin.url'])
.returns('[email protected]:sindresorhus/awesome-lint-test.git');

gotStub
.withArgs('https://api.github.com/repos/sindresorhus/awesome-lint-test')
.returns({
body: {
description: null,
topics: ['awesome', 'awesome-list'],
license: null
}
});

const messages = await lint({config, filename: 'test/fixtures/github/0.md'});
t.deepEqual(messages, [
{
ruleId: 'awesome/github',
message: 'The repository should have a description'
}, {
ruleId: 'awesome/github',
message: 'License was not detected by GitHub'
}
]);
});

test.serial('github - missing topic awesome-list', async t => {
const execaStub = sandbox.stub(github.execa, 'stdout');
const gotStub = sandbox.stub(github.got, 'get');

execaStub
.withArgs('git', ['config', '--get', 'remote.origin.url'])
.returns('[email protected]:sindresorhus/awesome-lint-test.git');

gotStub
.withArgs('https://api.github.com/repos/sindresorhus/awesome-lint-test')
.returns({
body: {
description: 'Awesome lint',
topics: ['awesome'],
license: {
key: 'mit'
}
}
});

const messages = await lint({config, filename: 'test/fixtures/github/0.md'});
t.deepEqual(messages, [
{
ruleId: 'awesome/github',
message: 'The repository should have "awesome-list" as a GitHub topic'
}
]);
});

test.serial('github - missing topic awesome', async t => {
const execaStub = sandbox.stub(github.execa, 'stdout');
const gotStub = sandbox.stub(github.got, 'get');

execaStub
.withArgs('git', ['config', '--get', 'remote.origin.url'])
.returns('[email protected]:sindresorhus/awesome-lint-test.git');

gotStub
.withArgs('https://api.github.com/repos/sindresorhus/awesome-lint-test')
.returns({
body: {
description: 'Awesome lint',
topics: ['awesome-list'],
license: {
key: 'mit'
}
}
});

const messages = await lint({config, filename: 'test/fixtures/github/0.md'});
t.deepEqual(messages, [
{
ruleId: 'awesome/github',
message: 'The repository should have "awesome" as a GitHub topic'
}
]);
});

test.serial('github - remote origin is an GitLab repo', async t => {
const execaStub = sandbox.stub(github.execa, 'stdout');

execaStub
.withArgs('git', ['config', '--get', 'remote.origin.url'])
.returns('https://gitlab.com/sindresorhus/awesome-lint-test.git');

const messages = await lint({config, filename: 'test/fixtures/github/0.md'});
t.deepEqual(messages, [
{
ruleId: 'awesome/github',
message: 'Repository should be on GitHub'
}
]);
});