Skip to content

Instantly share code, notes, and snippets.

@gervaisb
Created March 7, 2016 15:49
Show Gist options
  • Save gervaisb/206a3441b2ed454e81d5 to your computer and use it in GitHub Desktop.
Save gervaisb/206a3441b2ed454e81d5 to your computer and use it in GitHub Desktop.
/*
A WebHook pipe who receive a Codeship notification when a build start or
change and update the related status on Bitbucket.
This script need 3 script properties :
+ BITBUCKET_REPO The repository that will be updated
+ BITBUCKET_OWNER Username of `BITBUCKET_REPO` owner
+ BITBUCKET_PASSWORD /!\ Plain password of the owner. /!\
Codeship has more statuses than Bitbucket, a simple conversion is applied.
This can be changed in function 'convert' at line 102.
+ "testing" and "waiting" resolves to "INPROGRESS";
+ "success" resolves to "SUCCESSFUL";
+ "infrastructure_failure", "blocked", "stopped" and "error" gives "FAILED";
+ "ignored" is ignored (do nothing)
@See Codeship WebHooks https://codeship.com/documentation/integrations/webhooks/
@See Bitbucket API, Build-sattus resource : https://confluence.atlassian.com/bitbucket/statuses-build-resource-779295267.html
*/
var Logger = BetterLog.useSpreadsheet(__ENTER_YOUR_LOGGING_SHEET_KEY_HERE__);
var props = PropertiesService.getScriptProperties();
var owner = props.getProperty('BITBUCKET_OWNER');// The account of the repository owner.
var repo = props.getProperty('BITBUCKET_REPO'); // The account of the repository owner.
var basic = Utilities.base64EncodeWebSafe(owner+':'+props.getProperty('BITBUCKET_PASSWORD'));
function doPost(request) {
var build = JSON.parse(request.postData.contents).build;
var response;
Logger.finest("Received build status from codeship : %s", build);
if (ignore(build)) {
Logger.log("Build #%s is ignored", build.build_id);
response = {
"message": "build is ignored"
}
} else if ( isNew(build) ) {
response = post(build.commit_id, {
"state": convert(build.status),
"key": "CODESHIP_"+build.project_id+"_"+build.build_id,
"url": build.build_url,
"description": build.message+" by "+build.committer
});
} else {
response = put(build.commit_id, "CODESHIP_"+build.project_id+"_"+build.build_id, {
"state": convert(build.status),
"url": build.build_url,
"description": build.message+" by "+build.committer
});
}
var message = {
"input": build,
"output": {
"status": response.getResponseCode(),
"content": response.getContentText()
}
}
return ContentService.createTextOutput(JSON.stringify(message))
.setMimeType(ContentService.MimeType.JSON);
}
function ignore(build) {
return "ignored" === build.status;
}
function isNew(build) {
return "testing" === build.status;
}
function post(revision, status) {
var options = {
"method": "post",
"headers": {
'Authorization' : "Basic "+basic,
'Content-Type' : 'application/json'
},
"muteHttpExceptions": true,
"payload": JSON.stringify(status)
}
var path = "repositories/"+owner+"/"+repo+"/commit/"+revision+"/statuses/build";
Logger.log("POST %s:***@%s : %s ", owner, path, JSON.stringify(status));
return UrlFetchApp.fetch("https://api.bitbucket.org/2.0/"+path, options);
}
function put(revision, key, status) {
var options = {
"method": "put",
"headers": {
'Authorization' : "Basic "+basic,
'Content-Type' : 'application/json'
},
"muteHttpExceptions": true,
"payload": JSON.stringify(status)
}
var path = "repositories/"+owner+"/"+repo+"/commit/"+revision+"/statuses/build/"+key;
Logger.log("PUT %s:***@%s : %s ", owner, path, JSON.stringify(status));
return UrlFetchApp.fetch("https://api.bitbucket.org/2.0/"+path, options);
}
function convert(status) {
switch(status) {
case "testing":
case "waiting":
return "INPROGRESS";
case "success":
return "SUCCESSFUL";
case "infrastructure_failure":
case "blocked":
case "stopped":
case "error":
return "FAILED";
case "ignored":
return "!";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment