diff --git a/plugins/scaffolder-backend/src/scaffolder/index.ts b/plugins/scaffolder-backend/src/scaffolder/index.ts index 2c689489e4..470bc230d6 100644 --- a/plugins/scaffolder-backend/src/scaffolder/index.ts +++ b/plugins/scaffolder-backend/src/scaffolder/index.ts @@ -13,6 +13,5 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -export * from './stages/templater'; -export * from './stages/prepare'; +export * from './stages'; export * from './jobs'; diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/index.ts b/plugins/scaffolder-backend/src/scaffolder/stages/index.ts new file mode 100644 index 0000000000..78ea30db79 --- /dev/null +++ b/plugins/scaffolder-backend/src/scaffolder/stages/index.ts @@ -0,0 +1,18 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export * from './prepare'; +export * from './publish'; +export * from './templater'; diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.test.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.test.ts new file mode 100644 index 0000000000..f91e8bb40e --- /dev/null +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.test.ts @@ -0,0 +1,16 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +describe('Github Store', () => {}); diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/store/github.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts similarity index 80% rename from plugins/scaffolder-backend/src/scaffolder/stages/store/github.ts rename to plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts index 8f5cc40ff9..9aed55fd98 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/store/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts @@ -14,25 +14,33 @@ * limitations under the License. */ -import { Storer } from './types'; +import { Publisher } from './types'; import { Octokit } from '@octokit/rest'; -import { TemplateEntityV1alpha1 } from '@backstage/catalog-model'; + import { JsonValue } from '@backstage/config'; import { RequiredTemplateValues } from '../templater'; import { Repository, Remote, Signature, Cred } from 'nodegit'; -export class GithubStorer implements Storer { +export class GithubPublisher implements Publisher { private client: Octokit; constructor({ client }: { client: Octokit }) { this.client = client; } - async createRemote({ + async publish({ values, + directory, }: { - entity: TemplateEntityV1alpha1; values: RequiredTemplateValues & Record; - }) { + directory: string; + }): Promise<{ remoteUrl: string }> { + const remoteUrl = await this.createRemote(values); + await this.pushToRemote(directory, remoteUrl); + + return { remoteUrl }; + } + + private async createRemote(values: RequiredTemplateValues) { const [owner, name] = values.storePath.split('/'); const { @@ -45,7 +53,7 @@ export class GithubStorer implements Storer { return cloneUrl; } - async pushToRemote(directory: string, remote: string): Promise { + private async pushToRemote(directory: string, remote: string): Promise { const repo = await Repository.init(directory, 0); const index = await repo.refreshIndex(); await index.addAll(); diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/index.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/index.ts new file mode 100644 index 0000000000..dcfd2c9c34 --- /dev/null +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/index.ts @@ -0,0 +1,16 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export * from './github'; diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/store/types.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/types.ts similarity index 60% rename from plugins/scaffolder-backend/src/scaffolder/stages/store/types.ts rename to plugins/scaffolder-backend/src/scaffolder/stages/publish/types.ts index ae63a05197..a6db297bde 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/store/types.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/types.ts @@ -13,11 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { TemplateEntityV1alpha1 } from "@backstage/catalog-model"; -import { RequiredTemplateValues } from "../templater"; -import { JsonValue } from "@backstage/config"; +import { TemplateEntityV1alpha1 } from '@backstage/catalog-model'; +import { RequiredTemplateValues } from '../templater'; +import { JsonValue } from '@backstage/config'; -export type Storer = { - createRemote(opts: { entity: TemplateEntityV1alpha1, values: RequiredTemplateValues & Record}): Promise; - pushToRemote(directory: string, remote: string): Promise; -} +export type Publisher = { + publish(opts: { + entity: TemplateEntityV1alpha1; + values: RequiredTemplateValues & Record; + directory: string; + }): Promise<{ remoteUrl: string }>; +}; diff --git a/plugins/scaffolder-backend/src/service/router.ts b/plugins/scaffolder-backend/src/service/router.ts index efa2f9588d..633be88d4c 100644 --- a/plugins/scaffolder-backend/src/service/router.ts +++ b/plugins/scaffolder-backend/src/service/router.ts @@ -22,13 +22,14 @@ import { TemplaterBase, JobProcessor, RequiredTemplateValues, + StageContext, + GithubPublisher, } from '../scaffolder'; import { TemplateEntityV1alpha1 } from '@backstage/catalog-model'; import Docker from 'dockerode'; import {} from '@backstage/backend-common'; -import { StageContext } from '../scaffolder/jobs/types'; import { Octokit } from '@octokit/rest'; -import { GithubStorer } from '../scaffolder/stages/store/github'; + import { JsonValue } from '@backstage/config'; export interface RouterOptions { preparers: PreparerBuilder; @@ -44,7 +45,7 @@ export async function createRouter( const githubClient = new Octokit({ auth: process.env.GITHUB_ACCESS_TOKEN }); const { preparers, templater, logger: parentLogger, dockerClient } = options; - const githubStorer = new GithubStorer({ client: githubClient }); + const githubPulisher = new GithubPublisher({ client: githubClient }); const logger = parentLogger.child({ plugin: 'scaffolder' }); const jobProcessor = new JobProcessor(); @@ -117,26 +118,16 @@ export async function createRouter( }, }, { - name: 'Create VCS Repo', + name: 'Publish template', handler: async (ctx: StageContext<{ resultDir: string }>) => { - ctx.logger.info('Should now create the VCS repo'); - const remoteUrl = await githubStorer.createRemote({ + ctx.logger.info('Should not store the template'); + const { remoteUrl } = await githubPulisher.publish({ values: ctx.values, - entity: ctx.entity, + directory: ctx.resultDir, }); - return { remoteUrl }; }, }, - { - name: 'Push to remote', - handler: async ( - ctx: StageContext<{ resultDir: string; remoteUrl: string }>, - ) => { - ctx.logger.info('Should now push to the remote'); - await githubStorer.pushToRemote(ctx.resultDir, ctx.remoteUrl); - }, - }, ], });