Skip to content

Commit

Permalink
feat: add script for creating label and subscribing to repos (#1)
Browse files Browse the repository at this point in the history
* feat: add script for creating label and subscribing to repos
* docs: add basic information about the repo and usage
  • Loading branch information
IamLizu authored Jan 13, 2025
1 parent f02c082 commit 3ec6a13
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.


53 changes: 53 additions & 0 deletions owner/create-label.mjs
Original file line number Diff line number Diff line change
@@ -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();
52 changes: 52 additions & 0 deletions triage/subscribe-to-org-repos.js
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 3ec6a13

Please sign in to comment.