-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add --remote-repo-url to "iac test"
- Loading branch information
1 parent
057c554
commit 2a12048
Showing
18 changed files
with
451 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { IacOutputMeta } from '../../../../lib/types'; | ||
import { IacOrgSettings } from './local-execution/types'; | ||
import * as pathLib from 'path'; | ||
|
||
export interface GitRepository { | ||
readonly path: string; | ||
|
||
readRemoteUrl(): Promise<string | undefined>; | ||
} | ||
|
||
export interface GitRepositoryFinder { | ||
findRepositoryForPath(path: string): Promise<GitRepository | undefined>; | ||
} | ||
|
||
export async function buildMeta( | ||
repositoryFinder: GitRepositoryFinder, | ||
orgSettings: IacOrgSettings, | ||
projectRoot: string, | ||
remoteRepoUrl?: string, | ||
): Promise<IacOutputMeta> { | ||
const gitRemoteUrl = await getGitRemoteUrl( | ||
repositoryFinder, | ||
projectRoot, | ||
remoteRepoUrl, | ||
); | ||
const projectName = getProjectName(projectRoot, gitRemoteUrl); | ||
const orgName = getOrgName(orgSettings); | ||
return { projectName, orgName, gitRemoteUrl }; | ||
} | ||
|
||
function getProjectName(projectRoot: string, gitRemoteUrl?: string): string { | ||
if (gitRemoteUrl) { | ||
return getProjectNameFromGitUrl(gitRemoteUrl); | ||
} | ||
|
||
return pathLib.basename(pathLib.resolve(projectRoot)); | ||
} | ||
|
||
function getOrgName(orgSettings: IacOrgSettings): string { | ||
return orgSettings.meta.org; | ||
} | ||
|
||
async function getGitRemoteUrl( | ||
repositoryFinder: GitRepositoryFinder, | ||
projectRoot: string, | ||
remoteRepoUrl?: string, | ||
): Promise<string | undefined> { | ||
if (remoteRepoUrl) { | ||
return remoteRepoUrl; | ||
} | ||
|
||
const repository = await repositoryFinder.findRepositoryForPath(projectRoot); | ||
|
||
if (!repository) { | ||
return; | ||
} | ||
|
||
const resolvedRepositoryRoot = pathLib.resolve(repository.path); | ||
const resolvedProjectRoot = pathLib.resolve(projectRoot); | ||
|
||
if (resolvedRepositoryRoot != resolvedProjectRoot) { | ||
return; | ||
} | ||
|
||
return await repository.readRemoteUrl(); | ||
} | ||
|
||
export function getProjectNameFromGitUrl(url: string) { | ||
const regexps = [ | ||
/^ssh:\/\/([^@]+@)?[^:/]+(:[^/]+)?\/(?<name>.*).git\/?$/, | ||
/^(git|https?|ftp):\/\/[^:/]+(:[^/]+)?\/(?<name>.*).git\/?$/, | ||
/^[^@]+@[^:]+:(?<name>.*).git$/, | ||
]; | ||
|
||
const trimmed = url.trim(); | ||
|
||
for (const regexp of regexps) { | ||
const match = trimmed.match(regexp); | ||
|
||
if (match && match.groups) { | ||
return match.groups.name; | ||
} | ||
} | ||
|
||
return trimmed; | ||
} |
Oops, something went wrong.