Skip to content

Commit b54ad48

Browse files
authored
Merge pull request #19 from hey-api/feat/default-branch
feat: send default branch
2 parents 902b613 + cd80934 commit b54ad48

File tree

7 files changed

+77
-32
lines changed

7 files changed

+77
-32
lines changed

.github/workflows/ci.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@ jobs:
2121
id: upload-json-openapi-spec
2222
uses: ./
2323
with:
24-
api-key: ${{ secrets.HEY_API_TOKEN }}
2524
dry-run: true
2625
path-to-file: ./openapi.json
26+
env:
27+
API_KEY: ${{ secrets.HEY_API_TOKEN }}
28+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2729

2830
- name: Upload YAML OpenAPI spec
2931
id: upload-yaml-openapi-spec
3032
uses: ./
3133
with:
32-
api-key: ${{ secrets.HEY_API_TOKEN }}
3334
dry-run: true
3435
path-to-file: ./openapi.yaml
36+
env:
37+
API_KEY: ${{ secrets.HEY_API_TOKEN }}
38+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ jobs:
3131
- name: Upload OpenAPI spec
3232
uses: hey-api/[email protected]
3333
with:
34-
api-key: ${{ secrets.HEY_API_TOKEN }}
3534
path-to-file: path/to/openapi.json
3635
tags: optional,custom,tags
36+
env:
37+
API_KEY: ${{ secrets.HEY_API_TOKEN }}
38+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3739
```
3840
3941
The example above will upload your OpenAPI specification to Hey API on every
@@ -44,13 +46,6 @@ pull request and push to the `main` branch.
4446
To successfully upload an OpenAPI specification, you need to provide the
4547
following inputs (see `with` in the example above)
4648

47-
### `api-key`
48-
49-
This is the project API key you obtained from
50-
[Hey API](https://app.heyapi.dev/).
51-
52-
> Note: Personal API keys can't be used to upload specifications.
53-
5449
### `path-to-file`
5550

5651
A relative path to your OpenAPI file within the repository. Note that you might
@@ -62,6 +57,23 @@ need an additional step in your GitHub workflow to generate this file (see
6257
A comma-separated string value representing any custom tags you wish to add to
6358
your OpenAPI specification.
6459

60+
## Environment Variables
61+
62+
In addition to the required `path-to-file` input, you must provide the following
63+
environment variables.
64+
65+
### `API_KEY`
66+
67+
This is the project API key you obtained from
68+
[Hey API](https://app.heyapi.dev/).
69+
70+
> Note: Personal API keys can't be used to upload specifications.
71+
72+
### `GITHUB_TOKEN`
73+
74+
This variable will be available inside your workflow by default. It's used to
75+
fetch information about your repository, i.e. default branch.
76+
6577
## Next Steps
6678

6779
Please follow the

action.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ branding:
44
icon: file-plus
55
description: Upload your OpenAPI specification to Hey API
66
inputs:
7-
api-key:
8-
description: Hey API service token
9-
required: true
107
base-url:
118
description: Configure Hey API server URL
129
required: false

dist/index.js

Lines changed: 23 additions & 7 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.

src/main.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import { upload } from './upload'
66
* @returns {Promise<void>} Resolves when the action is complete.
77
*/
88
export async function run(): Promise<void> {
9+
if (!process.env.API_KEY) {
10+
core.setFailed('The API_KEY environment variable is required.')
11+
}
12+
13+
if (!process.env.GITHUB_TOKEN) {
14+
core.setFailed('The GITHUB_TOKEN environment variable is required.')
15+
}
16+
917
try {
10-
const apiKey: string = core.getInput('api-key', {
11-
required: true
12-
})
1318
const baseUrl: string = core.getInput('base-url', {
1419
required: false
1520
})
@@ -27,7 +32,6 @@ export async function run(): Promise<void> {
2732
core.debug(`Path to OpenAPI: ${pathToFile}`)
2833
core.debug(`Upload started: ${new Date().toTimeString()}`)
2934
await upload({
30-
apiKey,
3135
baseUrl,
3236
dryRun,
3337
pathToFile,

src/upload.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ import { postV1Specifications } from './client'
66
* Read and upload the provided OpenAPI specification to Hey API.
77
*/
88
export async function upload({
9-
apiKey,
109
baseUrl,
1110
dryRun,
1211
pathToFile,
1312
tags
1413
}: {
15-
/**
16-
* Hey API token.
17-
*/
18-
apiKey: string
1914
/**
2015
* Custom service url.
2116
*/
@@ -58,8 +53,23 @@ export async function upload({
5853
: 'application/json'
5954
})
6055

56+
const repository = process.env.GITHUB_REPOSITORY
57+
58+
const response = await fetch(`https://api.github.com/repos/${repository}`, {
59+
method: 'GET',
60+
headers: {
61+
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`
62+
}
63+
})
64+
65+
let defaultBranch
66+
if (response.ok) {
67+
const repo = await response.json()
68+
defaultBranch = repo.default_branch
69+
}
70+
6171
const { error } = await postV1Specifications({
62-
auth: apiKey,
72+
auth: process.env.API_KEY,
6373
baseUrl: baseUrl || 'https://api.heyapi.dev',
6474
body: {
6575
actor: process.env.GITHUB_ACTOR,
@@ -68,12 +78,14 @@ export async function upload({
6878
branch_base: process.env.GITHUB_BASE_REF,
6979
ci_platform: 'github',
7080
commit_sha: commitSha,
81+
// @ts-expect-error
82+
default_branch: defaultBranch,
7183
dry_run: dryRun,
7284
event_name: process.env.GITHUB_EVENT_NAME,
7385
job: process.env.GITHUB_JOB,
7486
ref: process.env.GITHUB_REF,
7587
ref_type: process.env.GITHUB_REF_TYPE,
76-
repository: process.env.GITHUB_REPOSITORY,
88+
repository,
7789
run_id: process.env.GITHUB_RUN_ID,
7890
run_number: process.env.GITHUB_RUN_NUMBER,
7991
specification,

0 commit comments

Comments
 (0)