Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check whether extension has an OSI approved license #513

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions lib/checkLicense.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/********************************************************************************
* Copyright (c) 2022 TypeFox and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/

// @ts-check
const checkLicense = require("osi-license-checker");
const fs = require('fs');
const octokit = require('octokit');

/**
* Check if the extension has an OSI-approved open-source license
* @param {URL} url
* @param {string} owner
* @param {string} repo
* @param {octokit.Octokit} octokit a preauthenthicated octokit client
* @param {string} packagePath
*/
exports.checkLicense = async function(url, owner, repo, octokit, packagePath) {
try {
const manifest = JSON.parse(
await fs.promises.readFile(packagePath, "utf-8")
);
const license = manifest.license;
if (!license) {
if (!checkLicense.checkShorthand(license)) {
console.error(`Not an OSS license: ${license}`);
return false;
}

if (url.hostname !== "github.com") {
console.error("Can' check license on non-github repositories ");
return false;
}

const ghLicenseResponse = (
await octokit.rest.licenses.getForRepo({ owner, repo })
).data.license;
if (!checkLicense.checkShorthand(ghLicenseResponse.spdx_id)) {
console.error(
`Not an OSS license: ${ghLicenseResponse.name} (${ghLicenseResponse.spdx_id})`
);
return false;
}
return true;
}
} catch (e) {
console.error("Can't get license", e);
return false;
}
}
2 changes: 2 additions & 0 deletions lib/resolveExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Octokit = require('octokit').Octokit;
const readVSIXPackage = require('vsce/out/zip').readVSIXPackage;
const download = require('download');
const exec = require('./exec');
const { checkLicense } = require('./checkLicense');

const token = process.env.GITHUB_TOKEN;
if (!token) {
Expand Down Expand Up @@ -53,6 +54,7 @@ exports.resolveExtension = async function ({ id, repository, location }, ms) {
await exec(`git clone --filter=blob:none --recurse-submodules ${repository} ${repoPath}`, { quiet: true });

const packagePath = [repoPath, location, 'package.json'].filter(p => !!p).join('/');

/**
* @param {string} ref
* @returns {Promise<string | undefined>}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"download": "^8.0.0",
"minimist": "^1.2.5",
"octokit": "^1.7.0",
"osi-license-checker": "latest",
"ovsx": "latest",
"semver": "^7.1.3",
"vsce": "^2.3.0"
Expand Down
13 changes: 13 additions & 0 deletions publish-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ const path = require('path');
const semver = require('semver');
const exec = require('./lib/exec');
const { createVSIX } = require('vsce');
const { checkLicense } = require('./lib/checkLicense');
const { Octokit } = require('octokit');

const token = process.env.GITHUB_TOKEN;
if (!token) {
throw new Error("GITHUB_TOKEN env var is not set");
}
const octokit = new Octokit({ auth: token });

(async () => {
/**
Expand All @@ -32,6 +40,11 @@ const { createVSIX } = require('vsce');
packagePath = path.join(packagePath, extension.location);
}

const [owner, repo] = extension.repository.slice(1).split("/");
if (!(await checkLicense(new URL(extension.repository), owner, repo, octokit, packagePath+'/package.json'))) {
process.exit(1);
}

/** @type {import('ovsx').PublishOptions} */
let options;
if (context.file) {
Expand Down