Skip to content

Instantly share code, notes, and snippets.

@miguelangelgonzalez
Forked from miguelgonzalezLF/mingle.groovy
Last active January 21, 2016 15:24
Show Gist options
  • Save miguelangelgonzalez/801d35b8f300a076879b to your computer and use it in GitHub Desktop.
Save miguelangelgonzalez/801d35b8f300a076879b to your computer and use it in GitHub Desktop.
Mingle integration for Gitblit using a groovy script
/*
* Copyright 2011 gitblit.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.gitblit.GitBlit
import com.gitblit.Keys
import com.gitblit.models.RepositoryModel
import com.gitblit.models.UserModel
import com.gitblit.utils.JGitUtils
import java.text.SimpleDateFormat
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.lib.Config
import org.eclipse.jgit.revwalk.RevCommit
import org.eclipse.jgit.transport.ReceiveCommand
import org.eclipse.jgit.transport.ReceiveCommand.Result
import org.slf4j.Logger
import com.gitblit.utils.Base64;
/**
* Sample Gitblit Post-Receive Hook: jenkins
*
* The Post-Receive hook is executed AFTER the pushed commits have been applied
* to the Git repository. This is the appropriate point to trigger an
* integration build or to send a notification.
*
* This script is only executed when pushing to *Gitblit*, not to other Git
* tooling you may be using.
*
* If this script is specified in *groovy.postReceiveScripts* of gitblit.properties
* or web.xml then it will be executed by any repository when it receives a
* push. If you choose to share your script then you may have to consider
* tailoring control-flow based on repository access restrictions.
*
* Scripts may also be specified per-repository in the repository settings page.
* Shared scripts will be excluded from this list of available scripts.
*
* This script is dynamically reloaded and it is executed within it's own
* exception handler so it will not crash another script nor crash Gitblit.
*
* Bound Variables:
* gitblit Gitblit Server com.gitblit.GitBlit
* repository Gitblit Repository com.gitblit.models.RepositoryModel
* receivePack JGit Receive Pack org.eclipse.jgit.transport.ReceivePack
* user Gitblit User com.gitblit.models.UserModel
* commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>
* url Base url for Gitblit String
* logger Logs messages to Gitblit org.slf4j.Logger
* clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger
*
* Accessing Gitblit Custom Fields:
* def myCustomField = repository.customFields.myCustomField
*
*/
// Indicate we have started the script
logger.info("mingle hook triggered by ${user.username} for ${repository.name}")
Repository r = gitblit.getRepository(repository.name)
// define the summary and commit urls
def repo = repository.name
def summaryUrl
def commitUr
if (gitblit.getBoolean(Keys.web.mountParameters, true)) {
repo = repo.replace('/', gitblit.getString(Keys.web.forwardSlashCharacter, '/')).replace('/', '%2F')
summaryUrl = url + "/summary/$repo"
commitUrl = url + "/commit/$repo/"
} else {
summaryUrl = url + "/summary?r=$repo"
commitUrl = url + "/commit?r=$repo&h="
}
SimpleDateFormat df = new SimpleDateFormat(gitblit.getString(Keys.web.datetimestampLongFormat, 'EEEE, MMMM d, yyyy h:mm a z'))
def table = { "\n ${JGitUtils.getDisplayName(it.authorIdent)}\n ${df.format(JGitUtils.getCommitDate(it))}\n\n $it.shortMessage\n\n $commitUrl$it.id.name" }
def SendToMingle(commitMessage){
def m = commitMessage =~ /(?:#|card)\d+/
if(m){
def mingleUrl = gitblit.getString('groovy.mingleServer', 'http://mingle.leapfactor.net:8080')
def mingleUser = gitblit.getString('groovy.mingleUser', 'git')
def minglePass = gitblit.getString('groovy.minglePass', 'Leap911')
def triggerUrl = "${mingleUrl}/api/v2/projects/${repository.customFields.mingleProject}/murmurs.xml"
def authString = mingleUser + ":" + minglePass;
byte[] authEncBytes = Base64.encodeBytes(authString.getBytes());
String authStringEnc = new String(authEncBytes);
def query = URLEncoder.encode("murmur[body]") + "=" + URLEncoder.encode(commitMessage, 'UTF8')
def url = new URL(triggerUrl)
def connection = url.openConnection()
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestMethod("POST")
connection.setRequestProperty('Content-Type', 'application/x-www-form-urlencoded')
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
wr.writeBytes (query);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
}
}
for(command in commands) {
def ref = command.refName
def refType = 'branch'
if (ref.startsWith('refs/heads/')) {
ref = command.refName.substring('refs/heads/'.length())
} else if (ref.startsWith('refs/tags/')) {
ref = command.refName.substring('refs/tags/'.length())
refType = 'tag'
}
switch (command.type) {
case ReceiveCommand.Type.UPDATE:
case ReceiveCommand.Type.UPDATE_NONFASTFORWARD:
def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name).reverse()
for(msg in commits.collect(table)){
SendToMingle(msg)
}
break
}
}
r.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment