From b7f9e49528d71704bf51d0f45c189002a1ec7e16 Mon Sep 17 00:00:00 2001 From: ashanfernando Date: Tue, 11 Mar 2025 18:53:56 +0530 Subject: [PATCH] checking lane exist --- .github/workflows/main.yml | 2 +- dist/index.js | 48 ++++++++++++++++++++++++++++++++++++++ scripts/graphql.ts | 25 ++++++++++++++++++++ scripts/pull-request.ts | 14 +++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 scripts/graphql.ts diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c32a051..89f9b1f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -27,6 +27,6 @@ jobs: - name: Add remote scope run: cd test-data && bit remote add http://localhost:4000 - name: Bit Pull Request - uses: bit-tasks/pull-request@main + uses: bit-tasks/pull-request@dev - name: Bit Lanes run: cd test-data && bit lane list --details diff --git a/dist/index.js b/dist/index.js index a4afbca..8f36c87 100644 --- a/dist/index.js +++ b/dist/index.js @@ -10908,6 +10908,40 @@ catch (error) { } +/***/ }), + +/***/ 8946: +/***/ ((__unused_webpack_module, exports) => { + +"use strict"; + +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.scopeQuery = void 0; +const GRAPHQL_ENDPOINT = 'https://api.v2.bit.cloud/graphql'; +const scopeQuery = (id, token) => { + const query = ` + query GET_SCOPE($scopeId: String!) { + getScope(id: $scopeId) { + id + } + } + `; + const variables = { scopeId: id }; + return fetch(GRAPHQL_ENDPOINT, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify({ + query, + variables, + }), + }).then(response => response.json()); +}; +exports.scopeQuery = scopeQuery; + + /***/ }), /***/ 595: @@ -10951,6 +10985,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); const exec_1 = __nccwpck_require__(1514); const github_1 = __nccwpck_require__(5438); const core = __importStar(__nccwpck_require__(2186)); +const graphql_1 = __nccwpck_require__(8946); const createSnapMessageText = (githubToken, repo, owner, prNumber) => __awaiter(void 0, void 0, void 0, function* () { const octokit = (0, github_1.getOctokit)(githubToken); let messageText = "CI"; @@ -11099,10 +11134,23 @@ const createVersionLabels = (githubToken, repo, owner, prNumber, status, version } }); function run(githubToken, repo, owner, prNumber, laneName, versionLabel, versionLabelsColors, wsDir, args) { + var _a, _b; return __awaiter(this, void 0, void 0, function* () { const org = process.env.ORG; const scope = process.env.SCOPE; + const token = process.env.BIT_CONFIG_USER_TOKEN || ""; let statusRaw = ""; + const scopeErrorMessage = `Scope: ${org}.${scope} does not exist or you don't have access to it`; + try { + const jsonData = yield (0, graphql_1.scopeQuery)(`${org}.${scope}`, token); + if (!((_b = (_a = jsonData === null || jsonData === void 0 ? void 0 : jsonData.data) === null || _a === void 0 ? void 0 : _a.getScope) === null || _b === void 0 ? void 0 : _b.id)) { + throw new Error(scopeErrorMessage); + } + } + catch (error) { + console.log(error); + throw new Error(scopeErrorMessage); + } yield (0, exec_1.exec)("bit", ["status", "--json"], { cwd: wsDir, listeners: { diff --git a/scripts/graphql.ts b/scripts/graphql.ts new file mode 100644 index 0000000..02a460b --- /dev/null +++ b/scripts/graphql.ts @@ -0,0 +1,25 @@ +const GRAPHQL_ENDPOINT = 'https://api.v2.bit.cloud/graphql'; + +export const scopeQuery = (id: string, token: string) => { + const query = ` + query GET_SCOPE($scopeId: String!) { + getScope(id: $scopeId) { + id + } + } + `; + + const variables = { scopeId: id }; + + return fetch(GRAPHQL_ENDPOINT, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify({ + query, + variables, + }), + }).then(response => response.json()); +}; \ No newline at end of file diff --git a/scripts/pull-request.ts b/scripts/pull-request.ts index 3ae8fa5..76ef7ce 100644 --- a/scripts/pull-request.ts +++ b/scripts/pull-request.ts @@ -1,6 +1,7 @@ import { exec } from "@actions/exec"; import { getOctokit } from "@actions/github"; import * as core from "@actions/core"; +import { scopeQuery } from "./graphql"; const createSnapMessageText = async ( githubToken: string, @@ -220,8 +221,21 @@ export default async function run( ) { const org = process.env.ORG; const scope = process.env.SCOPE; + const token = process.env.BIT_CONFIG_USER_TOKEN || ""; let statusRaw = ""; + const scopeErrorMessage = `Scope: ${org}.${scope} does not exist or you don't have access to it`; + + try { + const jsonData = await scopeQuery(`${org}.${scope}`, token); + + if (!jsonData?.data?.getScope?.id) { + throw new Error(scopeErrorMessage); + } + } catch (error) { + console.log(error); + throw new Error(scopeErrorMessage); + } await exec("bit", ["status", "--json"], { cwd: wsDir,