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

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
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": "2.3.1",
"license": "MIT",
"type": "module",
"repository": {
Expand Down
20 changes: 20 additions & 0 deletions packages/cli/src/helpers.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import { has } from 'lodash-es';

const DIR_NAME = getDirNameForCurrentFile(import.meta);

// helpers when when working with url to generate service
const DEFAULT_BRANCH = 'develop';
Copy link
Collaborator

Choose a reason for hiding this comment

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

There are times when we need to generate from feature branches and deploy to PR env.
We should handle that use case too.

Possibly, take branch as input via env variable?

Suggested change
const DEFAULT_BRANCH = 'develop';
const DEFAULT_BRANCH = process.env.GITHUB_BRANCH || 'develop';

Copy link
Author

Choose a reason for hiding this comment

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

done and tested this with a new branch and passing branch name in env variable

const URL_PREFIX = 'https://api.github.com/repos/harness/harness-core/contents/';

export const GITHUB_PAT = process.env.GITHUB_PAT;
export const GITHUB_API_ENDPOINT_URL = (yamlPath: string) =>
`${URL_PREFIX}${yamlPath}?ref=${DEFAULT_BRANCH}`;

// internal function
export function _convertToOpenAPI(schema: unknown): Promise<OpenAPIV3.Document> {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -53,3 +61,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(''),
);
}
37 changes: 31 additions & 6 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 {
GITHUB_API_ENDPOINT_URL,
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,47 @@ 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');
}

// 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 is not defined, please set GITHUB_PAT environment variable');
}

const configUrl = process.env.CI ? config.url : GITHUB_API_ENDPOINT_URL(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; charset=utf-8') {
const responseJson: any = await response.json();
const yamlContent = responseJson.content;
spec = yaml.load(b64DecodeUnicode(yamlContent), {
json: true,
schema: yaml.JSON_SCHEMA,
}) as OpenAPIV3.Document;

if (contentType === 'application/json') {
spec = (await response.json()) 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');
}
} else {
throw new Error('Neither file nor url provided');
Expand Down
Loading