From 9ca13e1e668ab027d1b4b9a23fdf5c7712866786 Mon Sep 17 00:00:00 2001 From: Leo Ribeiro Date: Fri, 10 Nov 2023 18:10:03 -0500 Subject: [PATCH] Script to add repetitive tasks to multiple repos --- scripts/github/github-create-issues.js | 107 +++++++++++++++++++++++++ scripts/github/github-create-labels.js | 3 +- 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 scripts/github/github-create-issues.js diff --git a/scripts/github/github-create-issues.js b/scripts/github/github-create-issues.js new file mode 100644 index 0000000..da7ac43 --- /dev/null +++ b/scripts/github/github-create-issues.js @@ -0,0 +1,107 @@ +import "dotenv/config"; +import { Octokit } from "@octokit/core"; + +const octokit = new Octokit({ + auth: process.env["GH_TOKEN"], // Your GitHub token from environment variable +}); + +const owner = process.env["GH_OWNER"]; // Owner of the repository from environment variable + +const projectId = "PVT_kwDOBaHX684AX9Bo"; // SDK Development + +const repos = [ + { repo: "tbdex-rs", labels: ["cicd"] }, + { repo: "tbdex-js", labels: ["cicd"] }, + { repo: "tbdex-kt", labels: ["cicd"] }, + { repo: "web5-kt", labels: ["cicd"] }, + { repo: "web5-js", labels: ["cicd"] }, + { repo: "web5-rs", labels: ["cicd"] }, +]; + +const issueTitle = "Setup artifacts publishing"; + +const issueBody = ` +We need to have an easy automated workflow to publish artifacts to the corresponding sdk registry (npm, maven, crates etc.), and also publish to the internal Artifactory registry too +`; + +async function createIssue(repoName, issueTitle, issueBody, labels) { + try { + console.info(`Creating issue in ${repoName}...`) + const issueResponse = await octokit.request( + "POST /repos/{owner}/{repo}/issues", + { + owner, + repo: repoName, + title: issueTitle, + body: issueBody, + labels, + } + ); + console.info(`Issue created in ${repoName}: ${issueResponse.data.html_url}`); + + await addIssueToProject(issueResponse.data.node_id); + } catch (error) { + console.error( + `Error creating and associating issue in ${repoName}: ${error.message}` + ); + } +} + +async function addIssueToProject(issueId) { + try { + const mutation = ` + mutation ($projectId: ID!, $contentId: ID!) { + addProjectV2ItemById(input: {projectId: $projectId, contentId: $contentId}) { + item { + id + } + } + } + `; + await octokit.graphql(mutation, { + projectId, + contentId: issueId, + }); + console.info(`Issue added to project: ${issueId}`); + } catch (error) { + console.error(`Error adding issue to project: ${error.message}`); + } +} + +async function createIssues() { + for (const repo of repos) { + await createIssue(repo.repo, issueTitle, issueBody, repo.labels); + } +} + +/** + * Used to list all projects ids,so we can update the script + */ +async function listOrgProjects() { + const query = ` + query ($orgName: String!) { + organization(login: $orgName) { + projectsV2(first: 100) { + nodes { + id + title + url + } + } + } + } + `; + + try { + const response = await octokit.graphql(query, { orgName: owner }); + const projects = response.organization.projectsV2.nodes; + projects.forEach(project => { + console.info(`${project.id} - Project Title: ${project.title}, URL: ${project.url}`); + }); + } catch (error) { + console.error(`Error fetching projects: ${error.message}`); + } +} + +createIssues().catch(console.error); +// listOrgProjects().catch(console.error); diff --git a/scripts/github/github-create-labels.js b/scripts/github/github-create-labels.js index b8c6f7a..4fa87f2 100644 --- a/scripts/github/github-create-labels.js +++ b/scripts/github/github-create-labels.js @@ -30,7 +30,8 @@ const labelsToCreate = [ { "name": "tbdex-orderstatus", "color": "D68910", "description": "tbDEX Order-Status Message" }, { "name": "tbdex-close", "color": "34495E", "description": "tbDEX Close Message" }, { "name": "tbdex-server", "color": "3498DB", "description": "HTTP server for tbDEX PFIs " }, - { "name": "tbdex-client", "color": "E74C3C", "description": "HTTP client for tbDEX wallets" } + { "name": "tbdex-client", "color": "E74C3C", "description": "HTTP client for tbDEX wallets" }, + { "name": "cicd", "color": "94D114", "description": "CI/CD and Automation" }, ]; async function fetchExistingLabels() {