Skip to content

Commit

Permalink
Make delete-env work as documented (#101)
Browse files Browse the repository at this point in the history
I realized after looking at the code that delete-env was deleting an environment, which isn't really related to deployments like the rest of this action (at least not in a way that mirrors deactivate-env like the README says).

I've added code here to do the delete in the way I expected it to work--deactivate all deployments for the given environment, then delete them.
  • Loading branch information
awesomeunleashed authored May 10, 2022
1 parent b0db961 commit a9488c4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/lib/deactivate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async function deactivateEnvironment(
}

log.info(`${environment}: ${existing} deployments updated`);
return deployments;
}

export default deactivateEnvironment;
40 changes: 40 additions & 0 deletions src/lib/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { GitHub } from "@actions/github/lib/utils";

import { DeploymentContext } from "./context";
import deactivateEnvironment from "./deactivate";

/**
* Delete all deployments within this environment.
*/
async function deleteEnvironment(
github: InstanceType<typeof GitHub>,
context: DeploymentContext
) {
const {
log,
owner,
repo,
coreArgs: { environment },
} = context;
const deployments = await deactivateEnvironment(github, context);
if (!deployments) {
return;
}
const existing = deployments.data.length;
for (let i = 0; i < existing; i++) {
const deployment = deployments.data[i];
log.info(
`${environment}.${deployment.id}: deleting deployment (${deployment.sha})"`
);
await github.rest.repos.deleteDeployment({
owner,
repo,
deployment_id: deployment.id,
});
log.debug(`${environment}.${deployment.id} deleted`);
}

log.info(`${environment}: ${existing} deployments deleted`);
}

export default deleteEnvironment;
7 changes: 2 additions & 5 deletions src/steps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GitHub } from "@actions/github/lib/utils";

import { DeploymentContext } from "../lib/context";
import deactivateEnvironment from "../lib/deactivate";
import deleteEnvironment from "../lib/delete";
import {
getBooleanInput,
getOptionalInput,
Expand Down Expand Up @@ -90,11 +91,7 @@ export async function run(
{
log.debug(`'${step}' arguments`, { coreArgs });

await github.rest.repos.deleteAnEnvironment({
owner: context.owner,
repo: context.repo,
environment_name: coreArgs.environment,
});
await deleteEnvironment(github, context);
}
break;

Expand Down

0 comments on commit a9488c4

Please sign in to comment.