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

Adding initial scripts #1

Merged
merged 2 commits into from
Jan 13, 2025
Merged
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
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';
Copy link
Member

@bjohansebas bjohansebas Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be passed as command-line arguments using process.argv

const ORGANIZATIONS = process.argv.slice(2)

for (const arg of ORGANIZATIONS) {
...
}

Or simply add the three organizations.


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();