Skip to content

feat: send default branch #19

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

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ jobs:
id: upload-json-openapi-spec
uses: ./
with:
api-key: ${{ secrets.HEY_API_TOKEN }}
dry-run: true
path-to-file: ./openapi.json
env:
API_KEY: ${{ secrets.HEY_API_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Upload YAML OpenAPI spec
id: upload-yaml-openapi-spec
uses: ./
with:
api-key: ${{ secrets.HEY_API_TOKEN }}
dry-run: true
path-to-file: ./openapi.yaml
env:
API_KEY: ${{ secrets.HEY_API_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ jobs:
- name: Upload OpenAPI spec
uses: hey-api/[email protected]
with:
api-key: ${{ secrets.HEY_API_TOKEN }}
path-to-file: path/to/openapi.json
tags: optional,custom,tags
env:
API_KEY: ${{ secrets.HEY_API_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

The example above will upload your OpenAPI specification to Hey API on every
Expand All @@ -44,13 +46,6 @@ pull request and push to the `main` branch.
To successfully upload an OpenAPI specification, you need to provide the
following inputs (see `with` in the example above)

### `api-key`

This is the project API key you obtained from
[Hey API](https://app.heyapi.dev/).

> Note: Personal API keys can't be used to upload specifications.

### `path-to-file`

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

## Environment Variables

In addition to the required `path-to-file` input, you must provide the following
environment variables.

### `API_KEY`

This is the project API key you obtained from
[Hey API](https://app.heyapi.dev/).

> Note: Personal API keys can't be used to upload specifications.

### `GITHUB_TOKEN`

This variable will be available inside your workflow by default. It's used to
fetch information about your repository, i.e. default branch.

## Next Steps

Please follow the
Expand Down
3 changes: 0 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ branding:
icon: file-plus
description: Upload your OpenAPI specification to Hey API
inputs:
api-key:
description: Hey API service token
required: true
base-url:
description: Configure Hey API server URL
required: false
Expand Down
30 changes: 23 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import { upload } from './upload'
* @returns {Promise<void>} Resolves when the action is complete.
*/
export async function run(): Promise<void> {
if (!process.env.API_KEY) {
core.setFailed('The API_KEY environment variable is required.')
}

if (!process.env.GITHUB_TOKEN) {
core.setFailed('The GITHUB_TOKEN environment variable is required.')
}

try {
const apiKey: string = core.getInput('api-key', {
required: true
})
const baseUrl: string = core.getInput('base-url', {
required: false
})
Expand All @@ -27,7 +32,6 @@ export async function run(): Promise<void> {
core.debug(`Path to OpenAPI: ${pathToFile}`)
core.debug(`Upload started: ${new Date().toTimeString()}`)
await upload({
apiKey,
baseUrl,
dryRun,
pathToFile,
Expand Down
26 changes: 19 additions & 7 deletions src/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ import { postV1Specifications } from './client'
* Read and upload the provided OpenAPI specification to Hey API.
*/
export async function upload({
apiKey,
baseUrl,
dryRun,
pathToFile,
tags
}: {
/**
* Hey API token.
*/
apiKey: string
/**
* Custom service url.
*/
Expand Down Expand Up @@ -58,8 +53,23 @@ export async function upload({
: 'application/json'
})

const repository = process.env.GITHUB_REPOSITORY

const response = await fetch(`https://api.github.com/repos/${repository}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`
}
})

let defaultBranch
if (response.ok) {
const repo = await response.json()
defaultBranch = repo.default_branch
}

const { error } = await postV1Specifications({
auth: apiKey,
auth: process.env.API_KEY,
baseUrl: baseUrl || 'https://api.heyapi.dev',
body: {
actor: process.env.GITHUB_ACTOR,
Expand All @@ -68,12 +78,14 @@ export async function upload({
branch_base: process.env.GITHUB_BASE_REF,
ci_platform: 'github',
commit_sha: commitSha,
// @ts-expect-error
default_branch: defaultBranch,
dry_run: dryRun,
event_name: process.env.GITHUB_EVENT_NAME,
job: process.env.GITHUB_JOB,
ref: process.env.GITHUB_REF,
ref_type: process.env.GITHUB_REF_TYPE,
repository: process.env.GITHUB_REPOSITORY,
repository,
run_id: process.env.GITHUB_RUN_ID,
run_number: process.env.GITHUB_RUN_NUMBER,
specification,
Expand Down