This commit is contained in:
Semyon Bezrukov 2024-09-11 17:35:30 +03:00
parent f08e80f131
commit 22d656691c

27
Jenkinsfile vendored
View File

@ -94,6 +94,7 @@ pipeline {
stats.repos.each { repo, status -> stats.repos.each { repo, status ->
pAction = printBranches(repo) pAction = printBranches(repo)
status.primary = (pAction) ? 'success' : 'failure' status.primary = (pAction) ? 'success' : 'failure'
status.secondary = 'none'
} }
} else if (params.action_type.startsWith('start')) { } else if (params.action_type.startsWith('start')) {
@ -358,11 +359,13 @@ def printBranches(String repo) {
return sh ( return sh (
label: "${repo}: branches list", label: "${repo}: branches list",
script: """ script: """
curl -sv -f -X 'GET' \ HTTP_CODE=\$(curl -s -X 'GET' \
'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branches' \ 'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branches' \
-H 'accept: application/json' \ -H 'Authorization: '"\$GITEA_TOKEN" \
-H 'Authorization: '"\$GITEA_TOKEN" | \ -w '%{http_code}' \
jq -r '.[] | [.name, .protected] | @tsv' -o output.json)
test \$HTTP_CODE -eq 200
jq -r '.[] | [.name, .protected] | @tsv' output.json
""", """,
returnStatus: true returnStatus: true
) == 0 ) == 0
@ -373,13 +376,11 @@ def protectBranch(String repo, String branch) {
label: "${repo}: protect ${branch}", label: "${repo}: protect ${branch}",
script: """ script: """
echo '{ echo '{
"branch_name": "master" "branch_name": "${branch}"
}' | \ }' | \
curl -sv -f -X 'POST' \ curl -s -X 'POST' \
'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branch_protections' \ 'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branch_protections' \
-H 'accept: application/json' \ -H 'Authorization: token '"\$GITEA_TOKEN" \
-H 'Authorization: '"\$GITEA_TOKEN" \
-H 'Content-Type: application/json' \
-d @- -d @-
""", """,
returnStatus: true returnStatus: true
@ -387,13 +388,13 @@ def protectBranch(String repo, String branch) {
} }
def unprotectBranch(String repo, String branch) { def unprotectBranch(String repo, String branch) {
String branchUrl = URLEncoder.encode(branch)
return sh ( return sh (
label: "${repo}: unprotect ${branch}", label: "${repo}: unprotect ${branch}",
script: """ script: """
curl -sv -f -X 'DELETE' \ curl -s -X 'DELETE' \
'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branch_protections/${branch}' \ 'https://'"\$GIT_SERVER"'/api/v1/repos/${repo}/branch_protections/${branchUrl}' \
-H 'accept: application/json' \ -H 'Authorization: token '"\$GITEA_TOKEN"
-H 'Authorization: '"\$GITEA_TOKEN"
""", """,
returnStatus: true returnStatus: true
) == 0 ) == 0