chore: reworking more of the github flow so that we can move to isomorphic git instead of nodegit :()

This commit is contained in:
blam
2020-12-01 21:07:20 +01:00
parent f393f300e5
commit a5414403bd
3 changed files with 42 additions and 40 deletions
@@ -61,7 +61,7 @@ export class GithubPreparer implements PreparerBase {
try {
await git.clone({
fs: require('fs'),
fs,
http,
url: repositoryCheckoutUrl,
dir: checkoutLocation,
@@ -16,7 +16,7 @@
import { PublisherBase } from './types';
import { Octokit } from '@octokit/rest';
import { pushToRemoteUserPass } from './helpers';
import { pushToRemoteCred } from './helpers';
import { JsonValue } from '@backstage/config';
import { RequiredTemplateValues } from '../templater';
@@ -51,12 +51,10 @@ export class GithubPublisher implements PublisherBase {
directory: string;
}): Promise<{ remoteUrl: string }> {
const remoteUrl = await this.createRemote(values);
await pushToRemoteUserPass(
directory,
remoteUrl,
this.token,
'x-oauth-basic',
);
await pushToRemoteCred(directory, remoteUrl, {
username: this.token,
password: 'x-auth-basic',
});
return { remoteUrl };
}
@@ -13,43 +13,47 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Repository, Remote, Signature, Cred } from 'nodegit';
import git from 'isomorphic-git';
import globby from 'globby';
import fs from 'fs';
import http from 'isomorphic-git/http/node';
export async function pushToRemoteCred(
directory: string,
dir: string,
remote: string,
credentialsProvider: () => Cred,
auth?: { username: string; password: string },
): Promise<void> {
const repo = await Repository.init(directory, 0);
const index = await repo.refreshIndex();
await index.addAll();
await index.write();
const oid = await index.writeTree();
await repo.createCommit(
'HEAD',
Signature.now('Scaffolder', 'scaffolder@backstage.io'),
Signature.now('Scaffolder', 'scaffolder@backstage.io'),
'initial commit',
oid,
[],
);
await git.init({
fs,
dir,
});
const remoteRepo = await Remote.create(repo, 'origin', remote);
const paths = await globby(['./**', './**/.*'], { gitignore: true });
for (const filepath of paths) {
await git.add({ fs, dir, filepath });
}
await remoteRepo.push(['refs/heads/master:refs/heads/master'], {
callbacks: {
credentials: credentialsProvider,
},
await git.commit({
fs,
dir,
message: 'Initial commit',
author: { name: 'Scaffolder', email: 'scaffolder@backstage.io' },
committer: { name: 'Scaffolder', email: 'scaffolder@backstage.io' },
});
await git.addRemote({
fs,
dir,
remote: 'origin',
url: remote,
});
await git.push({
fs,
dir,
http,
remote: 'origin',
onAuth: () => auth,
});
}
export async function pushToRemoteUserPass(
directory: string,
remote: string,
username: string,
password: string,
): Promise<void> {
return pushToRemoteCred(directory, remote, () =>
Cred.userpassPlaintextNew(username, password),
);
}