Fixes #19 Bump dependencies & update octokit usage
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Inputs } from "./Inputs";
|
||||
import { Releases } from "./Releases";
|
||||
import { ReposCreateReleaseResponse } from "@octokit/rest";
|
||||
import { ReposCreateReleaseResponseData } from "@octokit/types";
|
||||
import { ArtifactUploader } from "./ArtifactUploader";
|
||||
import { ErrorMessage } from "./ErrorMessage";
|
||||
|
||||
@@ -26,13 +26,13 @@ export class Action {
|
||||
}
|
||||
}
|
||||
|
||||
private async createOrUpdateRelease(): Promise<ReposCreateReleaseResponse> {
|
||||
private async createOrUpdateRelease(): Promise<ReposCreateReleaseResponseData> {
|
||||
if (this.inputs.allowUpdates) {
|
||||
try {
|
||||
const getResponse = await this.releases.getByTag(this.inputs.tag)
|
||||
return await this.updateRelease(getResponse.data.id)
|
||||
} catch (error) {
|
||||
if (this.noPublishedRelease(error)) {
|
||||
if (Action.noPublishedRelease(error)) {
|
||||
return await this.updateDraftOrCreateRelease()
|
||||
} else {
|
||||
throw error
|
||||
@@ -43,7 +43,7 @@ export class Action {
|
||||
}
|
||||
}
|
||||
|
||||
private async updateRelease(id: number): Promise<ReposCreateReleaseResponse> {
|
||||
private async updateRelease(id: number): Promise<ReposCreateReleaseResponseData> {
|
||||
const response = await this.releases.update(
|
||||
id,
|
||||
this.inputs.tag,
|
||||
@@ -57,12 +57,12 @@ export class Action {
|
||||
return response.data
|
||||
}
|
||||
|
||||
private noPublishedRelease(error: any): boolean {
|
||||
private static noPublishedRelease(error: any): boolean {
|
||||
const errorMessage = new ErrorMessage(error)
|
||||
return errorMessage.status == 404
|
||||
}
|
||||
|
||||
private async updateDraftOrCreateRelease(): Promise<ReposCreateReleaseResponse> {
|
||||
private async updateDraftOrCreateRelease(): Promise<ReposCreateReleaseResponseData> {
|
||||
const draftReleaseId = await this.findMatchingDraftReleaseId()
|
||||
if (draftReleaseId) {
|
||||
return await this.updateRelease(draftReleaseId)
|
||||
@@ -80,7 +80,7 @@ export class Action {
|
||||
return draftRelease?.id
|
||||
}
|
||||
|
||||
private async createRelease(): Promise<ReposCreateReleaseResponse> {
|
||||
private async createRelease(): Promise<ReposCreateReleaseResponseData> {
|
||||
const response = await this.releases.create(
|
||||
this.inputs.tag,
|
||||
this.inputs.createdReleaseBody,
|
||||
|
||||
@@ -19,4 +19,4 @@ export class Artifact {
|
||||
readFile(): Buffer {
|
||||
return readFileSync(this.path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import * as core from '@actions/core';
|
||||
import { Artifact } from "./Artifact";
|
||||
import { Releases } from "./Releases";
|
||||
import { ReposListAssetsForReleaseResponseItem } from "@octokit/rest";
|
||||
import {Artifact} from "./Artifact";
|
||||
import {Releases} from "./Releases";
|
||||
|
||||
export interface ArtifactUploader {
|
||||
uploadArtifacts(artifacts: Artifact[], releaseId: number, uploadUrl: string): Promise<void>
|
||||
@@ -15,28 +14,32 @@ export class GithubArtifactUploader implements ArtifactUploader {
|
||||
}
|
||||
|
||||
async uploadArtifacts(artifacts: Artifact[],
|
||||
releaseId: number,
|
||||
uploadUrl: string): Promise<void> {
|
||||
releaseId: number,
|
||||
uploadUrl: string): Promise<void> {
|
||||
if (this.replacesExistingArtifacts) {
|
||||
await this.deleteUpdatedArtifacts(artifacts, releaseId)
|
||||
}
|
||||
for (const artifact of artifacts) {
|
||||
await this.uploadArtifact(artifact, uploadUrl)
|
||||
await this.uploadArtifact(artifact, releaseId, uploadUrl)
|
||||
}
|
||||
}
|
||||
|
||||
private async uploadArtifact(artifact: Artifact, uploadUrl: string, retry = 3) {
|
||||
private async uploadArtifact(artifact: Artifact,
|
||||
releaseId: number,
|
||||
uploadUrl: string,
|
||||
retry = 3) {
|
||||
try {
|
||||
core.debug(`Uploading artifact ${artifact.name}...`)
|
||||
await this.releases.uploadArtifact(uploadUrl,
|
||||
artifact.contentLength,
|
||||
artifact.contentType,
|
||||
artifact.readFile(),
|
||||
artifact.name)
|
||||
artifact.name,
|
||||
releaseId)
|
||||
} catch (error) {
|
||||
if (error.status >= 500 && retry > 0) {
|
||||
core.warning(`Failed to upload artifact ${artifact.name}. ${error.message}. Retrying...`)
|
||||
await this.uploadArtifact(artifact, uploadUrl, retry - 1)
|
||||
await this.uploadArtifact(artifact, releaseId, uploadUrl, retry - 1)
|
||||
} else {
|
||||
core.warning(`Failed to upload artifact ${artifact.name}. ${error.message}.`)
|
||||
}
|
||||
@@ -44,9 +47,9 @@ export class GithubArtifactUploader implements ArtifactUploader {
|
||||
}
|
||||
|
||||
private async deleteUpdatedArtifacts(artifacts: Artifact[], releaseId: number): Promise<void> {
|
||||
const response = await this.releases.listArtifactsForRelease(releaseId)
|
||||
const response = await this.releases.listArtifactsForRelease(releaseId)
|
||||
const releaseAssets = response.data
|
||||
const assetByName: Record<string, ReposListAssetsForReleaseResponseItem> = {}
|
||||
const assetByName: Record<string, { id: number; name: string }> = {}
|
||||
releaseAssets.forEach(asset => {
|
||||
assetByName[asset.name] = asset
|
||||
});
|
||||
@@ -58,4 +61,4 @@ export class GithubArtifactUploader implements ArtifactUploader {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ async function run() {
|
||||
function createAction(): Action {
|
||||
const token = core.getInput('token')
|
||||
const context = github.context
|
||||
const git = new github.GitHub(token)
|
||||
const git = github.getOctokit(token)
|
||||
const globber = new FileArtifactGlobber()
|
||||
|
||||
const inputs = new CoreInputs(globber, context)
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
import { Context } from "@actions/github/lib/context";
|
||||
import { GitHub } from "@actions/github";
|
||||
import { AnyResponse, Response, ReposDeleteReleaseAssetResponse, ReposListAssetsForReleaseResponse, ReposCreateReleaseResponse, ReposGetReleaseByTagResponse, ReposListReleasesResponse } from "@octokit/rest";
|
||||
import {Context} from "@actions/github/lib/context";
|
||||
import { GitHub } from '@actions/github/lib/utils'
|
||||
import {
|
||||
OctokitResponse,
|
||||
ReposListReleaseAssetsResponseData,
|
||||
ReposCreateReleaseResponseData,
|
||||
ReposGetReleaseByTagResponseData,
|
||||
ReposListReleasesResponseData,
|
||||
ReposUploadReleaseAssetResponseData
|
||||
} from "@octokit/types";
|
||||
|
||||
export interface Releases {
|
||||
create(
|
||||
@@ -10,15 +17,15 @@ export interface Releases {
|
||||
draft?: boolean,
|
||||
name?: string,
|
||||
prerelease?: boolean
|
||||
): Promise<Response<ReposCreateReleaseResponse>>
|
||||
): Promise<OctokitResponse<ReposCreateReleaseResponseData>>
|
||||
|
||||
deleteArtifact(assetId: number): Promise<Response<ReposDeleteReleaseAssetResponse>>
|
||||
deleteArtifact(assetId: number): Promise<OctokitResponse<any>>
|
||||
|
||||
getByTag(tag: string): Promise<Response<ReposGetReleaseByTagResponse>>
|
||||
getByTag(tag: string): Promise<OctokitResponse<ReposGetReleaseByTagResponseData>>
|
||||
|
||||
listArtifactsForRelease(releaseId: number): Promise<Response<ReposListAssetsForReleaseResponse>>
|
||||
listArtifactsForRelease(releaseId: number): Promise<OctokitResponse<ReposListReleaseAssetsResponseData>>
|
||||
|
||||
listReleases(): Promise<Response<ReposListReleasesResponse>>
|
||||
listReleases(): Promise<OctokitResponse<ReposListReleasesResponseData>>
|
||||
|
||||
update(
|
||||
id: number,
|
||||
@@ -28,22 +35,23 @@ export interface Releases {
|
||||
draft?: boolean,
|
||||
name?: string,
|
||||
prerelease?: boolean
|
||||
): Promise<Response<ReposCreateReleaseResponse>>
|
||||
): Promise<OctokitResponse<ReposCreateReleaseResponseData>>
|
||||
|
||||
uploadArtifact(
|
||||
assetUrl: string,
|
||||
contentLength: number,
|
||||
contentType: string,
|
||||
file: string | object,
|
||||
name: string
|
||||
): Promise<Response<AnyResponse>>
|
||||
name: string,
|
||||
releaseId: number,
|
||||
): Promise<OctokitResponse<ReposUploadReleaseAssetResponseData>>
|
||||
}
|
||||
|
||||
export class GithubReleases implements Releases {
|
||||
context: Context
|
||||
git: GitHub
|
||||
git: InstanceType<typeof GitHub>
|
||||
|
||||
constructor(context: Context, git: GitHub) {
|
||||
constructor(context: Context, git: InstanceType<typeof GitHub>) {
|
||||
this.context = context
|
||||
this.git = git
|
||||
}
|
||||
@@ -55,7 +63,8 @@ export class GithubReleases implements Releases {
|
||||
draft?: boolean,
|
||||
name?: string,
|
||||
prerelease?: boolean
|
||||
): Promise<Response<ReposCreateReleaseResponse>> {
|
||||
): Promise<OctokitResponse<ReposCreateReleaseResponseData>> {
|
||||
// noinspection TypeScriptValidateJSTypes
|
||||
return this.git.repos.createRelease({
|
||||
body: body,
|
||||
name: name,
|
||||
@@ -70,7 +79,7 @@ export class GithubReleases implements Releases {
|
||||
|
||||
async deleteArtifact(
|
||||
assetId: number
|
||||
): Promise<Response<ReposDeleteReleaseAssetResponse>> {
|
||||
): Promise<OctokitResponse<any>> {
|
||||
return this.git.repos.deleteReleaseAsset({
|
||||
asset_id: assetId,
|
||||
owner: this.context.repo.owner,
|
||||
@@ -80,22 +89,22 @@ export class GithubReleases implements Releases {
|
||||
|
||||
async listArtifactsForRelease(
|
||||
releaseId: number
|
||||
): Promise<Response<ReposListAssetsForReleaseResponse>> {
|
||||
return this.git.repos.listAssetsForRelease({
|
||||
): Promise<OctokitResponse<ReposListReleaseAssetsResponseData>> {
|
||||
return this.git.repos.listReleaseAssets({
|
||||
owner: this.context.repo.owner,
|
||||
release_id: releaseId,
|
||||
repo: this.context.repo.repo
|
||||
})
|
||||
}
|
||||
|
||||
async listReleases(): Promise<Response<ReposListReleasesResponse>> {
|
||||
async listReleases(): Promise<OctokitResponse<ReposListReleasesResponseData>> {
|
||||
return this.git.repos.listReleases({
|
||||
owner: this.context.repo.owner,
|
||||
repo: this.context.repo.repo
|
||||
})
|
||||
}
|
||||
|
||||
async getByTag(tag: string): Promise<Response<ReposGetReleaseByTagResponse>> {
|
||||
async getByTag(tag: string): Promise<OctokitResponse<ReposGetReleaseByTagResponseData>> {
|
||||
return this.git.repos.getReleaseByTag({
|
||||
owner: this.context.repo.owner,
|
||||
repo: this.context.repo.repo,
|
||||
@@ -111,7 +120,8 @@ export class GithubReleases implements Releases {
|
||||
draft?: boolean,
|
||||
name?: string,
|
||||
prerelease?: boolean
|
||||
): Promise<Response<ReposCreateReleaseResponse>> {
|
||||
): Promise<OctokitResponse<ReposCreateReleaseResponseData>> {
|
||||
// noinspection TypeScriptValidateJSTypes
|
||||
return this.git.repos.updateRelease({
|
||||
release_id: id,
|
||||
body: body,
|
||||
@@ -130,16 +140,20 @@ export class GithubReleases implements Releases {
|
||||
contentLength: number,
|
||||
contentType: string,
|
||||
file: string | object,
|
||||
name: string
|
||||
): Promise<Response<AnyResponse>> {
|
||||
name: string,
|
||||
releaseId: number,
|
||||
): Promise<OctokitResponse<ReposUploadReleaseAssetResponseData>> {
|
||||
return this.git.repos.uploadReleaseAsset({
|
||||
url: assetUrl,
|
||||
headers: {
|
||||
"content-length": contentLength,
|
||||
"content-type": contentType
|
||||
},
|
||||
file: file,
|
||||
name: name
|
||||
data: file as any,
|
||||
name: name,
|
||||
owner: this.context.repo.owner,
|
||||
release_id: releaseId,
|
||||
repo: this.context.repo.repo
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user