|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -o errexit |
| 4 | +set -o nounset |
| 5 | +set -o pipefail |
| 6 | + |
| 7 | +UPSTREAM=https://github.com/ddev/ddev-addon-template/blob/main |
| 8 | + |
| 9 | +# List to store actions |
| 10 | +actions=() |
| 11 | + |
| 12 | +# Check for unnecessary files and suggest removal |
| 13 | +check_remove_file() { |
| 14 | + local file=$1 |
| 15 | + if [[ -f "$file" ]]; then |
| 16 | + actions+=("Remove unnecessary file: $file") |
| 17 | + fi |
| 18 | +} |
| 19 | + |
| 20 | +# Check README.md for required conditions |
| 21 | +check_readme() { |
| 22 | + local readme="README.md" |
| 23 | + local badge |
| 24 | + |
| 25 | + if [[ -f "$readme" ]]; then |
| 26 | + # Check for 'ddev add-on get' |
| 27 | + if ! grep -q "ddev add-on get" "$readme"; then |
| 28 | + actions+=("README.md should contain 'ddev add-on get', see upstream file $UPSTREAM/README_ADDON.md?plain=1") |
| 29 | + fi |
| 30 | + |
| 31 | + # Check for 'ddev get' |
| 32 | + if grep -q "ddev get" "$readme"; then |
| 33 | + actions+=("Remove 'ddev get' from README.md, see upstream file $UPSTREAM/README_ADDON.md?plain=1") |
| 34 | + fi |
| 35 | + |
| 36 | + # Check for required badges and replacements |
| 37 | + if grep -q "project is maintained" "$readme"; then |
| 38 | + actions+=("README.md should not contain 'project is maintained' badge, see upstream file $UPSTREAM/README_ADDON.md?plain=1") |
| 39 | + fi |
| 40 | + |
| 41 | + # Ensure the required badges are present |
| 42 | + for badge in "add-on registry" "tests" "last commit" "release"; do |
| 43 | + if ! grep -q "$badge" "$readme"; then |
| 44 | + actions+=("README.md should contain badge: $badge, see upstream file $UPSTREAM/README_ADDON.md?plain=1") |
| 45 | + fi |
| 46 | + done |
| 47 | + else |
| 48 | + actions+=("README.md is missing, see upstream file $UPSTREAM/README_ADDON.md?plain=1") |
| 49 | + fi |
| 50 | +} |
| 51 | + |
| 52 | +# Check install.yaml for required conditions |
| 53 | +check_install_yaml() { |
| 54 | + local install_yaml="install.yaml" |
| 55 | + |
| 56 | + if [[ -f "$install_yaml" ]]; then |
| 57 | + # Check for ddev_version_constraint |
| 58 | + if ! grep -q "ddev_version_constraint: '>= v1.24.3'" "$install_yaml"; then |
| 59 | + actions+=("install.yaml should contain 'ddev_version_constraint: \">= v1.24.3\"', see upstream file $UPSTREAM/$install_yaml") |
| 60 | + fi |
| 61 | + |
| 62 | + # Check for addon-template |
| 63 | + if grep -q "addon-template" "$install_yaml"; then |
| 64 | + actions+=("install.yaml should not contain 'addon-template', use your own name") |
| 65 | + fi |
| 66 | + else |
| 67 | + actions+=("install.yaml is missing, see upstream file $UPSTREAM/$install_yaml") |
| 68 | + fi |
| 69 | +} |
| 70 | + |
| 71 | +# Check tests/test.bats for required conditions |
| 72 | +check_test_bats() { |
| 73 | + local test_bats="tests/test.bats" |
| 74 | + |
| 75 | + if [[ -f "$test_bats" ]]; then |
| 76 | + # Check for test_tags=release |
| 77 | + if grep -q "install from release" "$test_bats" && ! grep -q "# bats test_tags=release" "$test_bats"; then |
| 78 | + actions+=("$test_bats should contain '# bats test_tags=release', see upstream file $UPSTREAM/$test_bats") |
| 79 | + fi |
| 80 | + |
| 81 | + # Check for ddev add-on get |
| 82 | + if ! grep -q "ddev add-on get" "$test_bats"; then |
| 83 | + actions+=("$test_bats should contain 'ddev add-on get', see upstream file $UPSTREAM/$test_bats") |
| 84 | + fi |
| 85 | + else |
| 86 | + actions+=("$test_bats is missing, see upstream file $UPSTREAM/$test_bats") |
| 87 | + fi |
| 88 | +} |
| 89 | + |
| 90 | +# Check for correct shebang in commands/**/* files |
| 91 | +check_shebang() { |
| 92 | + local file |
| 93 | + while IFS= read -r -d '' file; do |
| 94 | + if [[ -f "$file" && -r "$file" ]]; then |
| 95 | + local first_line |
| 96 | + first_line=$(head -n1 "$file" 2>/dev/null || echo "") |
| 97 | + if [[ "$first_line" == "#!/bin/bash" ]]; then |
| 98 | + actions+=("$file should use '#!/usr/bin/env bash' instead of '#!/bin/bash'") |
| 99 | + fi |
| 100 | + fi |
| 101 | + done < <(find commands -type f -print0 2>/dev/null || true) |
| 102 | +} |
| 103 | + |
| 104 | +# Check .github/workflows/tests.yml for required conditions |
| 105 | +check_tests_workflow() { |
| 106 | + local tests_yml=".github/workflows/tests.yml" |
| 107 | + |
| 108 | + if [[ -f "$tests_yml" ]]; then |
| 109 | + # Check for ddev/github-action-add-on-test@v2 |
| 110 | + if ! grep -q "ddev/github-action-add-on-test@v2" "$tests_yml"; then |
| 111 | + actions+=("$tests_yml should use 'ddev/github-action-add-on-test@v2', see upstream file $UPSTREAM/$tests_yml") |
| 112 | + fi |
| 113 | + else |
| 114 | + actions+=("$tests_yml is missing, see upstream file $UPSTREAM/$tests_yml") |
| 115 | + fi |
| 116 | +} |
| 117 | + |
| 118 | +# Check for required GitHub template files |
| 119 | +check_github_templates() { |
| 120 | + local templates=( |
| 121 | + ".github/ISSUE_TEMPLATE/bug_report.yml" |
| 122 | + ".github/ISSUE_TEMPLATE/feature_request.yml" |
| 123 | + ".github/PULL_REQUEST_TEMPLATE.md" |
| 124 | + ) |
| 125 | + local template |
| 126 | + |
| 127 | + for template in "${templates[@]}"; do |
| 128 | + if [[ ! -f "$template" ]]; then |
| 129 | + actions+=("GitHub template missing: $template, see upstream file $UPSTREAM/$template?plain=1") |
| 130 | + fi |
| 131 | + done |
| 132 | + |
| 133 | + # Check PULL_REQUEST_TEMPLATE.md for the forbidden exact link |
| 134 | + local pr_template=".github/PULL_REQUEST_TEMPLATE.md" |
| 135 | + if [[ -f "$pr_template" ]]; then |
| 136 | + if grep -q "https://github.com/<user>/<repo>/tarball/<branch>" "$pr_template"; then |
| 137 | + actions+=("PULL_REQUEST_TEMPLATE.md should not contain 'https://github.com/<user>/<repo>/tarball/<branch>', see upstream file $UPSTREAM/$pr_template?plain=1") |
| 138 | + fi |
| 139 | + fi |
| 140 | +} |
| 141 | + |
| 142 | +# Main function |
| 143 | +main() { |
| 144 | + if [[ ! -f "install.yaml" ]]; then |
| 145 | + echo "ERROR: run this script from the add-on root directory, install.yaml is missing" >&2 |
| 146 | + exit 1 |
| 147 | + fi |
| 148 | + |
| 149 | + # Check unnecessary files |
| 150 | + check_remove_file "README_DEBUG.md" |
| 151 | + check_remove_file "images/gh-tmate.jpg" |
| 152 | + check_remove_file "images/template--button.png" |
| 153 | + check_remove_file "docker-compose.addon-template.yaml" |
| 154 | + |
| 155 | + # Check README.md for conditions |
| 156 | + check_readme |
| 157 | + |
| 158 | + # Check install.yaml for conditions |
| 159 | + check_install_yaml |
| 160 | + |
| 161 | + # Check tests/test.bats for conditions |
| 162 | + check_test_bats |
| 163 | + |
| 164 | + # Check shebang in commands/**/* files |
| 165 | + check_shebang |
| 166 | + |
| 167 | + # Check tests workflow |
| 168 | + check_tests_workflow |
| 169 | + |
| 170 | + # Check GitHub templates |
| 171 | + check_github_templates |
| 172 | + |
| 173 | + # If any actions are needed, throw an error |
| 174 | + if [[ ${#actions[@]} -gt 0 ]]; then |
| 175 | + echo "ERROR: Actions needed:" >&2 |
| 176 | + local action |
| 177 | + for action in "${actions[@]}"; do |
| 178 | + echo "- $action" >&2 |
| 179 | + done |
| 180 | + exit 1 |
| 181 | + else |
| 182 | + echo "All checks passed, no actions needed." |
| 183 | + fi |
| 184 | +} |
| 185 | + |
| 186 | +# Run the main function |
| 187 | +main |
0 commit comments