Skip to content

Commit

Permalink
Strip password from Git repository URL. Closes #32
Browse files Browse the repository at this point in the history
  • Loading branch information
rpalcolea committed Oct 2, 2019
1 parent 5670e86 commit fbab5a7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.iml
.gradle/
build/
out/
21 changes: 20 additions & 1 deletion src/main/groovy/nebula/plugin/info/scm/GitScmProvider.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.lib.RepositoryBuilder
import org.gradle.api.Project
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class GitScmProvider extends AbstractScmProvider {
private Logger logger = LoggerFactory.getLogger(GitScmProvider)

@Override
boolean supports(Project project) {
Expand All @@ -38,7 +41,11 @@ class GitScmProvider extends AbstractScmProvider {
String calculateModuleOrigin(File projectDir) {
Repository repository = getRepository(projectDir)
Config storedConfig = repository.getConfig()
return storedConfig.getString('remote', 'origin', 'url')
String url = storedConfig.getString('remote', 'origin', 'url')
if (url?.startsWith("https://") || url?.startsWith("http://")) {
url = hideSensitiveInformation(url)
}
return url
}

@Override
Expand Down Expand Up @@ -66,4 +73,16 @@ class GitScmProvider extends AbstractScmProvider {
String calculateBranch(File projectDir) {
return getRepository(projectDir).branch
}

private String hideSensitiveInformation(String url) {
try {
String credentials = url.toURL().getUserInfo()
if (credentials) {
return url.replaceFirst(credentials, credentials.replaceFirst(/:.*/, ""))
}
} catch (Exception e) {
logger.warn("Unable to remove credentials from repository URL. {0}", e.getMessage())
}
return url
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import nebula.test.ProjectSpec
import org.eclipse.jgit.api.Git
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Issue

class GitScmProviderLocalSpec extends ProjectSpec {
@Rule TemporaryFolder temp
Expand Down Expand Up @@ -57,6 +58,39 @@ class GitScmProviderLocalSpec extends ProjectSpec {
branch == 'master'
}

@Issue("32")
def 'Strip password from Git repository URL'() {
setup:
def projectDir = temp.newFolder()
def repoUrl = 'https://github-token-user:my-token@github.com/Netflix/gradle-template.git'

Git.cloneRepository()
.setURI(repoUrl)
.setDirectory(projectDir)
.call();

def fakeProjectDir = new File(projectDir, 'gradle/wrapper')
fakeProjectDir.mkdirs()

when:
String mapped = provider.calculateModuleSource(fakeProjectDir)

then:
mapped == '/gradle/wrapper'

when:
String origin = provider.calculateModuleOrigin(fakeProjectDir)

then:
origin == 'https://github-token-user@github.com/Netflix/gradle-template.git'

when:
String branch = provider.calculateBranch(fakeProjectDir)

then:
branch == 'master'
}

def 'no module origin'() {
setup:
def projectDir = temp.newFolder()
Expand Down

0 comments on commit fbab5a7

Please sign in to comment.