|
| 1 | +#!/usr/bin/env bash |
| 2 | +# SPDX-License-Identifier: PMPL-1.0-or-later |
| 3 | +# |
| 4 | +# fix-add-elixir-ci.sh — Add GitHub Actions CI workflow for Elixir/Phoenix projects |
| 5 | +# |
| 6 | +# Triggered when: mix.exs present, no CI workflow found running mix test. |
| 7 | +# |
| 8 | +# Generated workflow runs on ubuntu-latest with Erlang/OTP + Elixir via |
| 9 | +# erlef/setup-beam, SHA-pinned. Includes: |
| 10 | +# - mix deps.get |
| 11 | +# - mix compile --warnings-as-errors |
| 12 | +# - mix test |
| 13 | +# - mix credo (if credo dep detected) |
| 14 | +# - mix dialyzer (if dialyxir dep detected) |
| 15 | +# |
| 16 | +# Idempotent: skips if elixir.yml / mix.yml already exists. |
| 17 | +# Does NOT commit — dispatch-runner handles that. |
| 18 | +# |
| 19 | +# Usage: fix-add-elixir-ci.sh <repo-path> <finding-json> |
| 20 | + |
| 21 | +set -euo pipefail |
| 22 | + |
| 23 | +REPO_PATH="${1:?Usage: $0 <repo-path> <finding-json>}" |
| 24 | +FINDING_JSON="${2:?Missing finding JSON}" |
| 25 | + |
| 26 | +WORKFLOWS_DIR="${REPO_PATH}/.github/workflows" |
| 27 | + |
| 28 | +# Idempotency check |
| 29 | +for wf in elixir.yml mix.yml beam.yml elixir-ci.yml; do |
| 30 | + if [[ -f "${WORKFLOWS_DIR}/${wf}" ]]; then |
| 31 | + echo "[fix-add-elixir-ci] ${wf} already exists — skipping." |
| 32 | + exit 0 |
| 33 | + fi |
| 34 | +done |
| 35 | +# Also check for any workflow containing mix test |
| 36 | +if [[ -d "${WORKFLOWS_DIR}" ]] && grep -rl 'mix test' "${WORKFLOWS_DIR}/" >/dev/null 2>&1; then |
| 37 | + echo "[fix-add-elixir-ci] Workflow with 'mix test' already exists — skipping." |
| 38 | + exit 0 |
| 39 | +fi |
| 40 | + |
| 41 | +mkdir -p "${WORKFLOWS_DIR}" |
| 42 | + |
| 43 | +# Detect optional tooling from mix.exs |
| 44 | +MIX_EXS="${REPO_PATH}/mix.exs" |
| 45 | +HAS_CREDO=false |
| 46 | +HAS_DIALYXIR=false |
| 47 | +if [[ -f "${MIX_EXS}" ]]; then |
| 48 | + grep -q 'credo' "${MIX_EXS}" && HAS_CREDO=true || true |
| 49 | + grep -q 'dialyxir\|dialyzir' "${MIX_EXS}" && HAS_DIALYXIR=true || true |
| 50 | +fi |
| 51 | + |
| 52 | +# Detect OTP/Elixir version hints from .tool-versions or mix.exs |
| 53 | +OTP_VERSION="27" |
| 54 | +ELIXIR_VERSION="1.17" |
| 55 | +if [[ -f "${REPO_PATH}/.tool-versions" ]]; then |
| 56 | + otp_line=$(grep '^erlang ' "${REPO_PATH}/.tool-versions" | awk '{print $2}' | cut -d. -f1 || true) |
| 57 | + elixir_line=$(grep '^elixir ' "${REPO_PATH}/.tool-versions" | awk '{print $2}' | cut -d- -f1 || true) |
| 58 | + [[ -n "${otp_line}" ]] && OTP_VERSION="${otp_line}" |
| 59 | + [[ -n "${elixir_line}" ]] && ELIXIR_VERSION="${elixir_line}" |
| 60 | +fi |
| 61 | + |
| 62 | +# Build optional steps |
| 63 | +OPTIONAL_STEPS="" |
| 64 | +if [[ "${HAS_CREDO}" == "true" ]]; then |
| 65 | + OPTIONAL_STEPS="${OPTIONAL_STEPS} |
| 66 | + - name: Credo static analysis |
| 67 | + run: mix credo --strict |
| 68 | +" |
| 69 | +fi |
| 70 | +if [[ "${HAS_DIALYXIR}" == "true" ]]; then |
| 71 | + OPTIONAL_STEPS="${OPTIONAL_STEPS} |
| 72 | + - name: Dialyzer type checking |
| 73 | + run: mix dialyzer |
| 74 | +" |
| 75 | +fi |
| 76 | + |
| 77 | +cat > "${WORKFLOWS_DIR}/elixir.yml" << WORKFLOW |
| 78 | +# SPDX-License-Identifier: PMPL-1.0-or-later |
| 79 | +name: Elixir CI |
| 80 | +
|
| 81 | +on: |
| 82 | + push: |
| 83 | + branches: [main, master] |
| 84 | + pull_request: |
| 85 | + branches: [main, master] |
| 86 | +
|
| 87 | +permissions: |
| 88 | + contents: read |
| 89 | +
|
| 90 | +jobs: |
| 91 | + test: |
| 92 | + name: Build and test |
| 93 | + runs-on: ubuntu-latest |
| 94 | +
|
| 95 | + strategy: |
| 96 | + matrix: |
| 97 | + otp: ['${OTP_VERSION}'] |
| 98 | + elixir: ['${ELIXIR_VERSION}'] |
| 99 | +
|
| 100 | + steps: |
| 101 | + - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 |
| 102 | +
|
| 103 | + - name: Set up Elixir |
| 104 | + uses: erlef/setup-beam@5304e04ea2b355f03681464e683d92e3b2f18451 # v1 |
| 105 | + with: |
| 106 | + otp-version: \${{ matrix.otp }} |
| 107 | + elixir-version: \${{ matrix.elixir }} |
| 108 | +
|
| 109 | + - name: Restore dependencies cache |
| 110 | + uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4 |
| 111 | + with: |
| 112 | + path: deps |
| 113 | + key: \${{ runner.os }}-mix-\${{ hashFiles('**/mix.lock') }} |
| 114 | + restore-keys: \${{ runner.os }}-mix- |
| 115 | +
|
| 116 | + - name: Install dependencies |
| 117 | + run: mix deps.get |
| 118 | +
|
| 119 | + - name: Compile (warnings as errors) |
| 120 | + run: mix compile --warnings-as-errors |
| 121 | +${OPTIONAL_STEPS} |
| 122 | + - name: Run tests |
| 123 | + run: mix test |
| 124 | +WORKFLOW |
| 125 | + |
| 126 | +echo "[fix-add-elixir-ci] Created ${WORKFLOWS_DIR}/elixir.yml" |
| 127 | +echo " OTP ${OTP_VERSION} / Elixir ${ELIXIR_VERSION}" |
| 128 | +[[ "${HAS_CREDO}" == "true" ]] && echo " + credo step" || true |
| 129 | +[[ "${HAS_DIALYXIR}" == "true" ]] && echo " + dialyzer step" || true |
0 commit comments