blob: 6cc39e3b04de157e109b0061fb929c47b488893e [file] [log] [blame]
Ivan Udovichenkoad0e39a2019-11-26 15:06:54 +03001package com.mirantis.mk
2/**
3 * Checkout KaaS release metadata repo with clone or without, if cloneRepo parameter is set
4 *
5 * @param params map with expected parameters:
6 * - metadataCredentialsId
7 * - metadataGitRepoUrl
8 * - metadataGitRepoBranch
9 * - repoDir
10 * - cloneRepo
11 */
12def checkoutKaasReleaseMetadataRepo(Map params = [:]) {
13 def git = new com.mirantis.mk.Git()
14
15 String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit')
16 String gitUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/kaas/releases")
17 String gitBranch = params.get('metadataGitRepoBranch', 'master')
18 String gitRef = params.get('metadataGitRepoRef', 'HEAD')
19 String repoDir = params.get('repoDir', 'releases')
20 Boolean cloneRepo = params.get('cloneRepo', true)
21 if (cloneRepo) {
22 stage('Cleanup repo dir') {
23 dir(repoDir) {
24 deleteDir()
25 }
26 }
27 stage('Cloning artifact-metadata repository') {
28 git.checkoutGitRepository(repoDir, gitUrl, gitBranch, gitCredentialsId, true, 10, 0, gitRef)
29 }
30 } else {
31 git.changeGitBranch(repoDir, gitRef ?: gitBranch)
32 }
33}
34
35
36/**
37 * Update KaaS release metadata value and upload CR to release metadata repository
38 *
39 * @param key metadata key (Several keys could be passed joined by ';' character)
40 * @param value metadata value (Several values could be passed joined by ';' character)
41 * @param params map with expected parameters:
42 * - metadataCredentialsId
43 * - metadataGitRepoUrl
44 * - metadataGitRepoBranch
45 * - repoDir
46 * - comment
47 * - crTopic
48 * - crAuthorName
49 * - crAuthorEmail
50 * - kaasMetadataFileToUpdate
51 */
52
53def updateKaasReleaseMetadata(String key, String value, Map params) {
54 String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit')
55 String metadataRepoUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/kaas/releases")
56 String metadataGerritBranch = params.get('metadataGitRepoBranch', 'master')
57 String repoDir = params.get('repoDir', 'releases')
58 String comment = params.get('comment', '')
59 String crTopic = params.get('crTopic', '')
60 String changeAuthorName = params.get('crAuthorName', 'MCP-CI')
61 String changeAuthorEmail = params.get('crAuthorEmail', 'mcp-ci-jenkins@ci.mcp.mirantis.net')
62 String fileToUpdatePath = params.get('kaasMetadataFileToUpdatePath', '')
63 String updateChartVersion = params.get('kaasMetadataUpdateChartVersion', '1')
64 String updateTagVersion = params.get('kaasMetadataUpdateTagVersion', '1')
65
66 def common = new com.mirantis.mk.Common()
67 def python = new com.mirantis.mk.Python()
68 def gerrit = new com.mirantis.mk.Gerrit()
69 def git = new com.mirantis.mk.Git()
70
71 def cred = common.getCredentials(gitCredentialsId, 'key')
72 String gerritUser = cred.username
73 String gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0]
74 String metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/')
75 String gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0]
76 String workspace = common.getWorkspace()
77 String venvDir = "${workspace}/gitreview-venv"
78 String ChangeId
79 String commitMessage
80 String gitRemote
81 stage("Installing virtualenv") {
82 python.setupVirtualenv(venvDir, 'python3', ['git-review'])
83 }
84 checkoutKaasReleaseMetadataRepo(params)
85 dir(repoDir) {
86 gitRemote = sh(
87 script:
88 'git remote -v | head -n1 | cut -f1',
89 returnStdout: true,
90 ).trim()
91 }
92
93 stage('Creating CR') {
94 def gerritAuth = ['PORT': gerritPort, 'USER': gerritUser, 'HOST': gerritHost]
95 def changeParams = ['owner': gerritUser, 'status': 'open', 'project': metadataProject, 'branch': metadataGerritBranch, 'topic': crTopic]
96 def gerritChange = gerrit.findGerritChange(gitCredentialsId, gerritAuth, changeParams)
97 git.changeGitBranch(repoDir, metadataGerritBranch)
98 if (gerritChange) {
99 def jsonChange = readJSON text: gerritChange
100 changeNum = jsonChange['number']
101 ChangeId = 'Change-Id: '
102 ChangeId += jsonChange['id']
103 } else {
104 ChangeId = ''
105 git.createGitBranch(repoDir, crTopic)
106 }
107
108 def keyArr = key.split(';')
109 def valueArr = value.split(';')
110 if (keyArr.size() == valueArr.size()) {
111 for (i in 0..keyArr.size()-1) {
112 common.infoMsg("Setting ${keyArr[i]} version to: ${valueArr[i]}")
113 sh '''set +x
114 tmpfile=\$(mktemp kaas_tmp_file.XXXXXX)
115 trap "rm -f \$tmpfile" 1 2 3 6
116
117 awk \
118 -v key_name='''+keyArr[i]+''' \
119 -v key_ver='''+valueArr[i]+''' \
120 -v update_chart_version='''+updateChartVersion+''' \
121 -v update_tag_version='''+updateTagVersion+''' '
122BEGIN {
123 match_found = 0;
124}
125
126// {
127if ($0 ~ "^\\\\s+- name: "key_name) {
128 match_found = 1;
129 print $0;
130 next;
131}
132if ($0 ~ /^\\s+- name: /) {
133 match_found = 0;
134}
135if (update_chart_version && match_found && $0 ~ /^\\s+version: /) {
136 print gensub(/(\\s+version:).*/,"\\\\1 "key_ver,1,$0);
137 next;
138}
139if (update_tag_version && match_found && $0 ~ /^\\s+tag: /) {
140 print gensub(/(\\s+tag:).*/,"\\\\1 "key_ver,1,$0);
141 next;
142}
143print $0
144}' '''+repoDir+'/'+fileToUpdatePath+''' > "\$tmpfile"
145 mv "\${tmpfile}" '''+repoDir+'/'+fileToUpdatePath+'''
146 '''
147 }
148 }
149
150 commitMessage =
151 """${comment}
152
153 |${ChangeId}
154 """.stripMargin()
155 //commit change
156 git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false)
157 //post change
158 gerrit.postGerritReview(gitCredentialsId, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch)
159 }
160}