Last active
January 27, 2024 12:00
-
-
Save ScriptRaccoon/b46635ce2c5ea8d74b42b6db20b2ebe4 to your computer and use it in GitHub Desktop.
Add MIT licenses to all your repos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TOKEN = "...." // your github access token | |
const owner = "..." // your github name | |
const blackList = [] // the names of the repos you don't want to process | |
const { Octokit } = require("@octokit/rest") // install this first | |
const octokit = new Octokit({ auth: TOKEN }) | |
const fs = require("fs") | |
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) | |
function licenseContent(year) { | |
return `Copyright (c) ${year} ${owner} | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE.` | |
} | |
// writes all your repos to a JSON file | |
async function writeRepos() { | |
const options = octokit.repos.listForAuthenticatedUser.endpoint.merge({ | |
per_page: 100, | |
}) | |
const repos = await octokit.paginate(options) | |
const my_repos = repos.filter( | |
(repo) => | |
repo.owner.login === owner && | |
!blackList.includes(repo.name) | |
) | |
const reduced_repos = my_repos.map((repo) => { | |
return { | |
name: repo.name, | |
year: repo.updated_at.split("-")[0], | |
} | |
}) | |
fs.writeFileSync("repos.json", JSON.stringify(reduced_repos)) | |
} | |
// adds a license file to a repo (unless it is already there) | |
async function add_license(repo) { | |
const year = new Date().getFullYear() | |
try { | |
await octokit.repos.getContent({ | |
owner: owner, | |
repo: repo.name, | |
path: "LICENSE", | |
}) | |
} catch (error) { | |
if (error.status === 404) { | |
try { | |
await octokit.repos.createOrUpdateFileContents({ | |
owner: owner, | |
repo: repo.name, | |
path: "LICENSE", | |
message: "Add MIT License", | |
content: Buffer.from(licenseContent(year)).toString( | |
"base64" | |
), | |
}) | |
console.log(`Added license to ${repo.name}`) | |
} catch (error) { | |
console.log(`Could not add license to ${repo.name}`) | |
console.error(error) | |
} | |
} else { | |
console.error(error) | |
} | |
} | |
} | |
// adds licenses to all repos in the JSON file | |
async function add_licenses() { | |
const repos = JSON.parse(fs.readFileSync("repos.json")) | |
for (const repo of repos) { | |
await add_license(repo) | |
await sleep(1000) | |
} | |
} | |
// execute this first to write all repos: | |
// writeRepos().catch(console.error) | |
// delete the repos from the file you want to exclude, | |
// or update the blackList array and run the command again | |
// then run the following to add the licenses | |
// add_licenses().catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment