From 3ec6a13217fecc1e31837b53654fe6c88a3ea83e Mon Sep 17 00:00:00 2001 From: S M Mahmudul Hasan Date: Mon, 13 Jan 2025 14:49:49 +0600 Subject: [PATCH] feat: add script for creating label and subscribing to repos (#1) * feat: add script for creating label and subscribing to repos * docs: add basic information about the repo and usage --- README.md | 22 ++++++++++++- owner/create-label.mjs | 53 ++++++++++++++++++++++++++++++++ triage/subscribe-to-org-repos.js | 52 +++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 owner/create-label.mjs create mode 100644 triage/subscribe-to-org-repos.js diff --git a/README.md b/README.md index f9dc151..efdb931 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,22 @@ # tooling -Tooling used in the project +Tooling used in the project. + +Simple scripts without extra fuss to help with the project. Written with less effort and more focus on the task at hand. + +## Scripts +### `owner/create-label.mjs` - Create Label +Allows you to create a label in all existing repositories in an organization. + +### `triage/subscribe-to-org-repos.js` - Subscribe to Org Repos +Allows you to subscribe to all repositories in an organization. + +## Usage +1. Clone the repository +2. Install the required dependency for the script you want to use +3. Make necessary changes to the script +4. Run the script + +## Note +- Your GitHub token should have the necessary permissions to perform the actions. + + diff --git a/owner/create-label.mjs b/owner/create-label.mjs new file mode 100644 index 0000000..874383e --- /dev/null +++ b/owner/create-label.mjs @@ -0,0 +1,53 @@ +import { Octokit } from '@octokit/rest'; + +const octokit = new Octokit({ + auth: 'token', // should have repo and admin:org scope +}); + +// Set the organization name, label name, color, and description +const orgName = 'org-name'; +const labelName = 'label-name'; +const labelColor = 'ff0000'; +const labelDescription = 'label-description'; + +async function createLabelInRepo(owner, repo) { + try { + await octokit.issues.createLabel({ + owner, + repo, + name: labelName, + color: labelColor, + description: labelDescription, + }); + console.log(`Label '${labelName}' created in ${repo}`); + } catch (error) { + if (error.status === 422) { + console.log(`Label '${labelName}' already exists in ${repo}`); + } else { + console.error( + `Failed to create label in ${repo}: ${error.message}` + ); + } + } +} + +async function createLabelInAllRepos() { + try { + const repos = await octokit.paginate(octokit.repos.listForOrg, { + org: orgName, + }); + + for (const repo of repos) { + await createLabelInRepo(orgName, repo.name); + } + + console.log('Label creation completed.'); + } catch (error) { + console.error( + `Error fetching repositories or creating labels: ${error.message}` + ); + process.exit(1); + } +} + +createLabelInAllRepos(); \ No newline at end of file diff --git a/triage/subscribe-to-org-repos.js b/triage/subscribe-to-org-repos.js new file mode 100644 index 0000000..10b820d --- /dev/null +++ b/triage/subscribe-to-org-repos.js @@ -0,0 +1,52 @@ +const axios = require('axios'); + +// Replace with your GitHub token and organization name +const GITHUB_TOKEN = 'token'; +const ORGANIZATION_NAME = 'pillarjs'; + +const subscribeToAllRepos = async () => { + try { + const headers = { + 'Authorization': `token ${GITHUB_TOKEN}`, + 'Accept': 'application/vnd.github.v3+json', + }; + + // Fetch all repositories of the organization + let page = 1; + let repos = []; + + while (true) { + const response = await axios.get(`https://api.github.com/orgs/${ORGANIZATION_NAME}/repos?per_page=100&page=${page}`, { headers }); + + if (response.data.length === 0) { + break; + } + + repos = repos.concat(response.data); + page++; + } + + console.log(`Found ${repos.length} repositories in the organization ${ORGANIZATION_NAME}.`); + + // Subscribe (watch) to each repository + for (const repo of repos) { + const { full_name } = repo; + try { + await axios.put( + `https://api.github.com/repos/${full_name}/subscription`, + { subscribed: true, ignored: false }, + { headers } + ); + console.log(`Subscribed to ${full_name}`); + } catch (error) { + console.error(`Failed to subscribe to ${full_name}: ${error.message}`); + } + } + + console.log('Finished subscribing to all repositories.'); + } catch (error) { + console.error('Error:', error.message); + } +}; + +subscribeToAllRepos(); \ No newline at end of file