Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow GCB repos to be reused in backend. #6579

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/init/features/frameworks/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
const FRAMEWORKS_CONN_PATTERN = /.+\/frameworks-github-conn-.+$/;
const FRAMEWORKS_OAUTH_CONN_NAME = "frameworks-github-oauth";
const CONNECTION_NAME_REGEX =
/^projects\/(?<projectId>[^\/]+)\/locations\/(?<location>[^\/]+)\/connections\/(?<id>[^\/]+)$/;

Check warning on line 20 in src/init/features/frameworks/repo.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

Check warning on line 20 in src/init/features/frameworks/repo.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

Check warning on line 20 in src/init/features/frameworks/repo.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

/**
* Exported for unit testing.
*/
export function parseConnectionName(name: string): ConnectionNameParts | undefined {
const match = name.match(CONNECTION_NAME_REGEX);

Check warning on line 26 in src/init/features/frameworks/repo.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Use the `RegExp#exec()` method instead

if (!match || typeof match.groups === undefined) {
return;
Expand Down Expand Up @@ -112,7 +112,7 @@
}

// Ensure that the selected connection exists in the same region as the backend
const { id: connectionId } = parseConnectionName(connection.name)!;

Check warning on line 115 in src/init/features/frameworks/repo.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion
await getOrCreateConnection(projectId, location, connectionId, {
authorizerCredential: connection.githubConfig?.authorizerCredential,
appInstallationId: connection.githubConfig?.appInstallationId,
Expand All @@ -130,7 +130,7 @@
): Promise<{ remoteUri: string; connection: gcb.Connection }> {
const remoteUriToConnection: Record<string, gcb.Connection> = {};
for (const conn of connections) {
const { id } = parseConnectionName(conn.name)!;

Check warning on line 133 in src/init/features/frameworks/repo.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion
const resp = await gcb.fetchLinkableRepositories(projectId, location, id);
if (resp.repositories && resp.repositories.length > 1) {
for (const repo of resp.repositories) {
Expand All @@ -148,12 +148,12 @@
value: "",
});

const remoteUri = await promptOnce({

Check warning on line 151 in src/init/features/frameworks/repo.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
type: "list",
message: "Which of the following repositories would you like to deploy?",
choices,
});
return { remoteUri, connection: remoteUriToConnection[remoteUri] };

Check warning on line 156 in src/init/features/frameworks/repo.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

Check warning on line 156 in src/init/features/frameworks/repo.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Computed name [remoteUri] resolves to an any value
}

async function promptConnectionAuth(conn: gcb.Connection): Promise<gcb.Connection> {
Expand All @@ -169,7 +169,7 @@
message: "Press Enter once you have authorized the app",
});
cleanup();
const { projectId, location, id } = parseConnectionName(conn.name)!;

Check warning on line 172 in src/init/features/frameworks/repo.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion
return await gcb.getConnection(projectId, location, id);
}

Expand Down Expand Up @@ -240,10 +240,6 @@
let repo: gcb.Repository;
try {
repo = await gcb.getRepository(projectId, location, connectionId, repositoryId);
const repoSlug = extractRepoSlugFromUri(repo.remoteUri);
if (repoSlug) {
throw new FirebaseError(`${repoSlug} has already been linked.`);
}
} catch (err: unknown) {
if ((err as FirebaseError).status === 404) {
const op = await gcb.createRepository(
Expand Down
15 changes: 15 additions & 0 deletions src/test/init/frameworks/repo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ describe("composer", () => {
);
});

it("re-uses existing repository it already exists", async () => {
getConnectionStub.resolves(completeConn);
fetchLinkableRepositoriesStub.resolves(repos);
promptOnceStub.onFirstCall().resolves(repos.repositories[0].remoteUri);
getRepositoryStub.resolves(repos.repositories[0]);

const r = await repo.getOrCreateRepository(
projectId,
location,
connectionId,
repos.repositories[0].remoteUri
);
expect(r).to.be.deep.equal(repos.repositories[0]);
});

it("throws error if no linkable repositories are available", async () => {
getConnectionStub.resolves(pendingConn);
fetchLinkableRepositoriesStub.resolves({ repositories: [] });
Expand Down
Loading