|
| 1 | +""" |
| 2 | +Run this script to do an in-place conversion of a pipeline job that is currently using |
| 3 | +insights-pipeline-lib<v3 to convert it to v3. Note that this will replace the contents of the |
| 4 | +file, so take a backup or ensure that you can revert changes (e.x. via git) on the file if |
| 5 | +necessary. |
| 6 | +
|
| 7 | +$ python convert-to-v3.py /path/to/Jenkinsfile |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import sys |
| 12 | + |
| 13 | + |
| 14 | +# We could use regex matching but at the moment building the regex strings would take more |
| 15 | +# time than it took to compile this list the "easy way"... |
| 16 | +replacements = { |
| 17 | + "cancelPriorBuilds": "pipelineUtils.cancelPriorBuilds", |
| 18 | + "changedFiles": "gitUtils.getFilesChanged", |
| 19 | + "checkCoverage": "pythonUtils.checkCoverage", |
| 20 | + "checkOutRepo": "gitUtils.checkOutRepo", |
| 21 | + "deployHelpers": "deployUtils", |
| 22 | + "deployServiceSet": "deployUtils.deployServiceSet", |
| 23 | + "deploymentPipeline": "execDeployPipeline", |
| 24 | + "getFilesChanged": "gitUtils.getFilesChanged", |
| 25 | + "ghNotify": "gitUtils.ghNotify", |
| 26 | + "openShift": "openShiftUtils", |
| 27 | + "pipelineVars.defaultUICloud": "pipelineVars.upshiftCloud", |
| 28 | + "pipelineVars.defaultUINameSpace": "pipelineVars.upshiftNameSpace", |
| 29 | + "pipfileComment.post": "pythonUtils.postPipfileComment", |
| 30 | + "pipfileComment.removeAll": "pythonUtils.removePipfileComments", |
| 31 | + "promoteImages": "deployUtils.promoteImages", |
| 32 | + "runBundleInstall": "rubyUtils.runBundleInstall", |
| 33 | + "runIfMasterOrPullReq": "pipelineUtils.runIfMasterOrPullReq", |
| 34 | + "runParallel": "pipelineUtils.runParallel", |
| 35 | + "runPipenvInstall": "pythonUtils.runPipenvInstall", |
| 36 | + "runPythonLintCheck": "pythonUtils.runLintCheck", |
| 37 | + "runSmokeTest": "execSmokeTest", |
| 38 | + "slackNotify": "slack.sendMsg", |
| 39 | + "stageWithContext": "gitUtils.stageWithContext", |
| 40 | + "triggeredByComment": "pipelineUtils.triggeredByComment", |
| 41 | + "waitForDeployment": "deployUtils.waitForDeployment", |
| 42 | + "withStatusContext.lint": 'gitUtils.withStatusContext("lint")', |
| 43 | + "withStatusContext.unitTest": 'gitUtils.withStatusContext("unittest")', |
| 44 | + "withStatusContext.integrationTest": 'gitUtils.withStatusContext("integrationtest")', |
| 45 | + "withStatusContext.coverage": 'gitUtils.withStatusContext("coverage")', |
| 46 | + "withStatusContext.pipInstall": 'gitUtils.withStatusContext("pipinstall")', |
| 47 | + "withStatusContext.bundleInstall": 'gitUtils.withStatusContext("bundleinstall")', |
| 48 | + "withStatusContext.swagger": 'gitUtils.withStatusContext("swagger")', |
| 49 | + "withStatusContext.smoke": 'gitUtils.withStatusContext("smoke")', |
| 50 | + "withStatusContext.dbMigrate": 'gitUtils.withStatusContext("dbmigrate")', |
| 51 | + "withStatusContext.artifacts": 'gitUtils.withStatusContext("artifacts")', |
| 52 | + "withStatusContext.waitForFrontend": 'gitUtils.withStatusContext("waitforfrontend")', |
| 53 | + "withStatusContext.lint(": 'gitUtils.withStatusContext("lint", ', |
| 54 | + "withStatusContext.unitTest(": 'gitUtils.withStatusContext("unittest", ', |
| 55 | + "withStatusContext.integrationTest(": 'gitUtils.withStatusContext("integrationtest", ', |
| 56 | + "withStatusContext.coverage(": 'gitUtils.withStatusContext("coverage", ', |
| 57 | + "withStatusContext.pipInstall(": 'gitUtils.withStatusContext("pipinstall", ', |
| 58 | + "withStatusContext.bundleInstall(": 'gitUtils.withStatusContext("bundleinstall", ', |
| 59 | + "withStatusContext.swagger(": 'gitUtils.withStatusContext("swagger", ', |
| 60 | + "withStatusContext.smoke(": 'gitUtils.withStatusContext("smoke", ', |
| 61 | + "withStatusContext.dbMigrate(": 'gitUtils.withStatusContext("dbmigrate", ', |
| 62 | + "withStatusContext.artifacts(": 'gitUtils.withStatusContext("artifacts", ', |
| 63 | + "withStatusContext.waitForFrontend(": 'gitUtils.withStatusContext("waitforfrontend", ', |
| 64 | + "withStatusContext.custom(": "gitUtils.withStatusContext(", |
| 65 | + '@Library("github.com/RedHatInsights/insights-pipeline-lib")': ( |
| 66 | + '@Library("github.com/RedHatInsights/insights-pipeline-lib@v3")' |
| 67 | + ), |
| 68 | + "@Library('github.com/RedHatInsights/insights-pipeline-lib')": ( |
| 69 | + "@Library('github.com/RedHatInsights/insights-pipeline-lib')" |
| 70 | + ) |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +try: |
| 75 | + filename = os.path.abspath(sys.argv[1]) |
| 76 | +except IndexError: |
| 77 | + print("You didn't provide a filename") |
| 78 | + sys.exit(1) |
| 79 | + |
| 80 | + |
| 81 | +with open(filename, "r") as readfile, open(f"{filename}-new", "w") as writefile: |
| 82 | + for lineno, line in enumerate(readfile): |
| 83 | + changed = False |
| 84 | + orig_line = line |
| 85 | + for orig, new in replacements.items(): |
| 86 | + if orig in line: |
| 87 | + changed = True |
| 88 | + line = line.replace(orig, new) |
| 89 | + if changed: |
| 90 | + |
| 91 | + print("line {} old: {}".format(lineno, orig_line.strip('\n'))) |
| 92 | + print("line {} new: {}\n".format(lineno, line.strip('\n'))) |
| 93 | + writefile.write(line) |
| 94 | + |
| 95 | +os.rename(f"{filename}-new", filename) |
| 96 | + |
| 97 | +print(f"Changes saved to {filename}") |
0 commit comments