Skip to content

Instantly share code, notes, and snippets.

@klutchell
Created January 10, 2024 20:58
Show Gist options
  • Save klutchell/6a97913026696a31706e9f9896f92278 to your computer and use it in GitHub Desktop.
Save klutchell/6a97913026696a31706e9f9896f92278 to your computer and use it in GitHub Desktop.
Cleanup repo settings files after exporting with https://github.com/balena-io-experimental/probot-get-repo-settings
#!/bin/bash
for file in .github/repos/*.yml; do
# delete custom merge settings
yq e 'del(.repository)' -i "$file"
# delete required approving review count as we use policy-bot instead
yq e 'del(.branches[].protection.required_pull_request_reviews.required_approving_review_count)' -i "$file"
if [ "$(yq e '.branches[].protection.required_pull_request_reviews' "$file")" = "{}" ]; then
yq e 'del(.branches[].protection.required_pull_request_reviews)' -i "$file"
fi
if [ -n "$(yq e '.branches[].protection.required_status_checks.contexts[] | select(test("Flowzone"))' "$file")" ]; then
# Append the repo to the list
yq e ".suborgrepos += [\"$(basename "$file" .yml)\"]" -i .github/suborgs/required_status_checks.flowzone.yml
fi
if [ -n "$(yq e '.branches[].protection.required_status_checks.contexts[] | select(test("^Jenkins build$"))' "$file")" ]; then
# Append the repo to the list
yq e ".suborgrepos += [\"$(basename "$file" .yml)\"]" -i .github/suborgs/required_status_checks.jenkins-build.yml
fi
if [ -n "$(yq e '.branches[].protection.required_status_checks.contexts[] | select(test("^Transformer Run$"))' "$file")" ]; then
# Append the repo to the list
yq e ".suborgrepos += [\"$(basename "$file" .yml)\"]" -i .github/suborgs/required_status_checks.transformers.yml
fi
# Use yq to delete the unwanted contexts and check if the contexts array is empty
yq e 'del(.branches[].protection.required_status_checks.contexts[] | select(. | test("^(ResinCI|VersionBot)")))' -i "$file"
yq e 'del(.branches[].protection.required_status_checks.contexts[] | select(. | test("policy-bot: ")))' -i "$file"
yq e 'del(.branches[].protection.required_status_checks.contexts[] | select(. | test("^Flowzone ")))' -i "$file"
yq e 'del(.branches[].protection.required_status_checks.contexts[] | select(. | test("^Jenkins build$")))' -i "$file"
yq e 'del(.branches[].protection.required_status_checks.contexts[] | select(. | test("^Transformer Run$")))' -i "$file"
if [ "$(yq e '.branches[].protection.required_status_checks.contexts | length' "$file")" = "0" ]; then
yq e 'del(.branches[].protection.required_status_checks.contexts)' -i "$file"
fi
if [ "$(yq e '.branches[].protection.required_status_checks' "$file")" = "{}" ]; then
yq e 'del(.branches[].protection.required_status_checks)' -i "$file"
fi
if [ "$(yq e '.branches[].protection' "$file")" = "{}" ]; then
yq e 'del(.branches)' -i "$file"
fi
if [ "$(yq e '.' "$file")" = "{}" ]; then
rm "$file"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment