Skip to content

Commit

Permalink
feat: use Humanitec TS client
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Jun 26, 2024
1 parent 83a88b5 commit c3435c4
Show file tree
Hide file tree
Showing 24 changed files with 332 additions and 617 deletions.
8 changes: 8 additions & 0 deletions .changeset/nice-cobras-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@humanitec/backstage-plugin-scaffolder-backend-module': minor
'@humanitec/backstage-plugin-backend': minor
'@humanitec/backstage-plugin-common': minor
'@humanitec/backstage-plugin': minor
---

Use Humanitec TS client
2 changes: 1 addition & 1 deletion backstage.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "1.27.7"
"version": "1.28.2"
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
]
},
"devDependencies": {
"@backstage/cli": "^0.26.6",
"@backstage/cli": "^0.26.9",
"@backstage/e2e-test-utils": "^0.1.1",
"@playwright/test": "^1.32.3",
"@spotify/prettier-config": "^12.0.0",
Expand All @@ -44,6 +44,7 @@
"typescript": "~5.3.0"
},
"resolutions": {
"@azure/storage-blob": "12.18.0",
"@types/react": "^18",
"@types/react-dom": "^18"
},
Expand Down
48 changes: 24 additions & 24 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@
"lint": "backstage-cli package lint"
},
"dependencies": {
"@backstage/app-defaults": "^1.5.5",
"@backstage/app-defaults": "^1.5.6",
"@backstage/catalog-model": "^1.5.0",
"@backstage/cli": "^0.26.6",
"@backstage/core-app-api": "^1.12.5",
"@backstage/core-components": "^0.14.7",
"@backstage/core-plugin-api": "^1.9.2",
"@backstage/integration-react": "^1.1.27",
"@backstage/plugin-api-docs": "^0.11.5",
"@backstage/plugin-catalog": "^1.20.0",
"@backstage/plugin-catalog-common": "^1.0.23",
"@backstage/plugin-catalog-graph": "^0.4.5",
"@backstage/plugin-catalog-import": "^0.11.0",
"@backstage/plugin-catalog-react": "^1.12.0",
"@backstage/plugin-org": "^0.6.25",
"@backstage/plugin-permission-react": "^0.4.22",
"@backstage/plugin-scaffolder": "^1.20.1",
"@backstage/plugin-scaffolder-react": "^1.8.6",
"@backstage/plugin-search": "^1.4.11",
"@backstage/plugin-search-react": "^1.7.11",
"@backstage/plugin-techdocs": "^1.10.5",
"@backstage/plugin-techdocs-module-addons-contrib": "^1.1.10",
"@backstage/plugin-techdocs-react": "^1.2.4",
"@backstage/plugin-user-settings": "^0.8.6",
"@backstage/theme": "^0.5.5",
"@backstage/cli": "^0.26.9",
"@backstage/core-app-api": "^1.12.6",
"@backstage/core-components": "^0.14.8",
"@backstage/core-plugin-api": "^1.9.3",
"@backstage/integration-react": "^1.1.28",
"@backstage/plugin-api-docs": "^0.11.6",
"@backstage/plugin-catalog": "^1.21.0",
"@backstage/plugin-catalog-common": "^1.0.24",
"@backstage/plugin-catalog-graph": "^0.4.6",
"@backstage/plugin-catalog-import": "^0.12.0",
"@backstage/plugin-catalog-react": "^1.12.1",
"@backstage/plugin-org": "^0.6.26",
"@backstage/plugin-permission-react": "^0.4.23",
"@backstage/plugin-scaffolder": "^1.21.0",
"@backstage/plugin-scaffolder-react": "^1.9.0",
"@backstage/plugin-search": "^1.4.12",
"@backstage/plugin-search-react": "^1.7.12",
"@backstage/plugin-techdocs": "^1.10.6",
"@backstage/plugin-techdocs-module-addons-contrib": "^1.1.11",
"@backstage/plugin-techdocs-react": "^1.2.5",
"@backstage/plugin-user-settings": "^0.8.7",
"@backstage/theme": "^0.5.6",
"@humanitec/backstage-plugin": "^0.7.0",
"@material-ui/core": "^4.12.2",
"@material-ui/icons": "^4.9.1",
Expand All @@ -49,7 +49,7 @@
"react-use": "^17.2.4"
},
"devDependencies": {
"@backstage/test-utils": "^1.5.5",
"@backstage/test-utils": "^1.5.6",
"@playwright/test": "^1.32.3",
"@testing-library/dom": "^9.0.0",
"@testing-library/jest-dom": "^6.0.0",
Expand Down
44 changes: 22 additions & 22 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@
"build-image": "docker build ../.. -f Dockerfile --tag backstage"
},
"dependencies": {
"@backstage/backend-common": "^0.22.0",
"@backstage/backend-defaults": "^0.2.18",
"@backstage/backend-tasks": "^0.5.23",
"@backstage/backend-common": "^0.23.1",
"@backstage/backend-defaults": "^0.3.2",
"@backstage/backend-tasks": "^0.5.25",
"@backstage/config": "^1.2.0",
"@backstage/plugin-app-backend": "^0.3.67",
"@backstage/plugin-auth-backend": "^0.22.5",
"@backstage/plugin-auth-backend-module-github-provider": "^0.1.15",
"@backstage/plugin-auth-backend-module-guest-provider": "^0.1.4",
"@backstage/plugin-auth-node": "^0.4.13",
"@backstage/plugin-catalog-backend": "^1.22.0",
"@backstage/plugin-catalog-backend-module-scaffolder-entity-model": "^0.1.16",
"@backstage/plugin-permission-backend": "^0.5.42",
"@backstage/plugin-permission-backend-module-allow-all-policy": "^0.1.15",
"@backstage/plugin-permission-common": "^0.7.13",
"@backstage/plugin-permission-node": "^0.7.29",
"@backstage/plugin-proxy-backend": "^0.4.16",
"@backstage/plugin-scaffolder-backend": "^1.22.8",
"@backstage/plugin-search-backend": "^1.5.9",
"@backstage/plugin-search-backend-module-catalog": "^0.1.24",
"@backstage/plugin-search-backend-module-techdocs": "^0.1.23",
"@backstage/plugin-search-backend-node": "^1.2.23",
"@backstage/plugin-techdocs-backend": "^1.10.5",
"@backstage/plugin-app-backend": "^0.3.69",
"@backstage/plugin-auth-backend": "^0.22.7",
"@backstage/plugin-auth-backend-module-github-provider": "^0.1.17",
"@backstage/plugin-auth-backend-module-guest-provider": "^0.1.6",
"@backstage/plugin-auth-node": "^0.4.15",
"@backstage/plugin-catalog-backend": "^1.23.1",
"@backstage/plugin-catalog-backend-module-scaffolder-entity-model": "^0.1.18",
"@backstage/plugin-permission-backend": "^0.5.44",
"@backstage/plugin-permission-backend-module-allow-all-policy": "^0.1.17",
"@backstage/plugin-permission-common": "^0.7.14",
"@backstage/plugin-permission-node": "^0.7.31",
"@backstage/plugin-proxy-backend": "^0.5.1",
"@backstage/plugin-scaffolder-backend": "^1.22.10",
"@backstage/plugin-search-backend": "^1.5.12",
"@backstage/plugin-search-backend-module-catalog": "^0.1.26",
"@backstage/plugin-search-backend-module-techdocs": "^0.1.25",
"@backstage/plugin-search-backend-node": "^1.2.25",
"@backstage/plugin-techdocs-backend": "^1.10.7",
"@humanitec/backstage-plugin-backend": "^0.6.3",
"@humanitec/backstage-plugin-scaffolder-backend-module": "^0.3.0",
"app": "link:../app",
Expand All @@ -49,7 +49,7 @@
"winston": "^3.2.1"
},
"devDependencies": {
"@backstage/cli": "^0.26.6",
"@backstage/cli": "^0.26.9",
"@types/dockerode": "^3.3.0",
"@types/express": "^4.17.6",
"@types/express-serve-static-core": "^4.17.5",
Expand Down
7 changes: 4 additions & 3 deletions plugins/humanitec-backend-scaffolder-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
},
"dependencies": {
"@backstage/backend-plugin-api": "0.6.18",
"@backstage/plugin-scaffolder-node": "^0.4.4",
"@backstage/plugin-scaffolder-node": "^0.4.6",
"@humanitec/backstage-plugin-common": "^0.5.1",
"js-yaml": "^4.1.0"
"js-yaml": "^4.1.0",
"zod": "^3.17.3"
},
"devDependencies": {
"@backstage/cli": "^0.26.6",
"@backstage/cli": "^0.26.9",
"@types/js-yaml": "^4.0.5",
"@types/supertest": "^2.0.8",
"msw": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import { TemplateAction, createTemplateAction } from '@backstage/plugin-scaffold
import { stat, readFile } from 'fs/promises';
import { join, resolve } from 'path';
import { loadAll } from 'js-yaml';
import { SetupFileSchema, createHumanitecClient } from '@humanitec/backstage-plugin-common';
import { createHumanitecClient } from '@humanitec/backstage-plugin-common';
import { object, string, array } from 'zod';

const SetupDocument = object({
id: string(),
name: string()
});

export const SetupFileSchema = array(SetupDocument);

interface HumanitecCreateApp {
orgId: string;
Expand All @@ -25,7 +33,7 @@ export function createHumanitecApp({ token, orgId }: HumanitecCreateApp): Templa
},
async handler(ctx) {
const { input, workspacePath, logger } = ctx;
const client = createHumanitecClient({ orgId, token });
const client = createHumanitecClient({ token });

const setupFile = input.setupFile ?? 'humanitec-apps.yaml';
const setupFilePath = resolve(join(workspacePath, setupFile));
Expand All @@ -42,78 +50,17 @@ export function createHumanitecApp({ token, orgId }: HumanitecCreateApp): Templa
const apps = SetupFileSchema.parse(setupFileContent);

for (const app of apps) {
let _app: { id: string, name: string };
try {
_app = await client.createApplication({ id: app.id, name: app.name });
await client.createApplication({
orgId,
ApplicationCreationRequest: { id: app.id, name: app.name }
});
logger.info(`Created ${app.name} with ${app.id}`)
} catch (e) {
logger.error(`Failed to create app ${app.id} with name ${app.name}`)
logger.debug(e);
continue;
}

if (app.environments) {
for (const key in app.environments) {
if (Object.prototype.hasOwnProperty.call(app.environments, key)) {
const env = app.environments[key];

const payload = {
metadata: env.metadata,
modules: {
add: env.modules,
update: {},
remove: []
},
};

try {
const delta = await client.createDelta(_app.id, payload);

const url = client.buildUrl({
resource: 'DELTA',
env_id: env.metadata.env_id,
delta_id: delta.id,
app_id: _app.id
});

logger.info(`Created delta ${url}`);
logger.debug(`Delta payload: ${JSON.stringify(payload)}`);

try {
const deployment = await client.deployDelta(_app.id, delta.metadata.env_id, {
delta_id: delta.id,
comment: `Initial deployment of delta ${delta.id}`
});
logger.info(`Created deployment: ${deployment.id}`);
} catch (e) {
logger.error(`Could not create deployment for ${delta.id}`);
logger.debug(e);
}

} catch (e) {
logger.error(`Could not create delta for ${_app.id}`);
logger.debug(e);
}
}
}
}

if (app.automations) {
for (const env_id in app.automations) {
if (Object.prototype.hasOwnProperty.call(app.automations, env_id)) {
for (const automation of app.automations[env_id]) {
try {
const created = await client.createAutomation(_app.id, env_id, automation);
logger.info(`Created automation[id: ${created.id}]`);
logger.debug(`Automation payload: ${JSON.stringify(automation)}`);
} catch (e) {
logger.error(`Failed to create automation`)
logger.debug(e);
}
}
}
}
}
}
},
});
Expand Down
4 changes: 2 additions & 2 deletions plugins/humanitec-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"postpack": "backstage-cli package postpack"
},
"dependencies": {
"@backstage/backend-common": "^0.22.0",
"@backstage/backend-common": "^0.23.1",
"@backstage/backend-plugin-api": "0.6.18",
"@backstage/config": "^1.2.0",
"@humanitec/backstage-plugin-common": "^0.5.1",
Expand All @@ -40,7 +40,7 @@
"yn": "^4.0.0"
},
"devDependencies": {
"@backstage/cli": "^0.26.6",
"@backstage/cli": "^0.26.9",
"@types/supertest": "^2.0.8",
"msw": "^1.0.0",
"supertest": "^6.2.4"
Expand Down
15 changes: 11 additions & 4 deletions plugins/humanitec-backend/src/service/app-info-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from 'events';

import { createHumanitecClient, fetchAppInfo } from '@humanitec/backstage-plugin-common';
import { createHumanitecClient, fetchAppInfo, HumanitecResponseError } from '@humanitec/backstage-plugin-common';

const defaultFetchInterval = 10000;

Expand All @@ -21,10 +21,12 @@ export class AppInfoService {

private token: string;
private fetchInterval: number;
private client: ReturnType<typeof createHumanitecClient>;

constructor(token: string, fetchInterval = defaultFetchInterval) {
this.token = token;
this.fetchInterval = fetchInterval;
this.client = createHumanitecClient({ token: this.token });
}

addSubscriber(orgId: string, appId: string, subscriber: (data: AppInfoUpdate) => void): () => void {
Expand Down Expand Up @@ -54,16 +56,21 @@ export class AppInfoService {

private async fetchAppInfo(orgId: string, appId: string): Promise<void> {
const key = `${orgId}:${appId}`;
const client = createHumanitecClient({ token: this.token, orgId });

const id = this.lastData.has(key) ? this.lastData.get(key)!.id + 1 : 0;

const update: AppInfoUpdate = { id: id };
try {
const data = await fetchAppInfo({ client }, appId);
const data = await fetchAppInfo({ client: this.client }, orgId, appId);
update.data = data;
} catch (error) {
if (error instanceof Error) {
if (error instanceof HumanitecResponseError) {
let msg = `Failed to fetch ${error.response.url} (${error.response.status})`;
if (!error.response.bodyUsed) {
msg += `\n${await error.response.text()}`;
}
update.error = new Error(msg);
} else if (error instanceof Error) {
update.error = error;
} else {
update.error = new Error(`${error}`);
Expand Down
2 changes: 1 addition & 1 deletion plugins/humanitec-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function createRouter(

const unsubscribe = appInfoService.addSubscriber(orgId, appId, (data) => {
if (data.error) {
response.write(`event: update-failure\ndata: ${data.error.message}\nid: ${data.id}\n\n`);
response.write(`event: update-failure\ndata: ${JSON.stringify(data.error.message)}\nid: ${data.id}\n\n`);
logger.error(`Error encountered trying to update environment`, data.error);
} else {
response.write(`event: update-success\ndata: ${JSON.stringify(data.data)}\nid: ${data.id}\n\n`);
Expand Down
6 changes: 3 additions & 3 deletions plugins/humanitec-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
"postpack": "backstage-cli package postpack"
},
"dependencies": {
"@humanitec/autogen": "^0.1.0",
"cross-fetch": "^4.0.0",
"exponential-backoff": "^3.1.0",
"zod": "^3.17.3"
"exponential-backoff": "^3.1.0"
},
"devDependencies": {
"@backstage/cli": "^0.26.6"
"@backstage/cli": "^0.26.9"
},
"files": [
"dist"
Expand Down
Loading

0 comments on commit c3435c4

Please sign in to comment.