Skip to content

Commit 708c796

Browse files
committed
feat: testing comparing two different openapi files from base and head ref
1 parent a194f08 commit 708c796

File tree

6 files changed

+87
-12
lines changed

6 files changed

+87
-12
lines changed

package-lock.json

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"eslint-plugin-prettier": "^5.1.3",
8383
"jest": "^29.7.0",
8484
"make-coverage-badge": "^1.2.0",
85+
"openapi-types": "^12.1.3",
8586
"prettier": "^3.2.5",
8687
"prettier-eslint": "^16.3.0",
8788
"ts-jest": "^29.1.2",

src/main.ts

+29-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
import * as core from '@actions/core'
22
import { wait } from './wait'
33
import { stdout } from 'process'
4+
import { getFileFromBranch } from 'src/utils/getFileFromBranch'
5+
import { diffOpenapiObject } from 'src/utils/diffOpenapiObject'
46

57
/**
68
* The main function for the action.
79
* @returns {Promise<void>} Resolves when the action is complete.
810
*/
911
export async function run(): Promise<void> {
1012
try {
13+
// parse two openapi files
14+
const baseBranch = process.env.GITHUB_BASE_REF!
15+
const headBranch = process.env.GITHUB_HEAD_REF!
16+
const filePath = './openapi.json'
17+
18+
const baseFile = JSON.parse(
19+
getFileFromBranch(baseBranch, filePath).toString()
20+
)
21+
const headFile = JSON.parse(
22+
getFileFromBranch(headBranch, filePath).toString()
23+
)
24+
25+
const diff = diffOpenapiObject(baseFile, headFile)
26+
27+
console.log('diff', diff)
28+
const result = JSON.stringify(diff, null, 2)
29+
1130
const ms: string = core.getInput('milliseconds')
1231

1332
// Debug logs are only output if the `ACTIONS_STEP_DEBUG` secret is true
@@ -25,22 +44,20 @@ export async function run(): Promise<void> {
2544

2645
stdout.write('This is a single-line string\n')
2746

28-
const result = `
29-
This is a multi-line string
30-
31-
# API Differences
32-
33-
## ADDED
34-
---
47+
// const result = `
48+
// This is a multi-line string
3549

50+
// # API Differences
3651

37-
## MODIFIED
38-
---
52+
// ## ADDED
53+
// ---
3954

55+
// ## MODIFIED
56+
// ---
4057

41-
## DELETED
42-
---
43-
`
58+
// ## DELETED
59+
// ---
60+
// `
4461

4562
console.log(result)
4663

src/types/openapiTypes.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import type { OpenAPIV3 } from "openapi-types";
2+
3+
export type OpenapiTypes = OpenAPIV3.Document;

src/utils/diffOpenapiObject.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { OpenapiTypes } from 'src/types/openapiTypes'
2+
3+
export type DiffOpenapiObject = (
4+
startOpenapiObj: OpenapiTypes,
5+
targetOpenapiObj: OpenapiTypes
6+
) => OpenapiTypes
7+
8+
export const diffOpenapiObject: DiffOpenapiObject = (
9+
startOpenapiObj,
10+
targetOpenapiObj
11+
) => {
12+
// diff two objects and return the diff
13+
// diff not only keys but values, too.
14+
// if the value is an object, diff it recursively
15+
// if value is same don't return
16+
// return startOpenapiObj;
17+
const diff = (startObj: any, targetObj: any) => {
18+
const keys = new Set([...Object.keys(startObj), ...Object.keys(targetObj)])
19+
const diffObj: any = {}
20+
keys.forEach(key => {
21+
if (
22+
startObj[key] &&
23+
targetObj[key] &&
24+
typeof startObj[key] === 'object' &&
25+
typeof targetObj[key] === 'object'
26+
) {
27+
const diffResult = diff(startObj[key], targetObj[key])
28+
if (Object.keys(diffResult).length > 0) {
29+
diffObj[key] = diffResult
30+
}
31+
} else if (startObj[key] !== targetObj[key]) {
32+
diffObj[key] = targetObj[key]
33+
}
34+
})
35+
return diffObj
36+
}
37+
38+
return diff(startOpenapiObj, targetOpenapiObj)
39+
}

src/utils/getFileFromBranch.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { execSync } from 'child_process'
2+
import { readFileSync } from 'fs'
3+
4+
export function getFileFromBranch(branch: string, filePath: string): Buffer {
5+
execSync(`git fetch origin ${branch}`)
6+
execSync(`git checkout FETCH_HEAD -- ${filePath}`)
7+
return readFileSync(filePath)
8+
}

0 commit comments

Comments
 (0)