Skip to content

Commit

Permalink
Merge pull request #16 from humanitec/configurable-automation-rule
Browse files Browse the repository at this point in the history
feat: configurable automation rule creation
  • Loading branch information
johanneswuerbach authored Aug 16, 2023
2 parents 913f77e + 1fa0477 commit 12d8d4c
Show file tree
Hide file tree
Showing 8 changed files with 6,815 additions and 4,251 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This GitHub action allows you to dynamically create and deleted preview environm
## Outputs

* `environment-url` : Rendered URL of the preview environment.
* `humanitec-env`: Id of the created environment.

## Example usage

Expand Down
6 changes: 6 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ inputs:
environment-url-template:
description: 'Provide a custom mustache template for the environment url, "https://app.humanitec.io/orgs/{{orgId}}/apps/{{appId}}/envs/{{envId}}" by default.'
required: false
create-automation-rule:
description: 'Automatically create an automation rule for the environment, "true" by default.'
required: false
default: 'true'
outputs:
environment-url:
description: 'URL where the running application can be accessed'
humanitec-env:
description: 'The id of the created environment'
runs:
using: 'node16'
main: 'dist/index.js'
Expand Down
10,886 changes: 6,690 additions & 4,196 deletions dist/index.js

Large diffs are not rendered by default.

68 changes: 34 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"dependencies": {
"@actions/core": "^1.9.1",
"@actions/github": "^5.1.1",
"@humanitec/autogen": "^0.0.8",
"@humanitec/autogen": "^0.0.9",
"axios": "^1.2.1",
"mustache": "^4.2.0"
},
Expand Down
51 changes: 51 additions & 0 deletions src/action.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {describe, expect, test, beforeEach, afterAll} from '@jest/globals';
import {runAction} from './action';
import {randomBytes} from 'crypto';
import {createApiClient} from './humanitec';
import {branchNameToEnvId} from './utils';

// Emulate https://github.com/actions/toolkit/blob/819157bf8/packages/core/src/core.ts#L128
const setInput = (name: string, value: string): void => {
Expand All @@ -22,8 +24,11 @@ const appId = ensureEnv('HUMANITEC_APP');

describe('action', () => {
let branch: string;
let humClient: ReturnType<typeof createApiClient>;

beforeEach(async () => {
humClient = createApiClient('', token);

setInput('humanitec-token', token);
setInput('humanitec-org', orgId);
setInput('humanitec-app', appId);
Expand All @@ -38,10 +43,56 @@ describe('action', () => {

test('succeeds', async () => {
try {
setInput('create-automation-rule', 'true');
setInput('action', 'create');
await runAction();
expect(process.exitCode).toBeFalsy();

// TODO Get from output instead
const envId = branchNameToEnvId('dev', branch);

const listRules = await humClient.orgsOrgIdAppsAppIdEnvsEnvIdRulesGet({
orgId,
appId,
envId,
});
expect(listRules.status).toBe(200);
expect(listRules.data).toHaveLength(1);
expect(listRules.data[0].match_ref).toEqual(`refs/heads/${branch}`);


setInput('action', 'notify');
await runAction();
expect(process.exitCode).toBeFalsy();

setInput('action', 'delete');
await runAction();
expect(process.exitCode).toBeFalsy();
} catch (e) {
console.log(e);
throw new Error('failed');
}
});


test('succeeds without automation rule', async () => {
try {
setInput('create-automation-rule', 'false');
setInput('action', 'create');
await runAction();
expect(process.exitCode).toBeFalsy();

// TODO Get from output instead
const envId = branchNameToEnvId('dev', branch);

const listRules = await humClient.orgsOrgIdAppsAppIdEnvsEnvIdRulesGet({
orgId,
appId,
envId,
});
expect(listRules.status).toBe(200);
expect(listRules.data).toEqual([]);

setInput('action', 'notify');
await runAction();
expect(process.exitCode).toBeFalsy();
Expand Down
39 changes: 23 additions & 16 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {getOctokit, context} from '@actions/github';
import {getInput, setOutput, info} from '@actions/core';
import {getInput, setOutput, info, getBooleanInput} from '@actions/core';
import {render} from 'mustache';

import {branchNameToEnvId} from './utils';
Expand All @@ -11,6 +11,8 @@ async function createEnvironment(input: ActionInput): Promise<void> {
const {orgId, appId, envId, context, octokit, humClient, branchName, environmentUrl, webAppUrl} = input;

const baseEnvId = getInput('base-env') || 'development';
const createAutomationRule = getBooleanInput('create-automation-rule');
console.log('createAutomationRule', createAutomationRule);
const imageName = (process.env.GITHUB_REPOSITORY || '').replace(/.*\//, '');
const image = getInput('image') || `registry.humanitec.io/${orgId}/${imageName}`;

Expand Down Expand Up @@ -48,23 +50,27 @@ async function createEnvironment(input: ActionInput): Promise<void> {

console.log(`Created environment: ${envId}, ${environmentUrl}`);

const matchRef =`refs/heads/${branchName}`;
const createRuleRes = await humClient.orgsOrgIdAppsAppIdEnvsEnvIdRulesPost({
orgId,
appId,
envId,
automationRuleRequest: {
active: true,
artefacts_filter: [image],
type: 'update',
match_ref: matchRef,
},
});
if (createRuleRes.status != 201) {
throw new Error(`Unexpected response creating rule: ${baseEnvRes.status}, ${baseEnvRes.data}`);

if (createAutomationRule) {
const matchRef =`refs/heads/${branchName}`;
const createRuleRes = await humClient.orgsOrgIdAppsAppIdEnvsEnvIdRulesPost({
orgId,
appId,
envId,
automationRuleRequest: {
active: true,
artefacts_filter: [image],
type: 'update',
match_ref: matchRef,
},
});
if (createRuleRes.status != 201) {
throw new Error(`Unexpected response creating rule: ${baseEnvRes.status}, ${baseEnvRes.data}`);
}

console.log(`Created auto-deployment rule for ${matchRef} and image ${image}`);
}

console.log(`Created auto-deployment rule for ${matchRef} and image ${image}`);

if (!octokit) {
return;
Expand Down Expand Up @@ -238,6 +244,7 @@ export async function runAction(): Promise<void> {
}
info('Using environment: '+environmentUrl);
setOutput('environment-url', environmentUrl);
setOutput('humanitec-env', envId);
if (action == 'get-environment-url') {
return;
}
Expand Down
13 changes: 9 additions & 4 deletions src/humanitec/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import {apiConfig, PublicApi} from '@humanitec/autogen';
import {APIConfig, apiConfig, PublicApi} from '@humanitec/autogen';

export type HumanitecClient = PublicApi

export const createApiClient = (basePath: string, token: string): PublicApi => {
const config = apiConfig({
const clientConfig: APIConfig = {
token,
apiHost: `https://${basePath}`,
internalApp: 'preview-envs-action/latest',
});
};

if (basePath) {
clientConfig.apiHost = `https://${basePath}`;
}

const config = apiConfig(clientConfig);

return new PublicApi(config);
};

0 comments on commit 12d8d4c

Please sign in to comment.