Updated checker to use JS; Added initial setup script as a Turbo command

This commit is contained in:
gbuomprisco
2024-09-22 15:42:27 +02:00
parent 1433acfff7
commit b0d052a108
7 changed files with 174 additions and 66 deletions

View File

@@ -1,23 +0,0 @@
#!/bin/sh
set -e
if [ "$NODE_ENV" = "production" ]; then
exit 0
fi
GIT_USER=$(git config user.name)
GIT_EMAIL=$(git config user.email)
if [ -z "$GIT_USER" ]; then
echo "Please set the git user name with the command 'git config user.name <username>'. The username needs to match the username in your Makerkit organization."
exit 1
fi
STATUS_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X GET "https://makerkit.dev/api/license/check?username=$GIT_USER&email=$GIT_EMAIL")
if [ "$STATUS_CODE" = "200" ]; then
exit 0
fi
exit 1

View File

@@ -1,6 +0,0 @@
#!/bin/sh
set -e
sh visibility.sh
sh license.sh

View File

@@ -3,6 +3,6 @@
"private": true,
"version": "0.1.0",
"scripts": {
"dev": "sh main.sh"
"dev": "node src/index.js"
}
}

View File

@@ -0,0 +1,99 @@
import { execSync } from 'child_process';
async function checkLicense() {
let gitUser, gitEmail;
try {
gitUser = execSync('git config user.name').toString().trim();
gitEmail = execSync('git config user.email').toString().trim();
} catch (error) {
console.error('Error getting git config:', error.message);
process.exit(1);
}
if (!gitUser) {
console.error(
"Please set the git user name with the command 'git config user.name <username>'. The username needs to match the username in your Makerkit organization.",
);
process.exit(1);
}
const res = await fetch(
`https://makerkit.dev/api/license/check?username=${encodeURIComponent(gitUser)}&email=${encodeURIComponent(gitEmail)}`,
);
if (res.status === 200) {
return Promise.resolve();
} else {
return Promise.reject(
new Error(`License check failed with status code: ${res.status}`),
);
}
}
function checkVisibility() {
let remoteUrl;
try {
remoteUrl = execSync('git config --get remote.origin.url')
.toString()
.trim();
} catch (error) {
console.error('Error getting git remote URL:', error.message);
process.exit(1);
}
if (!remoteUrl.includes('github.com')) {
return Promise.resolve();
}
let ownerRepo;
if (remoteUrl.startsWith('https://github.com/')) {
ownerRepo = remoteUrl.slice('https://github.com/'.length);
} else if (remoteUrl.startsWith('git@github.com:')) {
ownerRepo = remoteUrl.slice('git@github.com:'.length);
} else {
console.error('Unsupported GitHub URL format');
process.exit(1);
}
ownerRepo = ownerRepo.replace(/\.git$/, '');
return fetch(`https://api.github.com/repos/${ownerRepo}`)
.then((res) => {
if (res.status === 200) {
return res.json();
} else if (res.status === 404) {
return Promise.resolve();
} else {
return Promise.reject(
new Error(
`GitHub API request failed with status code: ${res.status}`,
),
);
}
})
.then((data) => {
if (data && !data.private) {
console.error(
'The repository has been LEAKED on GitHub. Please delete the repository. A DMCA Takedown Request will automatically be requested in the coming hours.',
);
process.exit(1);
}
});
}
async function main() {
if (process.env.NODE_ENV === 'production') {
return;
}
try {
await checkVisibility();
await checkLicense();
} catch (error) {
process.exit(1);
}
}
void main();

View File

@@ -1,36 +0,0 @@
#!/bin/bash
set -e
remote_url=$(git config --get remote.origin.url)
if [[ $remote_url == *"github.com"* ]]; then
if [[ $remote_url == https://github.com/* ]]; then
owner_repo=${remote_url#https://github.com/}
elif [[ $remote_url == git@github.com:* ]]; then
owner_repo=${remote_url#git@github.com:}
else
echo "Unsupported GitHub URL format"
exit 1
fi
owner_repo=${owner_repo%.git}
api_url="https://api.github.com/repos/$owner_repo"
response=$(curl -s -o /dev/null -w "%{http_code}" $api_url)
if [ $response -eq 200 ]; then
visibility=$(curl -s $api_url | grep -o '"private": \(true\|false\)' | awk '{print $2}')
if [ "$visibility" = "false" ]; then
echo "The repository has been LEAKED on GitHub. Please delete the repository. A DMCA Takedown Request will automatically be requested in the coming hours."
exit 1
else
exit 0
fi
elif [ $response -eq 404 ]; then
exit 0
else
exit 1
fi
else
exit 0
fi