Set record data with repo commit hash and package.json content (#65)

* Get latest commit hash from repo when adding project

* Update UI/UX

* Fill registry record with data from package.json

* Add package json type

* Correct record data based on laconic console

* Update README

---------

Co-authored-by: neeraj <neeraj.rtly@gmail.com>
This commit is contained in:
2024-02-14 11:03:22 +05:30
committed by GitHub
co-authored by neeraj
parent 76dfd3bb76
commit 9144d42f70
19 changed files with 203 additions and 105 deletions
+2
View File
@@ -16,8 +16,10 @@
"express": "^4.18.2",
"fs-extra": "^11.2.0",
"graphql": "^16.8.1",
"luxon": "^3.4.4",
"nanoid": "3",
"nanoid-dictionary": "^5.0.0-beta.1",
"octokit": "^3.1.2",
"reflect-metadata": "^0.2.1",
"semver": "^7.6.0",
"toml": "^3.0.0",
+7 -7
View File
@@ -28,13 +28,13 @@ export enum DeploymentStatus {
export interface ApplicationRecord {
type: string;
version:string
name: string
description: string
homepage: string
license: string
author: string
repository: string,
app_version: string
name?: string
description?: string
homepage?: string
license?: string
author?: string
repository?: string[],
app_version?: string
repository_ref: string
app_type: string
}
+2 -8
View File
@@ -20,14 +20,8 @@ export interface ApplicationDeploymentRequest {
version: string
name: string
application: string
config: {
env: {[key:string]: string}
},
meta: {
note: string
repository: string
repository_ref: string
}
config: string,
meta: string
}
@Entity()
+52 -32
View File
@@ -1,17 +1,19 @@
import debug from 'debug';
import assert from 'assert';
import { inc as semverInc } from 'semver';
import { DateTime } from 'luxon';
import { Registry as LaconicRegistry } from '@cerc-io/laconic-sdk';
import { RegistryConfig } from './config';
import { ApplicationDeploymentRequest } from './entity/Project';
import { ApplicationRecord } from './entity/Deployment';
import { PackageJSON } from './types';
const log = debug('snowball:registry');
const APP_RECORD_TYPE = 'ApplicationRecord';
const DEPLOYMENT_RECORD_TYPE = 'ApplicationDeploymentRequest';
const AUTHORITY_NAME = 'snowball';
// TODO: Move registry code to laconic-sdk/watcher-ts
export class Registry {
@@ -23,16 +25,21 @@ export class Registry {
this.registry = new LaconicRegistry(registryConfig.gqlEndpoint, registryConfig.restEndpoint, registryConfig.chainId);
}
async createApplicationRecord (data: { recordName: string, appType: string }): Promise<{registryRecordId: string, registryRecordData: ApplicationRecord}> {
// TODO: Get record name from repo package.json name
const recordName = data.recordName;
async createApplicationRecord ({
packageJSON,
commitHash,
appType
}: {
packageJSON: PackageJSON
appType: string,
commitHash: string
}): Promise<{registryRecordId: string, registryRecordData: ApplicationRecord}> {
// Use laconic-sdk to publish record
// Reference: https://git.vdb.to/cerc-io/test-progressive-web-app/src/branch/main/scripts/publish-app-record.sh
// Fetch previous records
const records = await this.registry.queryRecords({
type: APP_RECORD_TYPE,
name: recordName
name: packageJSON.name
}, true);
// Get next version of record
@@ -40,23 +47,21 @@ export class Registry {
const [latestBondRecord] = bondRecords.sort((a: any, b: any) => new Date(b.createTime).getTime() - new Date(a.createTime).getTime());
const nextVersion = semverInc(latestBondRecord?.attributes.version ?? '0.0.0', 'patch');
assert(nextVersion, 'Application record version not valid');
// Create record of type ApplicationRecord and publish
const applicationRecord = {
type: APP_RECORD_TYPE,
version: nextVersion ?? '',
name: recordName,
// TODO: Get data from repo package.json
description: '',
homepage: '',
license: '',
author: '',
repository: '',
app_version: '0.1.0',
// TODO: Get latest commit hash from repo production branch / deployment
repository_ref: '10ac6678e8372a05ad5bb1c34c34',
app_type: data.appType
version: nextVersion,
repository_ref: commitHash,
app_type: appType,
...(packageJSON.name && { name: packageJSON.name }),
...(packageJSON.description && { description: packageJSON.description }),
...(packageJSON.homepage && { homepage: packageJSON.homepage }),
...(packageJSON.license && { license: packageJSON.license }),
...(packageJSON.author && { author: typeof packageJSON.author === 'object' ? JSON.stringify(packageJSON.author) : packageJSON.author }),
...(packageJSON.repository && { repository: [packageJSON.repository] }),
...(packageJSON.version && { app_version: packageJSON.version })
};
const result = await this.registry.setRecord(
@@ -71,8 +76,8 @@ export class Registry {
log('Application record data:', applicationRecord);
// TODO: Discuss computation of crn
const crn = this.getCrn(data.recordName);
// TODO: Discuss computation of CRN
const crn = this.getCrn(packageJSON.name ?? '');
await this.registry.setName({ cid: result.data.id, crn }, this.registryConfig.privateKey, this.registryConfig.fee);
await this.registry.setName({ cid: result.data.id, crn: `${crn}@${applicationRecord.app_version}` }, this.registryConfig.privateKey, this.registryConfig.fee);
@@ -81,7 +86,14 @@ export class Registry {
return { registryRecordId: result.data.id, registryRecordData: applicationRecord };
}
async createApplicationDeploymentRequest (data: { appName: string }): Promise<{registryRecordId: string, registryRecordData: ApplicationDeploymentRequest}> {
async createApplicationDeploymentRequest (data: {
appName: string,
commitHash: string,
repository: string
}): Promise<{
registryRecordId: string,
registryRecordData: ApplicationDeploymentRequest
}> {
const crn = this.getCrn(data.appName);
const records = await this.registry.resolveNames([crn]);
const applicationRecord = records[0];
@@ -101,16 +113,17 @@ export class Registry {
// dns: '$CERC_REGISTRY_DEPLOYMENT_SHORT_HOSTNAME',
// deployment: '$CERC_REGISTRY_DEPLOYMENT_CRN',
config: {
// https://git.vdb.to/cerc-io/laconic-registry-cli/commit/129019105dfb93bebcea02fde0ed64d0f8e5983b
config: JSON.stringify({
env: {
CERC_WEBAPP_DEBUG: `${applicationRecord.attributes.app_version}`
}
},
meta: {
note: `Added by Snowball @ ${(new Date()).toISOString()}`,
repository: applicationRecord.attributes.repository,
repository_ref: applicationRecord.attributes.repository_ref
}
}),
meta: JSON.stringify({
note: `Added by Snowball @ ${DateTime.utc().toFormat('EEE LLL dd HH:mm:ss \'UTC\' yyyy')}`,
repository: data.repository,
repository_ref: data.commitHash
})
};
const result = await this.registry.setRecord(
@@ -128,7 +141,14 @@ export class Registry {
return { registryRecordId: result.data.id, registryRecordData: applicationDeploymentRequest };
}
getCrn (appName: string): string {
return `crn://${AUTHORITY_NAME}/applications/${appName}`;
getCrn (packageJsonName: string): string {
const [arg1, arg2] = packageJsonName.split('/');
if (arg2) {
const authority = arg1.replace('@', '');
return `crn://${authority}/applications/${arg2}`;
}
return `crn://${arg1}/applications/${arg1}`;
}
}
+63 -12
View File
@@ -1,6 +1,7 @@
import assert from 'assert';
import debug from 'debug';
import { DeepPartial, FindOptionsWhere } from 'typeorm';
import { Octokit } from 'octokit';
import { OAuthApp } from '@octokit/oauth-app';
@@ -18,12 +19,12 @@ const log = debug('snowball:service');
export class Service {
private db: Database;
private app: OAuthApp;
private oauthApp: OAuthApp;
private registry: Registry;
constructor (db: Database, app: OAuthApp, registry: Registry) {
this.db = db;
this.app = app;
this.oauthApp = app;
this.registry = registry;
}
@@ -35,6 +36,13 @@ export class Service {
});
}
async getOctokit (userId: string): Promise<Octokit> {
const user = await this.db.getUser({ where: { id: userId } });
assert(user && user.gitHubToken, 'User needs to be authenticated with GitHub token');
return new Octokit({ auth: user.gitHubToken });
}
async getOrganizationsByUserId (userId: string): Promise<Organization[]> {
const dbOrganizations = await this.db.getOrganizationsByUserId(userId);
return dbOrganizations;
@@ -200,7 +208,10 @@ export class Service {
await this.db.updateDeploymentById(oldCurrentDeployment.id, { isCurrent: false, domain: null });
}
const octokit = await this.getOctokit(userId);
const newDeployement = await this.createDeployment(userId,
octokit,
{
project: oldDeployment.project,
isCurrent: true,
@@ -213,10 +224,28 @@ export class Service {
return newDeployement;
}
async createDeployment (userId: string, data: DeepPartial<Deployment>): Promise<Deployment> {
async createDeployment (userId: string, octokit: Octokit, data: DeepPartial<Deployment>): Promise<Deployment> {
assert(data.project?.repository, 'Project repository not found');
const [owner, repo] = data.project.repository.split('/');
const { data: packageJSONData } = await octokit.rest.repos.getContent({
owner,
repo,
path: 'package.json',
ref: data.commitHash
});
if (!packageJSONData) {
throw new Error('Package.json file not found');
}
assert(!Array.isArray(packageJSONData) && packageJSONData.type === 'file');
const packageJSON = JSON.parse(atob(packageJSONData.content));
const { registryRecordId, registryRecordData } = await this.registry.createApplicationRecord({
recordName: data.project?.name ?? '',
appType: data.project?.template ?? ''
packageJSON,
appType: data.project!.template!,
commitHash: data.commitHash!
});
const newDeployement = await this.db.addDeployement({
@@ -250,24 +279,43 @@ export class Service {
const project = await this.db.addProject(userId, organization.id, data);
// TODO: Get repository details from github
await this.createDeployment(userId,
const octokit = await this.getOctokit(userId);
const [owner, repo] = project.repository.split('/');
const { data: [latestCommit] } = await octokit.rest.repos.listCommits({
owner,
repo,
sha: project.prodBranch,
per_page: 1
});
const { data: repoDetails } = await octokit.rest.repos.get({ owner, repo });
// Create deployment with prod branch and latest commit
const newDeployment = await this.createDeployment(userId,
octokit,
{
project,
isCurrent: true,
branch: project.prodBranch,
environment: Environment.Production,
// TODO: Set latest commit hash
commitHash: '',
domain: null
domain: null,
commitHash: latestCommit.sha
});
const { registryRecordId, registryRecordData } = await this.registry.createApplicationDeploymentRequest({ appName: project.name });
const { registryRecordId, registryRecordData } = await this.registry.createApplicationDeploymentRequest(
{
appName: newDeployment.registryRecordData.name!,
commitHash: latestCommit.sha,
repository: repoDetails.git_url
});
await this.db.updateProjectById(project.id, {
registryRecordId,
registryRecordData
});
// TODO: Setup repo webhook for push events
return project;
}
@@ -311,7 +359,10 @@ export class Service {
await this.db.updateDeploymentById(deploymentId, { domain: null, isCurrent: false });
const octokit = await this.getOctokit(userId);
const newDeployement = await this.createDeployment(userId,
octokit,
{
project: oldDeployment.project,
// TODO: Put isCurrent field in project
@@ -435,7 +486,7 @@ export class Service {
}
async authenticateGitHub (code:string, userId: string): Promise<{token: string}> {
const { authentication: { token } } = await this.app.createToken({
const { authentication: { token } } = await this.oauthApp.createToken({
code
});
+9
View File
@@ -0,0 +1,9 @@
export interface PackageJSON {
name?: string;
version?: string;
author?: string;
description?: string;
homepage?: string;
license?: string;
repository?: string;
}
+10 -10
View File
@@ -10,7 +10,7 @@
"registryRecordId": "qbafyrehvzya6ovp4yfpkqnddkui2iw7t6hbhwq74lbqs7bhobvmfhrowoi",
"registryRecordData": {},
"branch": "main",
"commitHash": "testXyz",
"commitHash": "d5dfd7b827226b0d09d897346d291c256e113e00",
"url": "testProject-ffhae3zq.snowball.xyz"
},
{
@@ -24,7 +24,7 @@
"registryRecordId": "wbafyreihvzya6ovp4yfpkqnddkui2iw7thbhwq74lbqs7bhobvmfhrowoi",
"registryRecordData": {},
"branch": "test",
"commitHash": "testXyz",
"commitHash": "d5dfd7b827226b0d09d897346d291c256e113e00",
"url": "testProject-vehagei8.snowball.xyz"
},
{
@@ -38,7 +38,7 @@
"registryRecordId": "ebafyreihvzya6ovp4yfpkqnddkui2iw7t6bhwq74lbqs7bhobvmfhrowoi",
"registryRecordData": {},
"branch": "test",
"commitHash": "testXyz",
"commitHash": "d5dfd7b827226b0d09d897346d291c256e113e00",
"url": "testProject-qmgekyte.snowball.xyz"
},
{
@@ -52,7 +52,7 @@
"registryRecordId": "rbafyreihvzya6ovp4yfpkqnddkui2iw7t6hbhw74lbqs7bhobvmfhrowoi",
"registryRecordData": {},
"branch": "prod",
"commitHash": "testXyz",
"commitHash": "d5dfd7b827226b0d09d897346d291c256e113e00",
"url": "testProject-f8wsyim6.snowball.xyz"
},
{
@@ -66,7 +66,7 @@
"registryRecordId": "tbafyreihvzya6ovp4yfpqnddkui2iw7t6hbhwq74lbqs7bhobvmfhrowoi",
"registryRecordData": {},
"branch": "main",
"commitHash": "testXyz",
"commitHash": "d5dfd7b827226b0d09d897346d291c256e113e00",
"url": "testProject-2-eO8cckxk.snowball.xyz"
},
{
@@ -80,7 +80,7 @@
"registryRecordId": "ybafyreihvzya6ovp4yfpkqnddkui2iw7t6bhwq74lbqs7bhobvmfhrowoi",
"registryRecordData": {},
"branch": "test",
"commitHash": "testXyz",
"commitHash": "d5dfd7b827226b0d09d897346d291c256e113e00",
"url": "testProject-2-yaq0t5yw.snowball.xyz"
},
{
@@ -94,7 +94,7 @@
"registryRecordId": "ubafyreihvzya6ovp4yfpkqnddkui2iw7t6hbhwq74lbqs7bhobvfhrowoi",
"registryRecordData": {},
"branch": "test",
"commitHash": "testXyz",
"commitHash": "d5dfd7b827226b0d09d897346d291c256e113e00",
"url": "testProject-2-hwwr6sbx.snowball.xyz"
},
{
@@ -108,7 +108,7 @@
"registryRecordId": "ibayreihvzya6ovp4yfpkqnddkui2iw7t6hbhwq74lbqs7bhobvmfhrowoi",
"registryRecordData": {},
"branch": "main",
"commitHash": "testXyz",
"commitHash": "d5dfd7b827226b0d09d897346d291c256e113e00",
"url": "iglootools-ndxje48a.snowball.xyz"
},
{
@@ -122,7 +122,7 @@
"registryRecordId": "obafyreihvzya6ovp4yfpkqnddkui2iw7t6hbhwq74lbqs7bhobvmfhrowoi",
"registryRecordData": {},
"branch": "test",
"commitHash": "testXyz",
"commitHash": "d5dfd7b827226b0d09d897346d291c256e113e00",
"url": "iglootools-gtgpgvei.snowball.xyz"
},
{
@@ -136,7 +136,7 @@
"registryRecordId": "pbafyreihvzya6ovp4yfpkqnddkui2iw7t6hbhwq74lbqs7bhobvmfhrowo",
"registryRecordData": {},
"branch": "test",
"commitHash": "testXyz",
"commitHash": "d5dfd7b827226b0d09d897346d291c256e113e00",
"url": "iglootools-b4bpthjr.snowball.xyz"
}
]
+10 -10
View File
@@ -3,10 +3,10 @@
"ownerIndex": 0,
"organizationIndex": 0,
"name": "testProject",
"repository": "test",
"repository": "snowball-tools/snowball-ts-framework-template",
"prodBranch": "main",
"description": "test",
"template": "test",
"template": "webapp",
"framework": "test",
"webhooks": [],
"icon": "",
@@ -18,10 +18,10 @@
"ownerIndex": 1,
"organizationIndex": 0,
"name": "testProject-2",
"repository": "test-2",
"repository": "snowball-tools/snowball-ts-framework-template",
"prodBranch": "main",
"description": "test-2",
"template": "test-2",
"template": "webapp",
"framework": "test-2",
"webhooks": [],
"icon": "",
@@ -33,10 +33,10 @@
"ownerIndex": 2,
"organizationIndex": 0,
"name": "iglootools",
"repository": "test-3",
"repository": "snowball-tools/snowball-ts-framework-template",
"prodBranch": "main",
"description": "test-3",
"template": "test-3",
"template": "webapp",
"framework": "test-3",
"webhooks": [],
"icon": "",
@@ -48,10 +48,10 @@
"ownerIndex": 1,
"organizationIndex": 0,
"name": "iglootools-2",
"repository": "test-4",
"repository": "snowball-tools/snowball-ts-framework-template",
"prodBranch": "main",
"description": "test-4",
"template": "test-4",
"template": "webapp",
"framework": "test-4",
"webhooks": [],
"icon": "",
@@ -63,10 +63,10 @@
"ownerIndex": 0,
"organizationIndex": 1,
"name": "snowball-2",
"repository": "test-5",
"repository": "snowball-tools/snowball-ts-framework-template",
"prodBranch": "main",
"description": "test-5",
"template": "test-5",
"template": "webapp",
"framework": "test-5",
"webhooks": [],
"icon": "",