generated from actions/typescript-action
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #120 Artifacts can be removed even when new ones aren't provided
- Loading branch information
Showing
8 changed files
with
164 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import {Artifact} from "../src/Artifact" | ||
import {GithubArtifactUploader} from "../src/ArtifactUploader" | ||
import {Releases} from "../src/Releases"; | ||
import {RequestError} from '@octokit/request-error' | ||
import {GithubArtifactDestroyer} from "../src/ArtifactDestroyer"; | ||
|
||
const releaseId = 100 | ||
|
||
const deleteMock = jest.fn() | ||
const listArtifactsMock = jest.fn() | ||
|
||
|
||
describe('ArtifactDestroyer', () => { | ||
beforeEach(() => { | ||
deleteMock.mockClear() | ||
listArtifactsMock.mockClear() | ||
}) | ||
|
||
it('destroys all artifacts', async () => { | ||
mockListWithAssets() | ||
mockDeleteSuccess() | ||
const destroyer = createDestroyer() | ||
|
||
await destroyer.destroyArtifacts(releaseId) | ||
|
||
expect(deleteMock).toBeCalledTimes(2) | ||
}) | ||
|
||
it('destroys nothing when no artifacts found', async () => { | ||
mockListWithoutAssets() | ||
const destroyer = createDestroyer() | ||
|
||
await destroyer.destroyArtifacts(releaseId) | ||
|
||
expect(deleteMock).toBeCalledTimes(0) | ||
}) | ||
|
||
it('throws when delete call fails', async () => { | ||
mockListWithAssets() | ||
mockDeleteError() | ||
const destroyer = createDestroyer() | ||
|
||
expect.hasAssertions() | ||
try { | ||
await destroyer.destroyArtifacts(releaseId) | ||
} catch (error) { | ||
expect(error).toEqual("error") | ||
} | ||
}) | ||
|
||
function createDestroyer(): GithubArtifactDestroyer { | ||
const MockReleases = jest.fn<Releases, any>(() => { | ||
return { | ||
create: jest.fn(), | ||
deleteArtifact: deleteMock, | ||
getByTag: jest.fn(), | ||
listArtifactsForRelease: listArtifactsMock, | ||
listReleases: jest.fn(), | ||
update: jest.fn(), | ||
uploadArtifact: jest.fn() | ||
} | ||
}) | ||
return new GithubArtifactDestroyer(new MockReleases()) | ||
} | ||
|
||
function mockDeleteError(): any { | ||
deleteMock.mockRejectedValue("error") | ||
} | ||
|
||
function mockDeleteSuccess(): any { | ||
deleteMock.mockResolvedValue({}) | ||
} | ||
|
||
function mockListWithAssets() { | ||
listArtifactsMock.mockResolvedValue([ | ||
{ | ||
name: "art1", | ||
id: 1 | ||
}, | ||
{ | ||
name: "art2", | ||
id: 2 | ||
} | ||
]) | ||
} | ||
|
||
function mockListWithoutAssets() { | ||
listArtifactsMock.mockResolvedValue([]) | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {Releases} from "./Releases"; | ||
import * as core from "@actions/core"; | ||
|
||
export interface ArtifactDestroyer { | ||
destroyArtifacts(releaseId: number): Promise<void> | ||
} | ||
|
||
export class GithubArtifactDestroyer implements ArtifactDestroyer { | ||
constructor(private releases: Releases) { | ||
} | ||
|
||
async destroyArtifacts(releaseId: number): Promise<void> { | ||
const releaseAssets = await this.releases.listArtifactsForRelease(releaseId) | ||
for (const artifact of releaseAssets) { | ||
const asset = artifact | ||
core.debug(`Deleting existing artifact ${artifact.name}...`) | ||
await this.releases.deleteArtifact(asset.id) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters