Skip to content

Commit 06b52f7

Browse files
committed
flatten structure of maybeLinkGitHub with early returns
1 parent ea676b1 commit 06b52f7

File tree

1 file changed

+49
-57
lines changed

1 file changed

+49
-57
lines changed

src/deploy.ts

Lines changed: 49 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -240,67 +240,59 @@ class Deployer {
240240
if (deployTarget.create) {
241241
throw new Error("Incorrect deploy target state");
242242
}
243+
// We only support cloud builds from the root directory so this ignores this.deployOptions.config.root
244+
const isGit = existsSync(".git");
245+
if (!isGit) throw new CliError("Not at root of a git repository; cannot enable continuous deployment.");
246+
const remotes = (await promisify(exec)("git remote -v", {cwd: this.deployOptions.config.root})).stdout
247+
.split("\n")
248+
.filter((d) => d)
249+
.map((d) => d.split(/\s/g));
250+
const gitHub = remotes.find(([, url]) => url.startsWith("https://github.com/"));
251+
if (!gitHub) throw new CliError("No GitHub remote found; cannot enable continuous deployment.");
252+
// TODO: validate "Your branch is up to date" & "nothing to commit, working tree clean"
253+
254+
// TODO allow setting this from CLI?
255+
if (!deployTarget.project.build_environment_id) throw new CliError("No build environment configured.");
256+
257+
// can do cloud build
258+
// TODO: validate local/remote refs match & we can access repo
259+
if (deployTarget.project.source) return true;
260+
261+
// Interactively try to link repository
243262
if (!this.effects.isTty) return false;
244-
if (deployTarget.project.build_environment_id && deployTarget.project.source) {
245-
// can do cloud build
246-
return true;
247-
} else {
248-
// We only support cloud builds from the root directory so this ignores this.deployOptions.config.root
249-
const isGit = existsSync(".git");
250-
if (isGit) {
251-
const remotes = (await promisify(exec)("git remote -v", {cwd: this.deployOptions.config.root})).stdout
252-
.split("\n")
253-
.filter((d) => d)
254-
.map((d) => d.split(/\s/g));
255-
const gitHub = remotes.find(([, url]) => url.startsWith("https://github.com/"));
256-
// TODO: validate "Your branch is up to date" & "nothing to commit, working tree clean"
257-
if (gitHub) {
258-
const [ownerName, repoName] = formatGitUrl(gitHub[1]).split("/");
259-
// Get current branch
260-
const branch = (
261-
await promisify(exec)("git rev-parse --abbrev-ref HEAD", {cwd: this.deployOptions.config.root})
262-
).stdout;
263-
let authedRepo = await this.apiClient.getGitHubRepository(ownerName, repoName);
264-
if (!authedRepo) {
265-
// Repo is not authorized; link to auth page and poll for auth
266-
const authUrl = new URL("/auth-github", OBSERVABLE_UI_ORIGIN);
267-
authUrl.searchParams.set("owner", ownerName);
268-
authUrl.searchParams.set("repo", repoName);
269-
this.effects.clack.log.info(
270-
`Authorize Observable to access the ${bold(repoName)} repository: ${link(
271-
authUrl
272-
)}`
273-
);
274-
const spinner = this.effects.clack.spinner();
275-
spinner.start("Waiting for repository to be authorized");
276-
const pollExpiration = Date.now() + DEPLOY_POLL_MAX_MS;
277-
while (!authedRepo) {
278-
await new Promise((resolve) => setTimeout(resolve, 2000));
279-
if (Date.now() > pollExpiration) {
280-
spinner.stop("Waiting for repository to be authorized timed out.");
281-
throw new CliError("Repository authorization failed");
282-
}
283-
authedRepo = await this.apiClient.getGitHubRepository(ownerName, repoName);
284-
if (authedRepo) spinner.stop("Repository authorized.");
285-
}
286-
}
287-
await this.apiClient.postProjectEnvironment(deployTarget.project.id, {
288-
source: {
289-
provider: authedRepo.provider,
290-
provider_id: authedRepo.provider_id,
291-
url: authedRepo.url,
292-
branch
293-
}
294-
});
295-
return true;
296-
} else {
297-
this.effects.clack.log.error("No GitHub remote found; cannot enable continuous deployment.");
263+
const [ownerName, repoName] = formatGitUrl(gitHub[1]).split("/");
264+
// Get current branch
265+
const branch = (await promisify(exec)("git rev-parse --abbrev-ref HEAD", {cwd: this.deployOptions.config.root}))
266+
.stdout;
267+
let authedRepo = await this.apiClient.getGitHubRepository(ownerName, repoName);
268+
if (!authedRepo) {
269+
// Repo is not authorized; link to auth page and poll for auth
270+
const authUrl = new URL("/auth-github", OBSERVABLE_UI_ORIGIN);
271+
authUrl.searchParams.set("owner", ownerName);
272+
authUrl.searchParams.set("repo", repoName);
273+
this.effects.clack.log.info(`Authorize Observable to access the ${bold(repoName)} repository: ${link(authUrl)}`);
274+
const spinner = this.effects.clack.spinner();
275+
spinner.start("Waiting for repository to be authorized");
276+
const pollExpiration = Date.now() + DEPLOY_POLL_MAX_MS;
277+
while (!authedRepo) {
278+
await new Promise((resolve) => setTimeout(resolve, 2000));
279+
if (Date.now() > pollExpiration) {
280+
spinner.stop("Waiting for repository to be authorized timed out.");
281+
throw new CliError("Repository authorization failed");
298282
}
299-
} else {
300-
this.effects.clack.log.error("Not at root of a git repository; cannot enable continuous deployment.");
283+
authedRepo = await this.apiClient.getGitHubRepository(ownerName, repoName);
284+
if (authedRepo) spinner.stop("Repository authorized.");
301285
}
302286
}
303-
return false;
287+
const response = await this.apiClient.postProjectEnvironment(deployTarget.project.id, {
288+
source: {
289+
provider: authedRepo.provider,
290+
provider_id: authedRepo.provider_id,
291+
url: authedRepo.url,
292+
branch
293+
}
294+
});
295+
return !!response;
304296
}
305297

306298
private async startNewDeploy(): Promise<GetDeployResponse> {

0 commit comments

Comments
 (0)