Skip to content
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

set X-GitHub-Api-Version 2022-11-28. #735

Merged
merged 2 commits into from
Jul 5, 2023
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
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

![test](https://github.com/shogo82148/actions-upload-release-asset/workflows/test/badge.svg)

This GitHub Action uploads release assets using [Upload a release asset](https://docs.github.com/en/rest/reference/repos#upload-a-release-asset).
This GitHub Action uploads release assets using [Upload a release asset](https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#upload-a-release-asset).
[actions/upload-release-asset] is great, but it is no longer maintained.
So, I create this action.

## FEATURE

The action is compatible with [actions/upload-release-asset],
however it has some additional features.

- You can upload multiple assets in one step by using [glob patterns](https://github.com/actions/toolkit/tree/master/packages/glob#patterns)
- The actions guesses the content-type from the extensions

Expand Down Expand Up @@ -38,13 +43,13 @@ jobs:

### Upload assets when a tag has been created

If you want to create a release in your workflow, you can use [shogo82148/actions-create-release](https://github.com/shogo82148/actions-create-release) GitHub Action.
If you want to create a release in your workflow, you can use [shogo82148/actions-create-release] GitHub Action.

```yaml
on:
push:
tags:
- 'v*'
- "v*"

jobs:
build:
Expand All @@ -55,14 +60,21 @@ jobs:
# steps for building assets
- run: echo "REPLACE ME!" > assets.txt

- name: Create Release
- name: Create a Release
id: create_release
uses: shogo82148/actions-create-release@v1

- uses: shogo82148/actions-upload-release-asset@v1
# A release created by shogo82148/actions-create-release is alway draft here.
# So users may not see an empty release.

- name: Upload Assets
uses: shogo82148/actions-upload-release-asset@v1
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: assets.txt

# shogo82148/actions-create-release publishes the release in the end of the job.
# Users now can download `assets.txt` from the release page.
```

## Inputs
Expand Down Expand Up @@ -99,4 +111,8 @@ If an asset with the same name already exists, overwrite it (Default: false).

## Related works

- [actions/upload-release-asset](https://github.com/actions/upload-release-asset) GitHub Action
- [actions/upload-release-asset] GitHub Action
- [shogo82148/actions-create-release] GitHub Action

[actions/upload-release-asset]: https://github.com/actions/upload-release-asset
[shogo82148/actions-create-release]: https://github.com/shogo82148/actions-create-release
23 changes: 12 additions & 11 deletions src/upload-release-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ interface Outputs {
const newGitHubClient = (token: string): http.HttpClient => {
return new http.HttpClient("shogo82148-actions-upload-release-asset/v1", [], {
headers: {
Authorization: `token ${token}`,
Accept: "application/vnd.github.v3+json",
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
},
});
};

// minium implementation of upload a release asset API
// https://docs.github.com/en/rest/reference/repos#upload-a-release-asset
// minium implementation of upload a release asset API.
// https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#upload-a-release-asset
const uploadReleaseAsset = async (
params: ReposUploadReleaseAssetParams
): Promise<Response<ReposUploadReleaseAssetResponse>> => {
Expand Down Expand Up @@ -84,8 +85,8 @@ interface ReposDeleteReleaseAssetParams {
githubToken: string;
}

// minium implementation of delete a release asset API
// https://docs.github.com/en/rest/reference/repos#delete-a-release-asset
// minium implementation of delete a release asset API.
// https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#delete-a-release-asset
const deleteReleaseAsset = async (params: ReposDeleteReleaseAssetParams): Promise<void> => {
const client = newGitHubClient(params.githubToken);
const resp = await client.request("DELETE", params.url, "", {});
Expand Down Expand Up @@ -116,8 +117,8 @@ interface ReposGetReleaseAsset {
name: string;
}

// minium implementation of get a release API
// https://docs.github.com/en/rest/reference/repos#get-a-release
// minium implementation of get a release API.
// https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#get-a-release
const getRelease = async (
params: ReposGetReleaseParams
): Promise<Response<ReposGetReleaseResponse>> => {
Expand Down Expand Up @@ -268,12 +269,12 @@ async function validateFilenames(files: string[], opts: Options): Promise<void>
);
}

// https://docs.github.com/en/rest/reference/repos#upload-a-release-asset
// we rename the filenames here to avoid being renamed by API.
//
// https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#upload-a-release-asset
// > GitHub renames asset filenames that have special characters,
// > non-alphanumeric characters, and leading or trailing periods.
// > The "List assets for a release" endpoint lists the renamed filenames.
//
// we rename the filenames here to avoid being renamed by API
export function canonicalName(name: string): string {
name = name.replace(/[,/]/g, ".");
name = name.replace(/[^-+@_.a-zA-Z0-9]/g, "");
Expand Down