feat(scaffolder): finally found a better name than storer. It's publisher

This commit is contained in:
blam
2020-07-01 10:10:04 +02:00
parent ad4dc42bbb
commit 2438aaa099
7 changed files with 84 additions and 33 deletions
@@ -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';
@@ -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';
@@ -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', () => {});
@@ -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<string, JsonValue>;
}) {
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<void> {
private async pushToRemote(directory: string, remote: string): Promise<void> {
const repo = await Repository.init(directory, 0);
const index = await repo.refreshIndex();
await index.addAll();
@@ -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';
@@ -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<string, JsonValue>}): Promise<string>;
pushToRemote(directory: string, remote: string): Promise<void>;
}
export type Publisher = {
publish(opts: {
entity: TemplateEntityV1alpha1;
values: RequiredTemplateValues & Record<string, JsonValue>;
directory: string;
}): Promise<{ remoteUrl: string }>;
};
@@ -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);
},
},
],
});