Skip to content

Commit

Permalink
Merge pull request #1 from Bandwidth/SWI-2821
Browse files Browse the repository at this point in the history
SWI-2821 Migrate to Internal Action
  • Loading branch information
ckoegel authored Jun 13, 2023
2 parents 1eef28e + 18adec4 commit 6a6cf59
Show file tree
Hide file tree
Showing 12 changed files with 11,560 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Test the CLI
name: Test CLI

on:
pull_request:

jobs:
test-cli:
name: Test CLI
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Test CLI
run: |
npm i
npm run test
21 changes: 21 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: API Specs Combine Action
description: A GitHub Action that combines the Bandwidth Product API specs into one file for SDK generation.
inputs:
username:
required: false
description: Github Username
default: DX-Bandwidth
token:
required: true
description: Github Token

runs:
using: "composite"
steps:
- name: Clone and Combine Product Specs
run: |
git clone https://${{ inputs.username }}:${{ inputs.token }}@github.com/Bandwidth/api-specs
cd ${{ github.action_path }}
npm i
node index.js --config oas-merge-config.yml --path ${{ github.workspace }}
shell: bash
75 changes: 75 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const { merge, isErrorResult } = require('openapi-merge');
const argv = require('minimist')(process.argv.slice(2));

function buildSpecArray(config, basePath) {
const mergeArray = [];
for (i in config.inputs) {
let apiSpec = yaml.load(
fs.readFileSync(`${basePath}${configFile.inputs[i].inputFile}`, 'utf8')
);
let servers = apiSpec.servers;

for (specPath in apiSpec.paths) {
apiSpec.paths[specPath].servers = servers;
}

delete apiSpec.info;
delete apiSpec.servers;

mergeObj = {
oas: apiSpec,
};

mergeArray.push(mergeObj);
}
return mergeArray;
}

function mergeSpecs(mergeArray) {
const mergeResult = merge(mergeArray);
if (isErrorResult(mergeResult)) {
console.error(`${mergeResult.message} (${mergeResult.type})`);
} else {
console.log(`Merge successful!`);
// console.log(JSON.stringify(mergeResult.output, null, 2));
return mergeResult;
}
}

function main(config, basePath) {
const mergeArray = buildSpecArray(config, basePath);
const apiSpec = mergeSpecs(mergeArray).output;

let info = {
title: 'Bandwidth',
description: `Bandwidth's Communication APIs`,
contact: {
name: 'Bandwidth',
url: 'https://dev.bandwidth.com',
email: 'letstalk@bandwidth.com',
},
version: '1.0.0',
};
apiSpec.info = info;

/**
* Only save the file if the -t/--test flag is not present
*/
fileExtension = path.extname(configFile.output);
if( argv.t != true && argv.test != true ){
if(fileExtension == '.json'){
fs.writeFileSync(`${basePath}${configFile.output}`, JSON.stringify(apiSpec, null, 4), 'utf8');
} else if(fileExtension == '.yaml' || fileExtension == '.yml'){
fs.writeFileSync(`${basePath}${configFile.output}`, yaml.dump(apiSpec));
} else {
throw new Error('Unsupported output file type. Only `.json`, `.yaml`, and `.yml` are supported.');
}
}
}

const configFile = yaml.load(fs.readFileSync(argv.c || argv.config, 'utf8'));
const basePath = argv.path;
main(configFile, basePath);
7 changes: 7 additions & 0 deletions oas-merge-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
inputs:
- inputFile: "/api-specs/external/messaging.yml"
- inputFile: "/api-specs/external/voice.yml"
- inputFile: "/api-specs/external/multi-factor-auth.yml"
- inputFile: "/api-specs/external/phone-number-lookup.yml"
output: "/api-specs/bandwidth.yml"
Loading

0 comments on commit 6a6cf59

Please sign in to comment.