From dfd04e1bf434730a3c44bc9e0c39772abca3fc77 Mon Sep 17 00:00:00 2001 From: Semyon Bezrukov Date: Wed, 11 Sep 2024 16:38:58 +0300 Subject: [PATCH] BM2 --- Jenkinsfile | 66 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 1e2992d..c4bc28d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -14,11 +14,11 @@ if (env.BRANCH_NAME == 'develop') { defaults.branch_type = 'release' } if (env.BRANCH_NAME ==~ /^(hotfix)\/.+/) { - defaults.action_type.addAll(['merge_hotfix', 'finish_hotfix', 'rename_hotfix', 'delete_hotfix', 'unprotect_hotfix']) + defaults.action_type.addAll(['merge_hotfix', 'finish_hotfix', 'rename_hotfix', 'delete_hotfix', 'protect_hotfix', 'unprotect_hotfix']) defaults.branch_type = 'hotfix' } if (env.BRANCH_NAME ==~ /^(release)\/.+/) { - defaults.action_type.addAll(['merge_release', 'finish_release', 'rename_release', 'delete_release', 'unprotect_release']) + defaults.action_type.addAll(['merge_release', 'finish_release', 'rename_release', 'delete_release', 'protect_release', 'unprotect_release']) defaults.branch_type = 'release' } @@ -94,9 +94,10 @@ pipeline { stats.repos.each { repo, status -> pAction = printBranches(repo) status.primary = (pAction) ? 'success' : 'failure' + status.secondary = 'none' } - } else if params.action_type.startsWith('start') { + } else if (params.action_type.startsWith('start')) { branch = defaults.branch_type + '/v' + params.version baseBranches = [env.BRANCH_NAME] @@ -117,13 +118,13 @@ pipeline { } } - if (params.protect_branch) { + if (params.protect_branch && !repo.contains('documents-pipeline')) { sAction = protectBranch(repo, branch) status.secondary = (sAction) ? 'lock' : '' } } - } else if params.action_type.startsWith('merge') { + } else if (params.action_type.startsWith('merge')) { baseBranches = ['master'] @@ -140,7 +141,7 @@ pipeline { } } - } else if params.action_type.startsWith('finish') { + } else if (params.action_type.startsWith('finish')) { baseBranches = ['master', 'develop'] if (!params.extra_branch.isEmpty()) @@ -164,7 +165,7 @@ pipeline { } } - } else if params.action_type.startsWith('rename') { + } else if (params.action_type.startsWith('rename')) { branch = defaults.branch_type + '/v' + params.version baseBranches = [env.BRANCH_NAME] @@ -181,7 +182,7 @@ pipeline { if (pAction) { unprotectBranch(repo, env.BRANCH_NAME) deleteBranch(repo, env.BRANCH_NAME) - if (params.protect_branch) { + if (params.protect_branch && !repo.contains('documents-pipeline')) { sAction = protectBranch(repo, branch) status.secondary = (sAction) ? 'lock' : '' } @@ -190,7 +191,7 @@ pipeline { } } - } else if params.action_type.startsWith('delete') { + } else if (params.action_type.startsWith('delete')) { stats.repos.each { repo, status -> if (!checkRemoteBranch(repo, branch)) { @@ -208,7 +209,7 @@ pipeline { } } - } else if params.action_type.startsWith('protect') { + } else if (params.action_type.startsWith('protect')) { stats.repos.each { repo, status -> pAction = protectBranch(repo, branch) @@ -216,7 +217,7 @@ pipeline { status.secondary = 'none' } - } else if params.action_type.startsWith('unprotect') { + } else if (params.action_type.startsWith('unprotect')) { stats.repos.each { repo, status -> pAction = unprotectBranch(repo, branch) @@ -358,11 +359,13 @@ def printBranches(String repo) { return sh ( label: "${repo}: branches list", script: """ - curl -X 'GET' \ - 'https://\$GIT_SERVER/api/v1/repos/${repo}/branches' \ - -H 'accept: application/json' \ - -H 'Authorization: \$GITEA_TOKEN' | \ - jq -r '.[] | [.name, .protected] | @tsv' + HTTP_CODE=\$(curl -s -X 'GET' \ + 'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branches' \ + -H 'Authorization: token '"\$GITEA_TOKEN" \ + -w '%{http_code}' \ + -o output.json) + test \$HTTP_CODE -eq 200 + jq -r '.[] | [.name, .protected] | @tsv' output.json """, returnStatus: true ) == 0 @@ -372,29 +375,36 @@ def protectBranch(String repo, String branch) { return sh ( label: "${repo}: protect ${branch}", script: """ - echo '{ - "branch_name": "master" + HTTP_CODE=\$(echo '{ + "branch_name": "${branch}" }' | \ - curl -X 'POST' \ - 'https://\$GIT_SERVER/api/v1/repos/${repo}/branch_protections?token=\$GITEA_TOKEN' \ - -H 'accept: application/json' \ - -H 'Authorization: \$GITEA_TOKEN' \ + curl -s -X 'POST' \ + 'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branch_protections' \ + -H 'Authorization: token '"\$GITEA_TOKEN" \ -H 'Content-Type: application/json' \ - -d - + -w '%{http_code}' \ + -o output.json \ + -d @-) + jq '.' output.json + test \$HTTP_CODE -eq 201 """, returnStatus: true ) == 0 } def unprotectBranch(String repo, String branch) { + String branchUrl = URLEncoder.encode(branch) return sh ( label: "${repo}: unprotect ${branch}", script: """ - curl -X 'DELETE' \ - 'https://\$GIT_SERVER/api/v1/repos/${repo}/branch_protections/${branch}?token=\$GITEA_TOKEN' \ - -H 'accept: application/json' \ - -H 'Authorization: \$GITEA_TOKEN' - """ + HTTP_CODE=\$(curl -s -X 'DELETE' \ + 'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branch_protections/${branchUrl}' \ + -H 'Authorization: token '"\$GITEA_TOKEN" \ + -w '%{http_code}' \ + -o output.json) + jq '.' output.json + test \$HTTP_CODE -eq 204 || test \$HTTP_CODE -eq 404 + """, returnStatus: true ) == 0 }