diff --git a/.changeset/thin-hairs-play.md b/.changeset/thin-hairs-play.md new file mode 100644 index 0000000..9aa449e --- /dev/null +++ b/.changeset/thin-hairs-play.md @@ -0,0 +1,5 @@ +--- +'@harnessio/oats-cli': major +--- + +support Open API yaml URL with Github API diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index a247574..b88629a 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -5,6 +5,8 @@ on: - opened - reopened - synchronize +env: + CI: true jobs: eslint: name: ESLint diff --git a/packages/cli/.env.example b/packages/cli/.env.example new file mode 100644 index 0000000..a292328 --- /dev/null +++ b/packages/cli/.env.example @@ -0,0 +1,4 @@ +GITHUB_PAT=GITHUB_PAT +ORGANIZATION=ORGANIZATION +REPO=REPO +BRANCH=BRANCH \ No newline at end of file diff --git a/packages/cli/README.md b/packages/cli/README.md index 933510b..94adb03 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -37,6 +37,21 @@ Options: ### Available commands +#### Environment Variables + +Consumer needs to pass these environment variables to fetch OpenAPI yaml of respective service from respective repository. +These should be kept in a .env file and keep the file in the root folder of consumer. + +**GITHUB_PAT**=GITHUB_PAT // Github Personal Access Token + +**ORGANIZATION**=ORGANIZATION // harness, wings-software etc. + +**REPO**=REPO // harness-core etc. + +**BRANCH**=BRANCH // develop, master etc. + +Use .env.example file in the current folder to quick start. + ### import ``` diff --git a/packages/cli/package.json b/packages/cli/package.json index bf6c8ea..3fc20bf 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "name": "@harnessio/oats-cli", - "version": "2.3.0", + "version": "3.0.0", "license": "MIT", "type": "module", "repository": { diff --git a/packages/cli/src/helpers.mts b/packages/cli/src/helpers.mts index dc3a829..8da9128 100644 --- a/packages/cli/src/helpers.mts +++ b/packages/cli/src/helpers.mts @@ -10,6 +10,17 @@ import { has } from 'lodash-es'; const DIR_NAME = getDirNameForCurrentFile(import.meta); +// helpers when when working with url to generate service +export const GITHUB_PAT = process.env.GITHUB_PAT; +const ORGANIZATION = process.env.ORGANIZATION || 'harness'; +const REPO = process.env.REPO || 'harness-core'; +const BRANCH = process.env.BRANCH || 'develop'; + +const URL_PREFIX = `https://api.github.com/repos/${ORGANIZATION}/${REPO}/contents/`; + +export const generateGithubApiEndpointUrl = (yamlPath: string) => + `${URL_PREFIX}${yamlPath}?ref=${BRANCH}`; + // internal function export function _convertToOpenAPI(schema: unknown): Promise { return new Promise((resolve, reject) => { @@ -53,3 +64,15 @@ export function getDirNameForCurrentFile(meta: ImportMeta): string { export function pathToTemplate(val: string): string { return val.replace(/\{/g, '${'); } + +export function b64DecodeUnicode(str: any) { + // Going backwards: from bytestream, to percent-encoding, to original string. + return decodeURIComponent( + atob(str) + .split('') + .map(function (c) { + return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); + }) + .join(''), + ); +} diff --git a/packages/cli/src/loadSpecFromFileOrUrl.mts b/packages/cli/src/loadSpecFromFileOrUrl.mts index cb7fcee..996fb66 100644 --- a/packages/cli/src/loadSpecFromFileOrUrl.mts +++ b/packages/cli/src/loadSpecFromFileOrUrl.mts @@ -6,7 +6,13 @@ import type { OpenAPIV3 } from 'openapi-types'; import yaml from 'js-yaml'; import { generateOpenAPISpec } from './generateOpenAPISpec.mjs'; -import { _convertToOpenAPI, logInfo } from './helpers.mjs'; +import { + generateGithubApiEndpointUrl, + GITHUB_PAT, + _convertToOpenAPI, + b64DecodeUnicode, + logInfo, +} from './helpers.mjs'; import type { IServiceConfig } from './config.mjs'; import type { IPluginReturn } from './plugin.mjs'; @@ -32,29 +38,51 @@ export async function loadSpecFromFileOrUrl(config: IServiceConfig): Promise