Skip to content

Commit

Permalink
typescript the things, delete environment default
Browse files Browse the repository at this point in the history
  • Loading branch information
strumwolf committed Mar 6, 2021
1 parent 8fcbf17 commit 3a9d843
Show file tree
Hide file tree
Showing 20 changed files with 2,936 additions and 6,160 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
17 changes: 10 additions & 7 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"extends": [
"eslint:recommended",
"xo-space/esnext",
"@twec/eslint-config",
"prettier"
],
"plugins": ["@typescript-eslint"],
"extends": ["plugin:github/typescript", "plugin:prettier/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"project": "./tsconfig.eslint.json"
},
"env": {
"node": true
"node": true,
"es6": true
},
"ignorePatterns": ["dist/*"],
"rules": {
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: lint
run: npm run lint
- name: run tests
run: npm test
- name: create GitHub deployment
uses: chrnorm/deployment-action@releases/v1
id: deployment
Expand Down
26 changes: 26 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Ava test",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/node_modules/bin/ava",
"args": [
"--no-timeouts",
"--colors",
"${file}",
"--require",
"ts-node/register"
],
"console": "integratedTerminal",
"sourceMaps": true,
"internalConsoleOptions": "neverOpen",
"runtimeExecutable": "node"
}
]
}
187 changes: 187 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import anyTest, { TestInterface } from 'ava';
import * as github from '@actions/github';
import { Octokit } from '@octokit/core';
import { main } from '../src/execute';

const test = anyTest as TestInterface<{
token: string;
ref: string;
octokit: Octokit;
repo: { owner: string; repo: string };
}>;

interface response {
id: number;
}

interface Context {
owner: string;
repo: string;
ref?: string;
}

async function createEnvironment(
octokit: Octokit,
environmentName: string,
{ owner, repo }: Context,
): Promise<void> {
await octokit.request(
'PUT /repos/{owner}/{repo}/environments/{environment_name}',
{
owner,
repo,
environment_name: environmentName,
},
);
console.log('env created');
}

async function createDeploymentWithStatus(
octokit: Octokit,
environment: string,
{ owner, repo, ref = 'main' }: Context,
): Promise<void> {
const createdDeployment = await octokit.request(
'POST /repos/{owner}/{repo}/deployments',
{
owner,
repo,
ref,
environment,
},
);
const data = createdDeployment.data;
await octokit.request(
'POST /repos/{owner}/{repo}/deployments/{deployment_id}/statuses',
{
owner: owner,
repo: repo,
state: 'success',
deployment_id: data['id'],
},
);
}

async function getDeployments(
octokit: Octokit,
environment: string,
{ owner, repo }: Context,
): Promise<number[]> {
const { data } = await octokit.request(
'GET /repos/{owner}/{repo}/deployments',
{
owner,
repo,
environment,
},
);
const deploymentIds: number[] = data.map((deployment) => deployment.id);
return deploymentIds;
}

test.beforeEach(async (t) => {
process.env.GITHUB_REPOSITORY = 'strumwolf/delete-deployment-environment';
process.env.GITHUB_REF = 'main';
github.context.ref = process.env.GITHUB_REF;
const { GITHUB_TOKEN = '' } = process.env;
const { repo, ref } = github.context;
const octokit = new Octokit({
auth: GITHUB_TOKEN,
});
t.context = {
token: GITHUB_TOKEN,
ref,
octokit,
repo,
};
process.env.INPUT_TOKEN = process.env.GITHUB_TOKEN;
});

test.serial('should successfully remove environment', async (t) => {
const { octokit, repo, ref } = t.context;
const context: Context = repo;
const environment = 'test-full-env-removal';
await createEnvironment(octokit, environment, context);
await createDeploymentWithStatus(octokit, environment, { ...context, ref });
process.env.INPUT_ENVIRONMENT = environment;
await main();
let environmentExists = true;
try {
await octokit.request(
'GET /repos/{owner}/{repo}/environments/{environment_name}',
{
owner: repo.owner,
repo: repo.repo,
environment_name: environment,
},
);
} catch (err) {
// status 404 indicates that the environment cannot be found in the repo
environmentExists = err.status === 404 ? false : true;
}
t.falsy(environmentExists);
});

test.serial(
'should successfully remove deployments when environment has not been created',
async (t) => {
const { octokit, repo, ref } = t.context;
const context: Context = repo;
const environment = 'test-remove-without-creating-environment';
await createDeploymentWithStatus(octokit, environment, { ...context, ref });
process.env.INPUT_ENVIRONMENT = environment;
await main();
let environmentExists = true;
try {
await octokit.request(
'GET /repos/{owner}/{repo}/environments/{environment_name}',
{
owner: repo.owner,
repo: repo.repo,
environment_name: environment,
},
);
} catch (err) {
// status 404 indicates that the environment cannot be found in the repo
environmentExists = err.status === 404 ? false : true;
}
t.falsy(environmentExists);
const deploymentsId = await getDeployments(octokit, environment, context);
t.is(deploymentsId.length, 0);
},
);

test.serial(
'should successfully remove deployments and not remove environment',
async (t) => {
const { octokit, repo, ref } = t.context;
const context: Context = repo;
const environment = 'test-remove-deployments-only';
await createEnvironment(octokit, environment, context);
await createDeploymentWithStatus(octokit, environment, { ...context, ref });
process.env.INPUT_ENVIRONMENT = environment;
process.env.INPUT_ONLYREMOVEDEPLOYMENTS = 'true';
await main();
let environmentExists = false;
try {
const res = await octokit.request(
'GET /repos/{owner}/{repo}/environments/{environment_name}',
{
owner: repo.owner,
repo: repo.repo,
environment_name: environment,
},
);
environmentExists = res.status === 200 ? true : false;
} catch (err) {
t.log(err);
t.fail();
}
t.truthy(environmentExists);
const deploymentsId = await getDeployments(octokit, environment, context);
t.is(deploymentsId.length, 0);
// delete all artifacts
delete process.env.INPUT_ONLYREMOVEDEPLOYMENTS;
await main();
},
);
8 changes: 7 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ description: 'Delete the entire environment and deployments for the branch the a
author: "strumwolf"
branding:
icon: briefcase
color: black
color: gray-dark
inputs:
token:
description: GitHub access token
required: true
environment:
description: Environment to be deleted
required: true
onlyRemoveDeployments:
description: Only remove deployments, keep the environment. Defaults to false
required: false
onlyDeactivateDeployments:
description: Only inactive deployments, keep the environment and deployments. Defaults to false
required: false
runs:
using: 'node12'
main: 'dist/index.js'
1 change: 1 addition & 0 deletions dist/execute.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function main(): Promise<void>;
Loading

0 comments on commit 3a9d843

Please sign in to comment.