|
| 1 | +name: Security Updates |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run every Monday at 2 AM UTC |
| 6 | + - cron: '0 2 * * 1' |
| 7 | + workflow_dispatch: # Allow manual trigger |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + security-scan: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + fetch-depth: 0 |
| 21 | + |
| 22 | + - name: Set up Python |
| 23 | + uses: actions/setup-python@v5 |
| 24 | + with: |
| 25 | + python-version: '3.12' |
| 26 | + |
| 27 | + - name: Install dependencies |
| 28 | + run: | |
| 29 | + python -m pip install --upgrade pip |
| 30 | + pip install -e . |
| 31 | + pip install pytest pip-audit safety |
| 32 | +
|
| 33 | + - name: Run security audit with pip-audit |
| 34 | + id: pip_audit |
| 35 | + continue-on-error: true |
| 36 | + run: | |
| 37 | + pip-audit --desc > security-report.txt 2>&1 || echo "vulnerabilities_found=true" >> $GITHUB_OUTPUT |
| 38 | + cat security-report.txt |
| 39 | +
|
| 40 | + - name: Check for vulnerabilities with Safety |
| 41 | + id: safety_check |
| 42 | + continue-on-error: true |
| 43 | + run: | |
| 44 | + safety check --json > safety-report.json 2>&1 || echo "safety_issues=true" >> $GITHUB_OUTPUT |
| 45 | +
|
| 46 | + - name: Attempt to fix vulnerabilities |
| 47 | + if: steps.pip_audit.outputs.vulnerabilities_found == 'true' || steps.safety_check.outputs.safety_issues == 'true' |
| 48 | + id: fix_vulnerabilities |
| 49 | + run: | |
| 50 | + # Upgrade all dependencies to latest secure versions |
| 51 | + pip list --outdated --format=json | python -c " |
| 52 | + import json, sys |
| 53 | + packages = json.load(sys.stdin) |
| 54 | + for pkg in packages: |
| 55 | + print(pkg['name']) |
| 56 | + " > outdated.txt |
| 57 | +
|
| 58 | + # Read requirements and upgrade |
| 59 | + if [ -f "requirements.txt" ]; then |
| 60 | + while IFS= read -r package; do |
| 61 | + if [ ! -z "$package" ]; then |
| 62 | + pip install --upgrade "$package" || true |
| 63 | + fi |
| 64 | + done < outdated.txt |
| 65 | +
|
| 66 | + # Freeze new versions |
| 67 | + pip freeze | grep -v "pyresolvers" > requirements-new.txt |
| 68 | + mv requirements-new.txt requirements.txt |
| 69 | +
|
| 70 | + echo "updated=true" >> $GITHUB_OUTPUT |
| 71 | + fi |
| 72 | +
|
| 73 | + - name: Run all tests |
| 74 | + if: steps.fix_vulnerabilities.outputs.updated == 'true' |
| 75 | + id: run_tests |
| 76 | + run: | |
| 77 | + pytest tests/test_validator.py -v --tb=short |
| 78 | + pytest tests/test_cli.py -v --tb=short |
| 79 | + echo "tests_passed=true" >> $GITHUB_OUTPUT |
| 80 | +
|
| 81 | + - name: Get current version |
| 82 | + if: steps.run_tests.outputs.tests_passed == 'true' |
| 83 | + id: get_version |
| 84 | + run: | |
| 85 | + VERSION=$(python -c "import re; content=open('pyresolvers/lib/core/__version__.py').read(); print(re.search(r\"'([^']+)'\", content).group(1))") |
| 86 | + echo "current_version=$VERSION" >> $GITHUB_OUTPUT |
| 87 | +
|
| 88 | + # Calculate new patch version |
| 89 | + IFS='.' read -ra PARTS <<< "$VERSION" |
| 90 | + MAJOR="${PARTS[0]}" |
| 91 | + MINOR="${PARTS[1]}" |
| 92 | + PATCH="${PARTS[2]}" |
| 93 | + NEW_PATCH=$((PATCH + 1)) |
| 94 | + NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH" |
| 95 | + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 96 | +
|
| 97 | + - name: Bump version |
| 98 | + if: steps.run_tests.outputs.tests_passed == 'true' |
| 99 | + run: | |
| 100 | + NEW_VERSION="${{ steps.get_version.outputs.new_version }}" |
| 101 | + echo "__version__ = '$NEW_VERSION'" > pyresolvers/lib/core/__version__.py |
| 102 | +
|
| 103 | + - name: Create security update branch |
| 104 | + if: steps.run_tests.outputs.tests_passed == 'true' |
| 105 | + run: | |
| 106 | + git config --local user.email "github-actions[bot]@users.noreply.github.com" |
| 107 | + git config --local user.name "github-actions[bot]" |
| 108 | +
|
| 109 | + BRANCH_NAME="security/auto-update-$(date +%Y%m%d)" |
| 110 | + git checkout -b $BRANCH_NAME |
| 111 | +
|
| 112 | + git add requirements.txt pyresolvers/lib/core/__version__.py |
| 113 | +
|
| 114 | + # Create detailed commit message |
| 115 | + echo "Security update v${{ steps.get_version.outputs.new_version }}" > commit-msg.txt |
| 116 | + echo "" >> commit-msg.txt |
| 117 | + echo "Automated security dependency updates:" >> commit-msg.txt |
| 118 | + echo "" >> commit-msg.txt |
| 119 | + cat security-report.txt >> commit-msg.txt || echo "No detailed report available" >> commit-msg.txt |
| 120 | + echo "" >> commit-msg.txt |
| 121 | + echo "All tests passed after updates." >> commit-msg.txt |
| 122 | +
|
| 123 | + git commit -F commit-msg.txt |
| 124 | + git push origin $BRANCH_NAME |
| 125 | +
|
| 126 | + echo "branch_name=$BRANCH_NAME" >> $GITHUB_ENV |
| 127 | +
|
| 128 | + - name: Create Pull Request |
| 129 | + if: steps.run_tests.outputs.tests_passed == 'true' |
| 130 | + env: |
| 131 | + GH_TOKEN: ${{ github.token }} |
| 132 | + run: | |
| 133 | + BRANCH_NAME="security/auto-update-$(date +%Y%m%d)" |
| 134 | + NEW_VERSION="${{ steps.get_version.outputs.new_version }}" |
| 135 | +
|
| 136 | + gh pr create \ |
| 137 | + --title "🔒 Security Update v$NEW_VERSION" \ |
| 138 | + --body "$(cat <<'EOF' |
| 139 | +## Automated Security Update |
| 140 | + |
| 141 | +This PR contains automated security dependency updates. |
| 142 | + |
| 143 | +### Changes |
| 144 | +- Updated vulnerable dependencies to secure versions |
| 145 | +- Bumped version to v$NEW_VERSION |
| 146 | +- All tests passed ✅ |
| 147 | + |
| 148 | +### Security Report |
| 149 | +\`\`\` |
| 150 | +$(cat security-report.txt) |
| 151 | +\`\`\` |
| 152 | + |
| 153 | +### Test Results |
| 154 | +- ✅ Unit tests passed |
| 155 | +- ✅ CLI tests passed |
| 156 | + |
| 157 | +### Next Steps |
| 158 | +1. Review the dependency changes |
| 159 | +2. Merge this PR to trigger release workflow |
| 160 | +3. A new release will be automatically published to PyPI |
| 161 | + |
| 162 | +--- |
| 163 | +🤖 This PR was automatically generated by the security workflow |
| 164 | +EOF |
| 165 | +)" \ |
| 166 | + --base master \ |
| 167 | + --head $BRANCH_NAME \ |
| 168 | + --label "security,automated" |
| 169 | + |
| 170 | + - name: Auto-merge if tests pass |
| 171 | + if: steps.run_tests.outputs.tests_passed == 'true' |
| 172 | + env: |
| 173 | + GH_TOKEN: ${{ github.token }} |
| 174 | + run: | |
| 175 | + BRANCH_NAME="security/auto-update-$(date +%Y%m%d)" |
| 176 | +
|
| 177 | + # Wait a moment for PR to be created |
| 178 | + sleep 5 |
| 179 | +
|
| 180 | + # Get PR number |
| 181 | + PR_NUMBER=$(gh pr list --head $BRANCH_NAME --json number --jq '.[0].number') |
| 182 | +
|
| 183 | + if [ ! -z "$PR_NUMBER" ]; then |
| 184 | + # Enable auto-merge |
| 185 | + gh pr merge $PR_NUMBER --auto --squash --delete-branch |
| 186 | +
|
| 187 | + echo "✅ Auto-merge enabled for PR #$PR_NUMBER" |
| 188 | + echo "PR will merge automatically once all checks pass" |
| 189 | + fi |
| 190 | +
|
| 191 | + - name: No vulnerabilities found |
| 192 | + if: steps.pip_audit.outputs.vulnerabilities_found != 'true' && steps.safety_check.outputs.safety_issues != 'true' |
| 193 | + run: | |
| 194 | + echo "✅ No security vulnerabilities found" |
| 195 | + echo "All dependencies are up to date and secure" |
0 commit comments