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

fix: [PL-56199]: support Open API yaml URL with Github API #34

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
2 changes: 2 additions & 0 deletions .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
- opened
- reopened
- synchronize
env:
CI: true
jobs:
eslint:
name: ESLint
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GITHUB_PAT=GITHUB_PAT
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we avoid hardcoded design dependency on Github?
considering that we have most of our repos moving to Harness Code now, we should support querying harness0.harness.io with the Harness PAT as well.

a simpler, more generic design would be to just take a complete url as a param, instead of taking org + repo + branch and constructing github urls via "magic".

ORGANIZATION=ORGANIZATION
REPO=REPO
BRANCH=BRANCH
15 changes: 15 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@harnessio/oats-cli",
"version": "2.3.0",
"version": "3.0.0",
"license": "MIT",
"type": "module",
"repository": {
Expand Down
23 changes: 23 additions & 0 deletions packages/cli/src/helpers.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenAPIV3.Document> {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there a way to avoid the need for this decoder? few concerns:

  1. it's very specific to github it seems
  2. it looks like magic code.. why is it doing what it's doing?

Copy link
Author

Choose a reason for hiding this comment

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

Github API returns bas64 encoded content, hence need to decode the content.

// 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(''),
);
}
44 changes: 36 additions & 8 deletions packages/cli/src/loadSpecFromFileOrUrl.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -33,28 +39,50 @@ export async function loadSpecFromFileOrUrl(config: IServiceConfig): Promise<IPl
? yaml.load(content, { json: true, schema: yaml.JSON_SCHEMA })
: JSON.parse(content);
} catch (_) {
throw new Error('Something went wrong while trying to parse contents');
throw new Error('Something went wrong while trying to parse contents from FILE');
namanharness marked this conversation as resolved.
Show resolved Hide resolved
}

// transform the spec using given transformer
} else if (config.url) {
// read from URL
logInfo('Fetching data from URL');
const response = await fetch(config.url);

if (!GITHUB_PAT && !process.env.CI) {
throw new Error(
'GITHUB_PAT (Personal Access Token) is not defined, please set GITHUB_PAT environment variable',
);
}

const configUrl = process.env.CI ? config.url : generateGithubApiEndpointUrl(config.url);
const configHeaders = process.env.CI
? {}
: { headers: { Authorization: `Bearer ${GITHUB_PAT}` } };

const response = await fetch(configUrl, {
...configHeaders,
});

const contentType = response.headers.get('Content-Type');
try {
logInfo('Parsing data from API');

if (contentType === 'application/json') {
spec = (await response.json()) as OpenAPIV3.Document;
logInfo(`Detected format: JSON`);
if (contentType === 'application/json; charset=utf-8') {
mayankVermaHarness marked this conversation as resolved.
Show resolved Hide resolved
const responseJson: any = await response.json();
const responseContent = responseJson.content;
const responseContentDecoded = b64DecodeUnicode(responseContent);
try {
spec = yaml.load(responseContentDecoded) as OpenAPIV3.Document;
logInfo(`Detected format: YAML`);
} catch (_) {
spec = JSON.parse(responseContentDecoded) as OpenAPIV3.Document;
logInfo(`Detected format: JSON`);
}
} else {
const txt = await response.text();
spec = yaml.load(txt) as OpenAPIV3.Document;
logInfo(`Detected format: YAML`);
}
} catch (_) {
throw new Error('Something went wrong while trying to parse contents');
throw new Error('Something went wrong while trying to parse contents from URL');
namanharness marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
throw new Error('Neither file nor url provided');
Expand Down
Loading