Milosz Wasilewski | 1043858 | 2020-12-03 11:36:21 +0000 | [diff] [blame] | 1 | import hudson.model.* |
| 2 | |
| 3 | def getUpstreamRoot(cause) { |
| 4 | causes = cause.getUpstreamCauses() |
| 5 | if (causes.size() > 0) { |
| 6 | if (causes[0] instanceof hudson.model.Cause.UpstreamCause) { |
| 7 | return getUpstreamRoot(causes[0]) |
| 8 | } |
| 9 | } |
| 10 | return cause |
| 11 | } |
| 12 | |
Paul Sokolovsky | 7c1186b | 2021-11-09 17:16:55 +0300 | [diff] [blame] | 13 | def description = "" |
| 14 | def rootUrl = manager.hudson.getRootUrl() |
| 15 | |
Milosz Wasilewski | 1043858 | 2020-12-03 11:36:21 +0000 | [diff] [blame] | 16 | // Add a LAVA job link to the description |
Paul Sokolovsky | 5aba733 | 2023-03-16 18:18:05 +0700 | [diff] [blame] | 17 | def matcher = manager.getLogMatcher("LAVA URL: (?<url>.*?) LAVA JOB ID: (?<jobid>\\d+)") |
Milosz Wasilewski | 1043858 | 2020-12-03 11:36:21 +0000 | [diff] [blame] | 18 | if (matcher?.matches()) { |
| 19 | def testJobId = matcher.group('jobid') |
| 20 | def testJobUrl = matcher.group('url') |
Paul Sokolovsky | c518937 | 2023-12-30 14:17:11 +0300 | [diff] [blame] | 21 | description += "LAVA Job Id: <a href='${testJobUrl}'>${testJobId}</a>\n" |
Leonardo Sandoval | 9f15980 | 2021-01-20 16:47:25 -0600 | [diff] [blame] | 22 | |
Leonardo Sandoval | 9f15980 | 2021-01-20 16:47:25 -0600 | [diff] [blame] | 23 | def lavaLogUrl = "${rootUrl}${manager.build.url}artifact/lava.log" |
Paul Sokolovsky | 117c7c5 | 2024-01-04 12:20:18 +0300 | [diff] [blame] | 24 | description += " | <a href='${lavaLogUrl}'>log</a>\n" |
Leonardo Sandoval | a8078d6 | 2021-02-11 16:29:25 -0600 | [diff] [blame] | 25 | |
Paul Sokolovsky | 2e533b4 | 2024-01-09 21:50:20 +0300 | [diff] [blame] | 26 | // Verify LAVA jobs results, all tests must pass, otherwise turn build into FAILED |
Leonardo Sandoval | a8078d6 | 2021-02-11 16:29:25 -0600 | [diff] [blame] | 27 | def testMatcher = manager.getLogMatcher("LAVA JOB RESULT: (?<result>\\d+)") |
| 28 | if (testMatcher?.matches()) { |
| 29 | def testJobSuiteResult = testMatcher.group('result') |
| 30 | // result = 1 means lava job fails |
| 31 | if (testJobSuiteResult == "1") { |
Paul Sokolovsky | 2e533b4 | 2024-01-09 21:50:20 +0300 | [diff] [blame] | 32 | manager.buildFailure() |
Leonardo Sandoval | a8078d6 | 2021-02-11 16:29:25 -0600 | [diff] [blame] | 33 | } |
| 34 | } |
Milosz Wasilewski | 1043858 | 2020-12-03 11:36:21 +0000 | [diff] [blame] | 35 | } |
Paul Sokolovsky | 7c1186b | 2021-11-09 17:16:55 +0300 | [diff] [blame] | 36 | |
Paul Sokolovsky | 6341435 | 2023-12-30 12:12:35 +0300 | [diff] [blame] | 37 | // Add a TuxSuite job link to the description |
| 38 | matcher = manager.getLogMatcher("TuxSuite test ID: (?<tuxid>[A-Za-z0-9]+)") |
| 39 | if (matcher?.matches()) { |
| 40 | def tuxId = matcher.group('tuxid') |
| 41 | def abbrTuxId = "..." + tuxId.substring(19) |
Arthur She | 78acaae | 2025-02-05 06:15:04 +0100 | [diff] [blame] | 42 | // Retrieve environment variables |
Arthur She | 313a988 | 2025-02-04 21:19:31 -0800 | [diff] [blame] | 43 | def envVars = manager.build.getEnvironment(manager.listener) |
| 44 | def tuxsuiteGroup = envVars.get('TUXSUITE_GROUP') |
| 45 | def tuxsuiteProject = envVars.get('TUXSUITE_PROJECT') |
Arthur She | 78acaae | 2025-02-05 06:15:04 +0100 | [diff] [blame] | 46 | |
Arthur She | 313a988 | 2025-02-04 21:19:31 -0800 | [diff] [blame] | 47 | // Ensure environment variables are set |
Arthur She | 78acaae | 2025-02-05 06:15:04 +0100 | [diff] [blame] | 48 | if (!tuxsuiteGroup || !tuxsuiteProject) { |
Arthur She | 313a988 | 2025-02-04 21:19:31 -0800 | [diff] [blame] | 49 | manager.listener.logger.println("Error: TUXSUITE_GROUP or TUXSUITE_PROJECT environment variable is not set.") |
Arthur She | 78acaae | 2025-02-05 06:15:04 +0100 | [diff] [blame] | 50 | manager.buildFailure() |
Arthur She | 313a988 | 2025-02-04 21:19:31 -0800 | [diff] [blame] | 51 | throw new RuntimeException("Missing required environment variables: TUXSUITE_GROUP or TUXSUITE_PROJECT.") |
Arthur She | 78acaae | 2025-02-05 06:15:04 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | description += "Tux Id: <a href='https://tuxapi.tuxsuite.com/v1/groups/${tuxsuiteGroup}/projects/${tuxsuiteProject}/tests/${tuxId}'>${abbrTuxId}</a>\n" |
| 55 | description += " | <a href='https://storage.tuxsuite.com/public/${tuxsuiteGroup}/${tuxsuiteProject}/tests/${tuxId}/lava-logs.html'>log</a>\n" |
Paul Sokolovsky | ff615d9 | 2024-01-26 16:29:55 +0700 | [diff] [blame] | 56 | |
| 57 | // Verify test job results set build status as FAILED if needed |
| 58 | def testMatcher = manager.getLogMatcher("TuxSuite test result: (?<result>\\d+)") |
| 59 | if (testMatcher?.matches()) { |
| 60 | def testJobSuiteResult = testMatcher.group('result') |
| 61 | // result = 1 means job fails |
| 62 | if (testJobSuiteResult == "1") { |
| 63 | manager.buildFailure() |
| 64 | } |
| 65 | } |
Paul Sokolovsky | 6341435 | 2023-12-30 12:12:35 +0300 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | |
Paul Sokolovsky | 7c1186b | 2021-11-09 17:16:55 +0300 | [diff] [blame] | 69 | def causes = manager.build.getAction(hudson.model.CauseAction.class).getCauses() |
| 70 | if (causes[0] instanceof hudson.model.Cause.UpstreamCause) { |
| 71 | def rootCause = getUpstreamRoot(causes[0]) |
| 72 | def upstreamBuild = rootCause.upstreamBuild |
| 73 | def upstreamProject = rootCause.upstreamProject |
| 74 | def jobName = upstreamProject |
| 75 | def jobConfiguration = upstreamProject |
| 76 | def jobUrl = "${rootUrl}job/${upstreamProject}/${upstreamBuild}" |
Paul Sokolovsky | c518937 | 2023-12-30 14:17:11 +0300 | [diff] [blame] | 77 | description += "<br>Top build: <a href='${jobUrl}'>${upstreamProject} #${upstreamBuild}</a>" |
Paul Sokolovsky | 7c1186b | 2021-11-09 17:16:55 +0300 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Set accumulated description |
| 81 | manager.build.setDescription(description) |