-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
3 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
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. | ||
|
||
|
This file contains 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
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(); |
This file contains 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
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(); |