Your browser was unable to load all of the resources. They may have been blocked by your firewall, proxy or browser configuration.
Press Ctrl+F5 or Ctrl+Shift+R to have your browser try again.

Merge two or more build requests in Pre-Queue script #4389

prinkush2 ·

Our review system trigger some tests on a QB configuration through rest API and I do not want each test to be triggered as separate build. All tests belonging to same review should be triggered in the same build. The standard API provided by review system can send rest api requests test-wise only.
I have been trying to merge requests from the same review in a single build as they have some common task to be performed.

Let's say for review id '1234' , three test builds are requested ,say, Build A, build B , build C ( could be in any order)
I have a variable USEFUL_VAR in each request which I want to merge in single build. If Build A is served first, I would like to append values of variable USEFUL_VAR of BUILD requests B and C into Build A.
But every time I try this I have been facing all sorts of concurrency issues in QB. Sometimes I lose some variables in the variable map and other times build C starts running which would be expected to be cancelled.

...
//  IF a RUNNING build and Current request have same REVIEW id ( say , 1234)
// b_id is the build id of the FIRST Test (say, BUILD A ) with same review id
def b_id = BuildEngine.instance.getBuildId(it.getId(),null);
        if(b_id != null )
        {
            try{
                String varName="USEFUL_VAR",varValue = request.getVariables().get("USEFUL_VAR")
                Build b = system.buildManager.load(b_id)
                Map<String, VariableWrapper> variables = b.getVariables()
                varValue = b.getVarValue(varName) + "\n" + varValue
                variables.put(varName, new VariableWrapper(varName, varValue))
                b.getVariables().get(varName).setValue(varValue)
                system.buildManager.saveVariables(b_id, variables.values())
                }
            catch(e){
                    logger.error("ERROR-updateVariable(varName=" + varName + " / varValue=" + varValue + "): " + e.getMessage())
                }
        }
// Cancel the current build request now as we have appended the important variable value
...

Could you please suggest a solution for how I can achieve this target of merging same name variables of different build requests?

  • replies 1
  • views 1587
  • stars 1
robinshen ADMIN ·

For builds in queue, please operate on the build object stored in request object directly instead of the object stored in database. I modified the script as below:

groovy:
String varName = "USEFUL_VAR"
String varValue = request.getVariables().get(varName)
for (eachRequest in system.buildEngine.getBuildRequests()) {
  if (hasSameReviewId(eachRequest, request) { // check if eachRequest has same review id as current request
    Build b = eachRequest.build
    b.getVar(varName).setValue(b.getVarValue(varName) + "\n" + varValue)
    system.buildEngine.cancelRequest(eachRequest.id)
  }
}