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

[utils] Format all git domains, not only github.com #1058

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions src/commands/git-metadata/library.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {newApiKeyValidator} from '../../helpers/apikey'
import {RequestBuilder} from '../../helpers/interfaces'
import {upload, UploadOptions, UploadStatus} from '../../helpers/upload'
import {getRequestBuilder, filterAndFormatGithubRemote} from '../../helpers/utils'
import {getRequestBuilder, filterAndFormatGitRemote} from '../../helpers/utils'
import {version} from '../../helpers/version'

import {getCommitInfo, newSimpleGit} from './git'
Expand All @@ -26,9 +26,9 @@ export const getGitCommitInfo = async (filterAndFormatGitRepoUrl = true): Promis
const simpleGit = await newSimpleGit()
const payload = await getCommitInfo(simpleGit)

const gitRemote = filterAndFormatGitRepoUrl ? filterAndFormatGithubRemote(payload.remote) : payload.remote
const gitRemote = filterAndFormatGitRepoUrl ? filterAndFormatGitRemote(payload.remote) : payload.remote

// gitRemote will never be undefined, as filterAndFormatGithubRemote will ONLY return undefined if it's
// gitRemote will never be undefined, as filterAndFormatGitRemote will ONLY return undefined if it's
// parameter value is also undefined. Added the " gitRemote ?? '' " to make the typechecker happy.
return [gitRemote ?? '', payload.hash]
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/lambda/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Cli, Command, Option} from 'clipanion'
import {ENVIRONMENT_ENV_VAR, SERVICE_ENV_VAR, VERSION_ENV_VAR} from '../../constants'
import {requestConfirmation} from '../../helpers/prompt'
import * as helperRenderer from '../../helpers/renderer'
import {resolveConfigFromFile, filterAndFormatGithubRemote, DEFAULT_CONFIG_PATHS} from '../../helpers/utils'
import {resolveConfigFromFile, filterAndFormatGitRemote, DEFAULT_CONFIG_PATHS} from '../../helpers/utils'

import {getCommitInfo, newSimpleGit} from '../git-metadata/git'
import {UploadCommand} from '../git-metadata/upload'
Expand Down Expand Up @@ -377,7 +377,7 @@ export class InstrumentCommand extends Command {
throw Error('Local changes have not been pushed remotely. Aborting git data tagging.')
}

const gitRemote = filterAndFormatGithubRemote(currentStatus.remote)
const gitRemote = filterAndFormatGitRemote(currentStatus.remote)

return {commitSha: currentStatus.hash, gitRemote}
}
Expand Down
19 changes: 19 additions & 0 deletions src/helpers/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,25 @@ describe('utils', () => {
})
})

describe('filterAndFormatGitRemote', () => {
test('git remotes get formatted correctly', async () => {
expect(ciUtils.filterAndFormatGitRemote('https:/datadog/test.git')).toEqual(
'github.com/datadog/test.git'
)
expect(ciUtils.filterAndFormatGitRemote('[email protected]:datadog/test.git')).toEqual('github.com/datadog/test.git')
expect(ciUtils.filterAndFormatGitRemote('github.com/datadog/test.git')).toEqual('github.com/datadog/test.git')
expect(ciUtils.filterAndFormatGitRemote('https://git.some.domain.com:8080/datadog/test.git')).toEqual(
'git.some.domain.com/datadog/test.git'
)
expect(ciUtils.filterAndFormatGitRemote('[email protected]:datadog/test.git')).toEqual(
'test.domain.com/datadog/test.git'
)
expect(ciUtils.filterAndFormatGitRemote('example.domain.com/datadog/test.git')).toEqual(
'example.domain.com/datadog/test.git'
)
})
})

describe('filterAndFormatGithubRemote', () => {
test('git remotes get formatted correctly', async () => {
expect(ciUtils.filterAndFormatGithubRemote('https:/datadog/test.git')).toEqual(
Expand Down
9 changes: 7 additions & 2 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,21 @@ export const filterSensitiveInfoFromRepository = (repositoryUrl: string | undefi

// Removes sensitive info from the given git remote url and normalizes the url prefix.
// "[email protected]:" and "https:/" prefixes will be normalized into "github.com/"
export const filterAndFormatGithubRemote = (rawRemote: string | undefined): string | undefined => {
export const filterAndFormatGitRemote = (rawRemote: string | undefined): string | undefined => {
rawRemote = filterSensitiveInfoFromRepository(rawRemote)
if (!rawRemote) {
return rawRemote
}
rawRemote = rawRemote.replace(/git@github\.com:|https:\/\/github\.com\//, 'github.com/')
rawRemote = rawRemote.replace(/(git@|https:\/\/)([^/^:]+)(:([\d]+))?(:|\/)/, '$2/')

return rawRemote
}

/**
* @deprecated Use filterAndFormatGitRemote instead
*/
export const filterAndFormatGithubRemote = filterAndFormatGitRemote

export const timedExecAsync = async <I, O>(f: (input: I) => Promise<O>, input: I): Promise<number> => {
const initialTime = Date.now()
await f(input)
Expand Down