Skip to content

Instantly share code, notes, and snippets.

@sparsick
Created October 17, 2018 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sparsick/d7239f2283b7a7e68f397b15093f9492 to your computer and use it in GitHub Desktop.
Save sparsick/d7239f2283b7a7e68f397b15093f9492 to your computer and use it in GitHub Desktop.
#!/usr/bin/env groovy
import groovy.transform.Field
@Grab('org.eclipse.jgit:org.eclipse.jgit:5.1.2.201810061102-r')
@Grab('io.github.http-builder-ng:http-builder-ng-core:1.0.3')
@Grab('io.github.cdimascio:java-dotenv:3.1.2')
import groovyx.net.http.HttpBuilder
import io.github.cdimascio.dotenv.Dotenv
import jgit.*
import org.eclipse.jgit.api.CreateBranchCommand
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.ListBranchCommand
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider
import java.nio.file.Files
@Field
def dotenv = Dotenv.load();
// ======
def scmUsername = dotenv.get('SCM_USERNAME')
def scmPassword = dotenv.get('SCM_PASSWORD')
def intellijHome = dotenv.get('INTELLIJ_HOME')
def codeFormatterSetting = dotenv.get("CODE_FORMATTER_SETTING")
def usernamePasswordCredentialsProvider = new UsernamePasswordCredentialsProvider(scmUsername, scmPassword)
def http = HttpBuilder.configure {
request.uri = "http://scm-manager-example.io"
}
http.post {
request.uri.path = '/scm/api/rest/authentication/login'
request.body = [username: scmUsername, password: scmPassword]
request.contentType = 'application/x-www-form-urlencoded'
response.failure {
throw new RuntimeException("Authentication failed.")
}
}
def result = http.get {
request.uri.path = '/scm/api/rest/repositories'
request.accept = 'application/json'
}
def allRepositoriesUrls = result.collect { it.url }
http.get {
request.uri.path = '/scm/api/rest/authentication/logout'
request.accept = 'application/json'
}
allRepositoriesUrls.each { repository ->
def repositoryName = repository.split('/').flatten().findAll { it != null }.last()
File localPath = Files.createTempDirectory("${repositoryName}-").toFile()
println "Clone ${repository} to ${localPath}"
Git.cloneRepository()
.setURI(repository)
.setDirectory(localPath)
.setNoCheckout(true)
.setCredentialsProvider(usernamePasswordCredentialsProvider)
.call()
.withCloseable { git ->
def remoteBranches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE).call()
def remoteBranchNames = remoteBranches.collect { it.name.replace('refs/remotes/origin/', '') }
println "Found the following branches: ${remoteBranchNames}"
remoteBranchNames.each { remoteBranch ->
println "Checkout branch $remoteBranch"
git.checkout()
.setName(remoteBranch)
.setCreateBranch(true)
.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
.setStartPoint("origin/" + remoteBranch)
.call()
def formatCommand = "$intellijHome/bin/format.sh -r -s $codeFormatterSetting $localPath"
println formatCommand.execute().text
git.add()
.addFilepattern('.')
.call()
git.commit()
.setAuthor("Automater", "noemail")
.setMessage('Format code according to IntelliJ setting.')
.call()
println "Commit successful!"
}
git.push()
.setCredentialsProvider(usernamePasswordCredentialsProvider)
.setPushAll()
.call()
println "Push is done"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment