Skip to content

Commit 488cda5

Browse files
committed
chore: add yaml spec
1 parent 2ab6934 commit 488cda5

File tree

7 files changed

+194
-30
lines changed

7 files changed

+194
-30
lines changed

.github/workflows/ci.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,18 @@ jobs:
2222
with:
2323
node-version: 20
2424

25-
- name: Upload OpenAPI spec
26-
id: upload-openapi-spec
25+
- name: Upload JSON OpenAPI spec
26+
id: upload-json-openapi-spec
2727
uses: ./
2828
with:
29+
dry-run: true
2930
hey-api-token: ${{ secrets.HEY_API_TOKEN }}
3031
path-to-openapi: ./openapi.json
32+
33+
- name: Upload YAML OpenAPI spec
34+
id: upload-yaml-openapi-spec
35+
uses: ./
36+
with:
37+
dry-run: true
38+
hey-api-token: ${{ secrets.HEY_API_TOKEN }}
39+
path-to-openapi: ./openapi.yaml

action.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ branding:
77
color: red
88

99
inputs:
10+
dry-run:
11+
description: Run action in dry run mode
12+
required: false
1013
hey-api-token:
1114
description: Hey API service token
1215
required: true

dist/index.js

Lines changed: 28 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openapi.yaml

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
openapi: '3.0.0'
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/pets:
11+
get:
12+
summary: List all pets
13+
operationId: listPets
14+
tags:
15+
- pets
16+
parameters:
17+
- name: limit
18+
in: query
19+
description: How many items to return at one time (max 100)
20+
required: false
21+
schema:
22+
type: integer
23+
maximum: 100
24+
format: int32
25+
responses:
26+
'200':
27+
description: A paged array of pets
28+
headers:
29+
x-next:
30+
description: A link to the next page of responses
31+
schema:
32+
type: string
33+
content:
34+
application/json:
35+
schema:
36+
$ref: '#/components/schemas/Pets'
37+
default:
38+
description: unexpected error
39+
content:
40+
application/json:
41+
schema:
42+
$ref: '#/components/schemas/Error'
43+
post:
44+
summary: Create a pet
45+
operationId: createPets
46+
tags:
47+
- pets
48+
requestBody:
49+
content:
50+
application/json:
51+
schema:
52+
$ref: '#/components/schemas/Pet'
53+
required: true
54+
responses:
55+
'201':
56+
description: Null response
57+
default:
58+
description: unexpected error
59+
content:
60+
application/json:
61+
schema:
62+
$ref: '#/components/schemas/Error'
63+
/pets/{petId}:
64+
get:
65+
summary: Info for a specific pet
66+
operationId: showPetById
67+
tags:
68+
- pets
69+
parameters:
70+
- name: petId
71+
in: path
72+
required: true
73+
description: The id of the pet to retrieve
74+
schema:
75+
type: string
76+
responses:
77+
'200':
78+
description: Expected response to a valid request
79+
content:
80+
application/json:
81+
schema:
82+
$ref: '#/components/schemas/Pet'
83+
default:
84+
description: unexpected error
85+
content:
86+
application/json:
87+
schema:
88+
$ref: '#/components/schemas/Error'
89+
components:
90+
schemas:
91+
Pet:
92+
type: object
93+
required:
94+
- id
95+
- name
96+
properties:
97+
id:
98+
type: integer
99+
format: int64
100+
name:
101+
type: string
102+
tag:
103+
type: string
104+
Pets:
105+
type: array
106+
maxItems: 100
107+
items:
108+
$ref: '#/components/schemas/Pet'
109+
Error:
110+
type: object
111+
required:
112+
- code
113+
- message
114+
properties:
115+
code:
116+
type: integer
117+
format: int32
118+
message:
119+
type: string

src/main.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import { upload } from './upload'
77
*/
88
export async function run(): Promise<void> {
99
try {
10+
const dryRun: boolean =
11+
core.getInput('dry-run', {
12+
required: false
13+
}) === 'true'
1014
const heyApiToken: string = core.getInput('hey-api-token', {
1115
required: true
1216
})
@@ -17,9 +21,11 @@ export async function run(): Promise<void> {
1721
core.debug(`Path to OpenAPI: ${pathToOpenApi}`)
1822

1923
core.debug(`Upload started: ${new Date().toTimeString()}`)
20-
await upload(pathToOpenApi, heyApiToken)
24+
await upload(pathToOpenApi, heyApiToken, dryRun)
2125
core.debug(`Upload completed: ${new Date().toTimeString()}`)
2226
} catch (error) {
23-
if (error instanceof Error) core.setFailed(error.message)
27+
if (error instanceof Error) {
28+
core.setFailed(error.message)
29+
}
2430
}
2531
}

src/upload.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,36 @@ import { readFileSync } from 'node:fs'
88
*/
99
export async function upload(
1010
pathToOpenApi: string,
11-
heyApiToken: string
11+
heyApiToken: string,
12+
dryRun?: boolean
1213
): Promise<void> {
1314
return new Promise(async resolve => {
14-
// TODO: throw if path is invalid
1515
if (!pathToOpenApi) {
16-
throw new Error('OpenAPI path is invalid')
16+
throw new Error('invalid OpenAPI path')
1717
}
1818

19-
const data = readFileSync(pathToOpenApi)
19+
let data: Buffer
20+
try {
21+
data = readFileSync(pathToOpenApi)
22+
} catch (error) {
23+
throw new Error('invalid OpenAPI path')
24+
}
25+
26+
let formData = [
27+
[encodeURIComponent('openapi'), encodeURIComponent(data.toString())]
28+
]
29+
30+
if (dryRun) {
31+
formData = [
32+
...formData,
33+
[encodeURIComponent('dry-run'), encodeURIComponent(dryRun)]
34+
]
35+
}
2036

21-
const body = [
22-
encodeURIComponent('openapi'),
23-
encodeURIComponent(data.toString())
24-
].join('=')
37+
const body = formData.flatMap(arr => arr.join('=')).join('&')
2538

2639
const response = await fetch(
27-
// 'https://platform-production-25fb.up.railway.app/api/openapi',
28-
'https://platform-platform-pr-10.up.railway.app/api/openapi',
40+
'https://platform-production-25fb.up.railway.app/api/openapi',
2941
{
3042
body,
3143
headers: {
@@ -37,7 +49,8 @@ export async function upload(
3749
)
3850

3951
if (response.status >= 300) {
40-
throw new Error(JSON.stringify(response.json()))
52+
const error = await response.json()
53+
throw new Error(JSON.stringify(error))
4154
}
4255

4356
resolve()

0 commit comments

Comments
 (0)