From b517d6bccd2f7f13273e193a1d550746b18dc20a Mon Sep 17 00:00:00 2001 From: Semyon Bezrukov Date: Thu, 12 Sep 2024 14:29:31 +0300 Subject: [PATCH] BM5 --- Jenkinsfile | 58 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 732710e..f90bd16 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -31,6 +31,7 @@ pipeline { } environment { GIT_SERVER = 'git.onlyoffice.com' + GIT_OWNER = 'heatray' GITEA_TOKEN = credentials('gitea-token') TELEGRAM_TOKEN = credentials('telegram-bot-token') } @@ -55,7 +56,7 @@ pipeline { ) booleanParam ( name: 'protect_branch', - description: 'Protect branch (for start only)', + description: 'Protect branch (for start & rename only)', defaultValue: defaults.protect_branch ) string ( @@ -96,7 +97,6 @@ pipeline { stats.repos.each { repo, status -> pAction = printBranches(repo) status.primary = (pAction) ? 'success' : 'failure' - status.secondary = 'none' } } else if (params.action_type == 'start') { @@ -120,9 +120,13 @@ pipeline { } } - if (params.protect_branch && !repo.contains('documents-pipeline')) { - sAction = protectBranch(repo, branch) - status.secondary = (sAction) ? 'lock' : '' + if (params.protect_branch) { + if (repo != 'documents-pipeline') { + sAction = protectBranch(repo, branch) + } else { + sAction = false + } + status.secondary = (sAction) ? 'lock' : 'none' } } @@ -159,9 +163,13 @@ pipeline { unprotectBranch(repo, branch) pAction = mergeBranch(repo, branch, baseBranches) status.primary = (pAction) ? 'success' : 'failure' - if (pAction && !repo.contains('documents-pipeline')) { - sAction = deleteBranch(repo, branch) - status.secondary = (sAction) ? 'delete' : '' + if (pAction) { + if (repo != 'documents-pipeline') { + sAction = deleteBranch(repo, branch) + } else { + sAction = false + } + status.secondary = (sAction) ? 'delete' : 'none' } } } @@ -184,9 +192,13 @@ pipeline { if (pAction) { unprotectBranch(repo, env.BRANCH_NAME) deleteBranch(repo, env.BRANCH_NAME) - if (params.protect_branch && !repo.contains('documents-pipeline')) { - sAction = protectBranch(repo, branch) - status.secondary = (sAction) ? 'lock' : '' + if (params.protect_branch) { + if (repo != 'documents-pipeline') { + sAction = protectBranch(repo, branch) + } else { + sAction = false + } + status.secondary = (sAction) ? 'lock' : 'none' } } } @@ -203,7 +215,7 @@ pipeline { dir ('repos/' + repo) { checkoutRepo(repo, branch) unprotectBranch(repo, branch) - if (!repo.contains('documents-pipeline')) { + if (repo != 'documents-pipeline') { pAction = deleteBranch(repo, branch) status.primary = (pAction) ? 'success' : 'failure' } @@ -216,7 +228,6 @@ pipeline { stats.repos.each { repo, status -> pAction = protectBranch(repo, branch) status.primary = (pAction) ? 'success' : 'failure' - status.secondary = 'none' } } else if (params.action_type == 'unprotect') { @@ -224,7 +235,6 @@ pipeline { stats.repos.each { repo, status -> pAction = unprotectBranch(repo, branch) status.primary = (pAction) ? 'success' : 'failure' - status.secondary = 'none' } } @@ -260,7 +270,7 @@ pipeline { } def getRepos() { - return ['heatray/foo'] + return ['foo'] } def checkoutRepo(String repo, String branch = 'master') { @@ -274,7 +284,7 @@ def checkoutRepo(String repo, String branch = 'master') { git clean -df else rm -rfv ./* - git clone -b ${branch} git@\$GIT_SERVER:${repo}.git . + git clone -b ${branch} git@\$GIT_SERVER:\$GIT_OWNER/${repo}.git . fi git branch -vv """ @@ -284,7 +294,7 @@ def checkoutRepo(String repo, String branch = 'master') { def checkRemoteBranch(String repo, String branch = 'master') { return sh ( label: "${repo}: check branch ${branch}", - script: "git ls-remote --exit-code git@\$GIT_SERVER:${repo}.git ${branch}", + script: "git ls-remote --exit-code git@\$GIT_SERVER:\$GIT_OWNER/${repo}.git ${branch}", returnStatus: true ) == 0 } @@ -322,7 +332,7 @@ def mergeBranch(String repo, String branch, ArrayList baseBranches) { echo "No new commits." continue fi - # gh pr create --repo ${repo} --base \$base --head ${branch} \ + # gh pr create --repo \$GIT_OWNER/${repo} --base \$base --head ${branch} \ # --title "Merge branch ${branch} into \$base" --fill || \ # true if ! git merge ${branch} --no-edit --no-ff \ @@ -362,7 +372,7 @@ def printBranches(String repo) { label: "${repo}: branches list", script: """ HTTP_CODE=\$(curl -s -X 'GET' \ - 'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branches' \ + 'https://'"\$GIT_SERVER"'/api/v1/repos/'"\$GIT_OWNER"'/${repo}/branches' \ -H 'Authorization: token '"\$GITEA_TOKEN" \ -w '%{http_code}' \ -o output.json) @@ -386,7 +396,7 @@ def protectBranch(String repo, String branch) { ] }' | \ curl -s -X 'POST' \ - 'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branch_protections' \ + 'https://'"\$GIT_SERVER"'/api/v1/repos/'"\$GIT_OWNER"'/${repo}/branch_protections' \ -H 'Authorization: token '"\$GITEA_TOKEN" \ -H 'Content-Type: application/json' \ -w '%{http_code}' \ @@ -405,7 +415,7 @@ def unprotectBranch(String repo, String branch) { label: "${repo}: unprotect ${branch}", script: """ HTTP_CODE=\$(curl -s -X 'DELETE' \ - 'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branch_protections/${branchUrl}' \ + 'https://'"\$GIT_SERVER"'/api/v1/repos/'"\$GIT_OWNER"'/${repo}/branch_protections/${branchUrl}' \ -H 'Authorization: token '"\$GITEA_TOKEN" \ -w '%{http_code}' \ -o output.json) @@ -443,10 +453,10 @@ def sendNotification() { switch(status.secondary) { case 'lock': text += '🔒'; break case 'delete': text += '♻️'; break - case 'none': text += ''; break - default: text += '➖' + case 'none': text += '➖'; break + default: text += '' } - text += " [${repo}](https://${env.GIT_SERVER}/${repo})" + text += " [${repo}](https://${env.GIT_SERVER}/${env.GIT_OWNER}/${repo})" } echo text