Skip to content

Commit

Permalink
re-add the matching connections logic and address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcnellis committed Sep 27, 2024
1 parent abb05ec commit f726078
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
41 changes: 24 additions & 17 deletions src/apphosting/githubConnections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const MANAGE_INSTALLATION_CHOICE = "@MANAGE_INSTALLATION";
/**
* Prompts the user to create a GitHub connection.
*/
export async function createGitHubConnection(
export async function getOrCreateGithubConnection(
projectId: string,
location: string,
connectionId: string | null,
Expand All @@ -102,18 +102,12 @@ export async function createGitHubConnection(
if (connectionId) {
// Check if the connection already exists.
try {
utils.logBullet(
clc.bold(`${clc.yellow("===")} Check if connection ${connectionId} already exists`),
);

const connection = await devConnect.getConnection(projectId, location, connectionId);
utils.logBullet(`Reusing existing connection ${connectionId}`);
return connection;
} catch (e) {
// Expected, the connection doesn't exist.
}
} else {
connectionId = generateConnectionId();
}

// Fetch the sentinel Oauth connection first which is needed to create further GitHub connections.
Expand All @@ -136,6 +130,23 @@ export async function createGitHubConnection(
installationId = await promptGitHubInstallation(projectId, location, oauthConn);
}

const connectionMatchingInstallation = await getConnectionForInstallation(
projectId,
location,
installationId,
);
if (connectionMatchingInstallation) {
const { id: matchingConnectionId } = parseConnectionName(connectionMatchingInstallation.name)!;

Check warning on line 139 in src/apphosting/githubConnections.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion

if (!connectionId || matchingConnectionId === connectionId) {
utils.logBullet(`Reusing matching connection ${matchingConnectionId}`);
return connectionMatchingInstallation;
}
}
if (!connectionId) {
connectionId = generateConnectionId();
}

const connection = await createFullyInstalledConnection(
projectId,
location,
Expand All @@ -155,26 +166,22 @@ export async function linkGitHubRepository(
location: string,
createConnectionId: string | null,
): Promise<devConnect.GitRepositoryLink> {
const connectionMatchingInstallation = await createGitHubConnection(
projectId,
location,
createConnectionId,
);
const connection = await getOrCreateGithubConnection(projectId, location, createConnectionId);

let repoCloneUri: string | undefined;

do {
if (repoCloneUri === MANAGE_INSTALLATION_CHOICE) {
await manageInstallation(connectionMatchingInstallation);
await manageInstallation(connection);
}

repoCloneUri = await promptCloneUri(projectId, connectionMatchingInstallation);
repoCloneUri = await promptCloneUri(projectId, connection);
} while (repoCloneUri === MANAGE_INSTALLATION_CHOICE);

const { id: connectionId } = parseConnectionName(connectionMatchingInstallation.name)!;
const { id: connectionId } = parseConnectionName(connection.name)!;

Check warning on line 181 in src/apphosting/githubConnections.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion
await getOrCreateConnection(projectId, location, connectionId, {
authorizerCredential: connectionMatchingInstallation.githubConfig?.authorizerCredential,
appInstallationId: connectionMatchingInstallation.githubConfig?.appInstallationId,
authorizerCredential: connection.githubConfig?.authorizerCredential,
appInstallationId: connection.githubConfig?.appInstallationId,
});

const repo = await getOrCreateRepository(projectId, location, connectionId, repoCloneUri);
Expand Down
6 changes: 1 addition & 5 deletions src/commands/apphosting-repos-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import { requireTosAcceptance } from "../requireTosAcceptance";
export const command = new Command("apphosting:repos:create")
.description("create a Firebase App Hosting Developer Connect Git Repository Link")
.option("-l, --location <location>", "specify the location of the backend", "")
.option(
"-g, --gitconnection <connection>",
"id of the connection (defaults to autogenerating a random id)",
"",
)
.option("-g, --gitconnection <connection>", "id of the connection", "")
.before(ensureApiEnabled)
.before(requireInteractive)
.before(requireTosAcceptance(APPHOSTING_TOS_ID))
Expand Down

0 comments on commit f726078

Please sign in to comment.