Skip to content

Instantly share code, notes, and snippets.

@l8nites
Last active February 15, 2019 19:28
Show Gist options
  • Save l8nites/9181a12c25fe50f66120d999a2df1435 to your computer and use it in GitHub Desktop.
Save l8nites/9181a12c25fe50f66120d999a2df1435 to your computer and use it in GitHub Desktop.
List all gitlab projects where master branch is behind staging
component{
this.projList = {
'test' = [1]
}
remote any function allProjects(){
for (var i = 1; i<=200; i++){
var gitlabAPI = 'https://gitlab.example.com/api/v4/projects/#i#';
cfhttp( url = gitlabAPI, method="GET", result="local.cfhttp" ) {
cfhttpparam( type="header", name="Content-Type", value="application/json");
cfhttpparam( type="header", name="PRIVATE-TOKEN", value="[PrivateToken]");
cfhttpparam( type="url", name="simple", value="true");
};
if (local.cfhttp.status_code == 200){
var prj = deserializeJSON(local.cfhttp.filecontent);
fileWrite("projects/" & replace(prj.path_with_namespace, "/", '.', "all") & ".#i#.json", local.cfhttp.filecontent);
}
}
}
remote any function allProjectsMasterVsStaging(){
var branchPath = '/repository/branches/';
for (var i = 1; i<=200; i++){
var gitlabAPI = 'https://gitlab.example.com/api/v4/projects/#i#';
var branch = "staging";
cfhttp( url = gitlabAPI & branchPath & branch, method="GET", result="local.cfhttp" ) {
cfhttpparam( type="header", name="Content-Type", value="application/json");
cfhttpparam( type="header", name="PRIVATE-TOKEN", value="[PrivateToken]");
cfhttpparam( type="url", name="simple", value="true");
};
if (local.cfhttp.status_code == 200){
var prj = deserializeJSON(local.cfhttp.filecontent);
var stagingDate = prj.commit.committed_date;
} else {
continue;
}
var branch = "master";
cfhttp( url = gitlabAPI & branchPath & branch, method="GET", result="local.cfhttp" ) {
cfhttpparam( type="header", name="Content-Type", value="application/json");
cfhttpparam( type="header", name="PRIVATE-TOKEN", value="[PrivateToken]");
cfhttpparam( type="url", name="simple", value="true");
};
if (local.cfhttp.status_code == 200){
var prj = deserializeJSON(local.cfhttp.filecontent);
var masterDate = prj.commit.committed_date;
}
var daysDiff = dateDiff("d", masterDate, stagingDate);
if (daysDiff > 0) {
cfhttp( url = gitlabAPI, method="GET", result="local.cfhttp" ) {
cfhttpparam( type="header", name="Content-Type", value="application/json");
cfhttpparam( type="header", name="PRIVATE-TOKEN", value="[PrivateToken]");
cfhttpparam( type="url", name="simple", value="true");
};
if (local.cfhttp.status_code == 200){
var prj = deserializeJSON(local.cfhttp.filecontent);
job.start("#i#: " & replace(prj.path_with_namespace, "/", '.', "all") & " master branch is #daysDiff# days behind staging.");
job.complete();
}
}
}
}
remote any function compareMasterToStaging(projArray = 'all'){
if (arguments.projArray == 'all'){
this.projList['all'] = [];
for (var i = 1; i<=200; i++){
arrayAppend(this.projList['all'], i);
}
}
arrayEach(this.projList[projArray], function(i,index){
// GET /projects/:id/repository/compare?from=master&to=feature
var privateToken = '{{ tokenString }}';
var branchPath = '/repository/compare?';
var gitlabAPI = 'https://gitlab.example.com/api/v4/projects/#i#';
var branch = "from=master&to=staging";
cfhttp( url = gitlabAPI & branchPath & branch, method="GET", result="local.ahead" ) {
cfhttpparam( type="header", name="Content-Type", value="application/json");
cfhttpparam( type="header", name="PRIVATE-TOKEN", value="#privateToken#");
cfhttpparam( type="url", name="simple", value="true");
};
if (local.ahead.status_code == 200){
// fileWrite("releases/#i#.test.ahead.json", local.ahead.filecontent);
var aheadRes = deserializeJSON(local.ahead.filecontent);
} else {
job.start("#i#: #branch# - #local.ahead.status_code#");
job.complete();
continue;
}
var branch = "from=staging&to=master";
cfhttp( url = gitlabAPI & branchPath & branch, method="GET", result="local.behind" ) {
cfhttpparam( type="header", name="Content-Type", value="application/json");
cfhttpparam( type="header", name="PRIVATE-TOKEN", value="#privateToken#");
cfhttpparam( type="url", name="simple", value="true");
};
if (local.behind.status_code == 200){
// fileWrite("releases/#i#.test.behind.json", local.behind.filecontent);
var behindRes = deserializeJSON(local.behind.filecontent);
} else {
job.start("#i#: #branch# - #local.behind.status_code#");
job.complete();
continue;
}
if ( arrayLen(aheadRes.commits) || arrayLen(aheadRes.diffs) || arrayLen(behindRes.commits) || arrayLen(behindRes.diffs) ) {
cfhttp( url = gitlabAPI, method="GET", result="local.project" ) {
cfhttpparam( type="header", name="Content-Type", value="application/json");
cfhttpparam( type="header", name="PRIVATE-TOKEN", value="#privateToken#");
cfhttpparam( type="url", name="simple", value="true");
};
if (local.project.status_code == 200){
var prj = deserializeJSON(local.project.filecontent);
if ( arrayLen(aheadRes.diffs) ) {
job.start("#i#: " & replace(prj.path_with_namespace, "/", '.', "all") & " staging is ahead by #arrayLen(aheadRes.commits)# commits in #arrayLen(aheadRes.diffs)# files. #prj.web_url#");
fileWrite("compare/#i#.#replace(prj.path_with_namespace, "/", '.', "all")#.ahead.json", local.ahead.filecontent);
job.complete();
}
if ( arrayLen(behindRes.diffs) ) {
job.start("#i#: " & replace(prj.path_with_namespace, "/", '.', "all") & " staging is behind by #arrayLen(behindRes.commits)# commits in #arrayLen(behindRes.diffs)# files. #prj.web_url#");
fileWrite("compare/#i#.#replace(prj.path_with_namespace, "/", '.', "all")#.behind.json", local.behind.filecontent);
job.complete();
}
}
}
});
}
remote any function createTag(projArray = 'all', tagName = '', tagDescr = ''){
if ( tagName == '' ){
print.redline("tagName value required.");
return;
}
if (isNumeric(projArray)){
this.projList['single'] = [projArray];
projArray = 'single';
}
arrayEach(this.projList[projArray], function(i,index){
var privateToken = '{{ Private Token }}';
var gitlabAPI = 'https://gitlab.impelos.com/api/v4/projects/#i#';
cfhttp( url = gitlabAPI, method="GET", result="local.project" ) {
cfhttpparam( type="header", name="Content-Type", value="application/json");
cfhttpparam( type="header", name="PRIVATE-TOKEN", value="#privateToken#");
cfhttpparam( type="url", name="simple", value="true");
};
if (local.project.status_code != 200){
job.start("#i#: project - #local.project.status_code#");
job.complete();
continue;
}
var prj = deserializeJSON(local.project.filecontent);
cfhttp( url = gitlabAPI & "/repository/branches/master", method="GET", result="local.branch" ) {
cfhttpparam( type="header", name="Content-Type", value="application/json");
cfhttpparam( type="header", name="PRIVATE-TOKEN", value="#privateToken#");
cfhttpparam( type="url", name="simple", value="true");
};
if (local.branch.status_code == 200){
fileWrite("compare/#i#.master." & replace(prj.path_with_namespace, "/", '.', "all") & ".json", local.branch.filecontent);
var bran = deserializeJSON(local.branch.filecontent);
} else {
job.start("#i#: #prj.path_with_namespace# master branch - #local.branch.status_code#");
job.complete();
continue;
}
cfhttp( url = gitlabAPI & "/repository/tags", method="POST", result="local.createTag" ) {
cfhttpparam( type="header", name="PRIVATE-TOKEN", value="#privateToken#");
cfhttpparam( type="form",name="tag_name", value="#tagName#");
cfhttpparam( type="form",name="ref", value="#bran.commit.id#");
cfhttpparam( type="form",name="release_description", value="#tagDescr#");
};
if ( listContains("200, 201", local.createTag.status_code)){
fileWrite("releases/#i#.POST." & replace(prj.path_with_namespace, "/", '.', "all") & ".json", local.createTag.filecontent);
} else {
fileWrite("releases/#i#.POSThole." & replace(prj.path_with_namespace, "/", '.', "all") & ".json", serializeJSON(local.createTag));
job.start("#i#: release POST - #local.createTag.status_code#");
job.complete();
continue;
}
cfhttp( url = gitlabAPI & "/repository/tags/#tagName#", method="GET", result="local.release" ) {
cfhttpparam( type="header", name="Content-Type", value="application/json");
cfhttpparam( type="header", name="PRIVATE-TOKEN", value="#privateToken#");
cfhttpparam( type="url", name="simple", value="true");
};
if (local.release.status_code == 200){
fileWrite("releases/#i#." & replace(prj.path_with_namespace, "/", '.', "all") & ".json", local.release.filecontent);
} else {
job.start("#i#: release POST - #local.release.status_code#");
job.complete();
continue;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment