Skip to content

Commit

Permalink
Add removeArtifacts (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosalie241 committed Sep 6, 2021
1 parent a4f828a commit f597c8a
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -22,6 +22,7 @@ This action will create a GitHub release and optionally upload an artifact to it
This will preserve the existing prerelease state during updates.
- **owner**: Optionally specify the owner of the repo where the release should be generated. Defaults to current repo's owner.
- **prerelease**: Optionally marks this release as prerelease. Set to true to enable.
- **removeArtifacts**: Indicates if existing release artifacts should be removed. Defaults to `false`.
- **replacesArtifacts**: Indicates if existing release artifacts should be replaced. Defaults to `true`.
- **repo**: Optionally specify the repo where the release should be generated. Defaults to current repo.
- **tag**: An optional tag for the release. If this is omitted the git ref will be used (if it is a tag).
Expand Down
2 changes: 2 additions & 0 deletions __tests__/Action.test.ts
Expand Up @@ -29,6 +29,7 @@ const createPrerelease = true
const updatePrerelease = false
const releaseId = 101
const replacesArtifacts = true
const removeArtifacts = false
const tag = 'tag'
const token = 'token'
const updateBody = 'updateBody'
Expand Down Expand Up @@ -298,6 +299,7 @@ describe("Action", () => {
owner: "owner",
createdPrerelease: createPrerelease,
replacesArtifacts: replacesArtifacts,
removeArtifacts: removeArtifacts,
repo: "repo",
tag: tag,
token: token,
Expand Down
25 changes: 22 additions & 3 deletions __tests__/ArtifactUploader.test.ts
Expand Up @@ -89,6 +89,25 @@ describe('ArtifactUploader', () => {
expect(deleteMock).toBeCalledWith(2)
})

it('removes all artifacts', async () => {
mockDeleteSuccess()
mockListWithAssets()
mockUploadArtifact()
const uploader = createUploader(false, true)

await uploader.uploadArtifacts(artifacts, releaseId, url)

expect(uploadMock).toBeCalledTimes(2)
expect(uploadMock)
.toBeCalledWith(url, contentLength, 'raw', fileContents, 'art1', releaseId)
expect(uploadMock)
.toBeCalledWith(url, contentLength, 'raw', fileContents, 'art2', releaseId)

expect(deleteMock).toBeCalledTimes(2)
expect(deleteMock).toBeCalledWith(1)
expect(deleteMock).toBeCalledWith(2)
})

it('replaces no artifacts when previous asset list empty', async () => {
mockDeleteSuccess()
mockListWithoutAssets()
Expand Down Expand Up @@ -129,7 +148,7 @@ describe('ArtifactUploader', () => {
it('throws upload error when replacesExistingArtifacts is true', async () => {
mockListWithoutAssets()
mockUploadError()
const uploader = createUploader(true, true)
const uploader = createUploader(true, false, true)

expect.hasAssertions()
try {
Expand Down Expand Up @@ -170,7 +189,7 @@ describe('ArtifactUploader', () => {
expect(deleteMock).toBeCalledTimes(0)
})

function createUploader(replaces: boolean, throws: boolean = false): GithubArtifactUploader {
function createUploader(replaces: boolean, removes: boolean = false, throws: boolean = false): GithubArtifactUploader {
const MockReleases = jest.fn<Releases, any>(() => {
return {
create: jest.fn(),
Expand All @@ -182,7 +201,7 @@ describe('ArtifactUploader', () => {
uploadArtifact: uploadMock
}
})
return new GithubArtifactUploader(new MockReleases(), replaces, throws)
return new GithubArtifactUploader(new MockReleases(), replaces, removes, throws)
}

function mockDeleteError(): any {
Expand Down
11 changes: 11 additions & 0 deletions __tests__/Inputs.test.ts
Expand Up @@ -236,6 +236,17 @@ describe('Inputs', () => {
})
})

describe('removeArtifacts', () => {
it('returns false', () => {
expect(inputs.removeArtifacts).toBe(false)
})

it('returns true', () => {
mockGetInput.mockReturnValue('true')
expect(inputs.removeArtifacts).toBe(true)
})
})

describe('repo', () => {
it('returns repo from context', function () {
process.env.GITHUB_REPOSITORY = "owner/repo"
Expand Down
2 changes: 2 additions & 0 deletions __tests__/Integration.test.ts
Expand Up @@ -23,6 +23,7 @@ describe.skip('Integration Test', () => {
const uploader = new GithubArtifactUploader(
releases,
inputs.replacesArtifacts,
inputs.removeArtifacts,
inputs.artifactErrorsFailBuild,
)
action = new Action(inputs, outputs, releases, uploader)
Expand All @@ -46,6 +47,7 @@ describe.skip('Integration Test', () => {
owner: "ncipollo",
createdPrerelease: false,
replacesArtifacts: true,
removeArtifacts: false,
repo: "actions-playground",
tag: "release-action-test",
token: getToken(),
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Expand Up @@ -75,6 +75,10 @@ inputs:
description: "Optionally marks this release as prerelease. Set to true to enable."
required: false
default: ''
removeArtifacts:
description: 'Indicates if existing release artifacts should be removed, Defaults to false.'
required: false
default: 'false'
replacesArtifacts:
description: "Indicates if existing release artifacts should be replaced. Defaults to true."
required: false
Expand Down
16 changes: 15 additions & 1 deletion lib/ArtifactUploader.js
Expand Up @@ -31,16 +31,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.GithubArtifactUploader = void 0;
const core = __importStar(require("@actions/core"));
class GithubArtifactUploader {
constructor(releases, replacesExistingArtifacts = true, throwsUploadErrors = false) {
constructor(releases, replacesExistingArtifacts = true, removeArtifacts = false, throwsUploadErrors = false) {
this.releases = releases;
this.replacesExistingArtifacts = replacesExistingArtifacts;
this.removeArtifacts = removeArtifacts;
this.throwsUploadErrors = throwsUploadErrors;
}
uploadArtifacts(artifacts, releaseId, uploadUrl) {
return __awaiter(this, void 0, void 0, function* () {
if (this.replacesExistingArtifacts) {
yield this.deleteUpdatedArtifacts(artifacts, releaseId);
}
if (this.removeArtifacts) {
yield this.deleteUploadedArtifacts(releaseId);
}
for (const artifact of artifacts) {
yield this.uploadArtifact(artifact, releaseId, uploadUrl);
}
Expand Down Expand Up @@ -84,5 +88,15 @@ class GithubArtifactUploader {
}
});
}
deleteUploadedArtifacts(releaseId) {
return __awaiter(this, void 0, void 0, function* () {
const releaseAssets = yield this.releases.listArtifactsForRelease(releaseId);
for (const artifact of releaseAssets) {
const asset = artifact;
core.debug(`Deleting existing artifact ${artifact.name}...`);
yield this.releases.deleteArtifact(asset.id);
}
});
}
}
exports.GithubArtifactUploader = GithubArtifactUploader;
4 changes: 4 additions & 0 deletions lib/Inputs.js
Expand Up @@ -117,6 +117,10 @@ class CoreInputs {
return undefined;
return this.createdPrerelease;
}
get removeArtifacts() {
const removes = core.getInput('removeArtifacts');
return removes == 'true';
}
get replacesArtifacts() {
const replaces = core.getInput('replacesArtifacts');
return replaces == 'true';
Expand Down
2 changes: 1 addition & 1 deletion lib/Main.js
Expand Up @@ -57,7 +57,7 @@ function createAction() {
const inputs = new Inputs_1.CoreInputs(globber, context);
const outputs = new Outputs_1.CoreOutputs();
const releases = new Releases_1.GithubReleases(inputs, git);
const uploader = new ArtifactUploader_1.GithubArtifactUploader(releases, inputs.replacesArtifacts, inputs.artifactErrorsFailBuild);
const uploader = new ArtifactUploader_1.GithubArtifactUploader(releases, inputs.replacesArtifacts, inputs.removeArtifacts, inputs.artifactErrorsFailBuild);
return new Action_1.Action(inputs, outputs, releases, uploader);
}
run();
13 changes: 13 additions & 0 deletions src/ArtifactUploader.ts
Expand Up @@ -10,6 +10,7 @@ export class GithubArtifactUploader implements ArtifactUploader {
constructor(
private releases: Releases,
private replacesExistingArtifacts: boolean = true,
private removeArtifacts: boolean = false,
private throwsUploadErrors: boolean = false,
) {
}
Expand All @@ -20,6 +21,9 @@ export class GithubArtifactUploader implements ArtifactUploader {
if (this.replacesExistingArtifacts) {
await this.deleteUpdatedArtifacts(artifacts, releaseId)
}
if (this.removeArtifacts) {
await this.deleteUploadedArtifacts(releaseId)
}
for (const artifact of artifacts) {
await this.uploadArtifact(artifact, releaseId, uploadUrl)
}
Expand Down Expand Up @@ -65,4 +69,13 @@ export class GithubArtifactUploader implements ArtifactUploader {
}
}
}

private async deleteUploadedArtifacts(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)
}
}
}
5 changes: 5 additions & 0 deletions src/Inputs.ts
Expand Up @@ -15,6 +15,7 @@ export interface Inputs {
readonly draft: boolean
readonly owner: string
readonly createdPrerelease: boolean
readonly removeArtifacts: boolean
readonly replacesArtifacts: boolean
readonly repo: string
readonly tag: string
Expand Down Expand Up @@ -138,6 +139,10 @@ export class CoreInputs implements Inputs {
if (CoreInputs.omitPrereleaseDuringUpdate) return undefined
return this.createdPrerelease
}
get removeArtifacts(): boolean {
const removes = core.getInput('removeArtifacts')
return removes == 'true'
}
get replacesArtifacts(): boolean {
const replaces = core.getInput('replacesArtifacts')
return replaces == 'true'
Expand Down
2 changes: 1 addition & 1 deletion src/Main.ts
Expand Up @@ -27,7 +27,7 @@ function createAction(): Action {
const inputs = new CoreInputs(globber, context)
const outputs = new CoreOutputs()
const releases = new GithubReleases(inputs, git)
const uploader = new GithubArtifactUploader(releases, inputs.replacesArtifacts, inputs.artifactErrorsFailBuild)
const uploader = new GithubArtifactUploader(releases, inputs.replacesArtifacts, inputs.removeArtifacts, inputs.artifactErrorsFailBuild)
return new Action(inputs, outputs, releases, uploader)
}

Expand Down

0 comments on commit f597c8a

Please sign in to comment.