summaryrefslogtreecommitdiff
path: root/post-build-lava.groovy
blob: cbcc15de9508af7def442070e5f9293bd16f053a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import hudson.model.*

// Add a LAVA job link to the description
def matcher = manager.getLogMatcher(".*LAVA Job Id.*")
if (matcher?.matches()) {
    def lavaJobId = matcher.group(0).split(",")[0].substring(13)
    if (!lavaJobId.isInteger()) {
        lavaJobId = matcher.group(0).tokenize("'")[1]
    }
    def lavaServer = matcher.group(0).tokenize("/")[1]
    def lavaJobUrl = "https://${lavaServer}/scheduler/job/${lavaJobId}"
    def lavaDescription = "&nbsp;LAVA Job Id: <a href='${lavaJobUrl}'>${lavaJobId}</a>"

    def cause = manager.build.getAction(hudson.model.CauseAction.class).getCauses()
    def upstreamBuild = cause[0].upstreamBuild
    def upstreamProject = cause[0].upstreamProject
    def jobName = upstreamProject
    def jobConfiguration = upstreamProject
    def jobUrl = manager.hudson.getRootUrl() + "job/${upstreamProject}/${upstreamBuild}"
    def jobDescription = "<br>&nbsp;Build <a href='${jobUrl}'>${upstreamProject} #${upstreamBuild}</a>"

    manager.build.setDescription(lavaDescription + jobDescription)

    // Multi-configuration project
    if (upstreamProject.contains("/")) {
        jobName = upstreamProject.split("/")[0]
        jobConfiguration = upstreamProject.split("/")[1]
    }

    def jobs = hudson.model.Hudson.instance.getItem(jobName).getAllJobs()

    for (job in jobs) {
        if (job.name == jobConfiguration) {
            if (job.getLastBuild().getDescription() != null) {
                lavaDescription += "<br>" + job.getLastBuild().getDescription()
            }
            job.getLastBuild().setDescription(lavaDescription)
        }
    }

    // Add parameters
    def action = manager.build.getAction(hudson.model.ParametersAction.class)
    def parameters = [
            new StringParameterValue("LAVA_SERVER", "${lavaServer}/RPC2/"),
            new StringParameterValue("LAVA_JOB_ID", "${lavaJobId}"),
            new StringParameterValue("BUILD_JOB", "${jobUrl}")
    ]
    updatedAction = action.createUpdated(parameters)
    manager.build.replaceAction(updatedAction)

    // Update the pool of jobs to monitor
    job = hudson.model.Hudson.instance.getItem("check-lava-status")
    property = job.getProperty(hudson.model.ParametersDefinitionProperty.class)
    parameter = property.getParameterDefinition("LAVA_JOB_ID_POOL")
    lavaJobIdPool = parameter.getDefaultValue()
    lavaJobIdPool += " ${manager.build.number}"
    parameter.setDefaultValue(lavaJobIdPool)
    job.save()

    // Call post-build-report with parameters file
    def skipReport = manager.build.getEnvironment(manager.listener)['SKIP_REPORT']
    if (!skipReport.toBoolean()) {
        def project = manager.build.project;
        def pbrParamsCache = ""

        def sourceJob = hudson.model.Hudson.instance.getItem(jobName)
        def sourceVariables = sourceJob.getBuildByNumber(upstreamBuild.toInteger()).getEnvironment()
        for (sourceVariableName in sourceVariables.keySet()) {
            if (sourceVariableName.startsWith("GERRIT")) {
                sourceVariableValue = sourceVariables.get(sourceVariableName)
                pbrParamsCache += "SOURCE_${sourceVariableName}=${sourceVariableValue}\n";
            }
            if (sourceVariableName.startsWith("ART_URL")) {
                sourceVariableValue = sourceVariables.get(sourceVariableName)
                pbrParamsCache += "${sourceVariableName}=${sourceVariableValue}\n";
            }
        }

        pbrParamsCache += "SOURCE_PROJECT_NAME=${jobName}\n";
        pbrParamsCache += "SOURCE_BUILD_NUMBER=${upstreamBuild}\n";
        pbrParamsCache += "SOURCE_BUILD_ID=${upstreamBuild}\n";
        pbrParamsCache += "SOURCE_BUILD_URL=${jobUrl}\n";
        pbrParamsCache += "LAVA_JOB_IDS=${lavaJobId}\n";

        def pbrParams = project.getWorkspace().child("post_build_reports_parameters");
        pbrParams.write(pbrParamsCache, null);
    }
}