|
| 1 | +tagOs() |
| 2 | +tagIde() |
| 3 | +tagCiOrLocal() |
| 4 | +addCiMetadata() |
| 5 | +addGitMetadata() |
| 6 | + |
| 7 | +void tagOs() { |
| 8 | + buildScan.tag System.getProperty('os.name') |
| 9 | +} |
| 10 | + |
| 11 | +void tagIde() { |
| 12 | + if (System.getProperty('idea.version')) { |
| 13 | + buildScan.tag 'IntelliJ IDEA' |
| 14 | + } else if (System.getProperty('eclipse.buildId')) { |
| 15 | + buildScan.tag 'Eclipse' |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +void tagCiOrLocal() { |
| 20 | + buildScan.tag(isCi() ? 'CI' : 'LOCAL') |
| 21 | +} |
| 22 | + |
| 23 | +void addGitMetadata() { |
| 24 | + buildScan.background { |
| 25 | + def gitCommitId = execAndGetStdout('git', 'rev-parse', '--short=8', '--verify', 'HEAD') |
| 26 | + def gitBranchName = execAndGetStdout('git', 'rev-parse', '--abbrev-ref', 'HEAD') |
| 27 | + def gitStatus = execAndGetStdout('git', 'status', '--porcelain') |
| 28 | + |
| 29 | + if(gitCommitId) { |
| 30 | + def commitIdLabel = 'Git Commit ID' |
| 31 | + value commitIdLabel, gitCommitId |
| 32 | + link 'Git commit build scans', customValueSearchUrl([(commitIdLabel): gitCommitId]) |
| 33 | + } |
| 34 | + if (gitBranchName) { |
| 35 | + tag gitBranchName |
| 36 | + value 'Git branch', gitBranchName |
| 37 | + } |
| 38 | + if (gitStatus) { |
| 39 | + tag 'dirty' |
| 40 | + value 'Git status', gitStatus |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +void addCiMetadata() { |
| 46 | + def ciBuild = 'CI BUILD' |
| 47 | + if (System.getenv('bamboo.resultsUrl')) { |
| 48 | + buildScan.link ciBuild, System.getenv('bamboo.resultsUrl') |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +boolean isCi() { |
| 53 | + System.getenv('bamboo.resultsUrl') |
| 54 | +} |
| 55 | + |
| 56 | +String execAndGetStdout(String... args) { |
| 57 | + def stdout = new ByteArrayOutputStream() |
| 58 | + exec { |
| 59 | + commandLine(args) |
| 60 | + standardOutput = stdout |
| 61 | + } |
| 62 | + return stdout.toString().trim() |
| 63 | +} |
| 64 | + |
| 65 | +String customValueSearchUrl(Map<String, String> search) { |
| 66 | + def query = search.collect { name, value -> |
| 67 | + "search.names=${encodeURL(name)}&search.values=${encodeURL(value)}" |
| 68 | + }.join('&') |
| 69 | + |
| 70 | + "$buildScan.server/scans?$query" |
| 71 | +} |
| 72 | + |
| 73 | +String encodeURL(String url){ |
| 74 | + URLEncoder.encode(url, 'UTF-8') |
| 75 | +} |
0 commit comments