Add write permissions for dependabot towncrier. #10
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Dependabot Towncrier | |
on: | |
push: | |
branches: | |
- "dependabot/**" | |
jobs: | |
dependabot-towncrier: | |
name: Change log fragment | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- uses: actions/checkout@v2 | |
with: | |
fetch-depth: 1 | |
- name: Fetch the main branch | |
run: git fetch origin main --depth=1 | |
- uses: actions/setup-python@v5 | |
with: | |
python-version: "3.13" | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install towncrier | |
- name: Configure git | |
run: | | |
git config --local user.email "$(git log --pretty='%ae' -1)" | |
git config --local user.name "Dependabot[bot]" | |
- name: Generate Fragment if Missing | |
id: generate_fragment # ID for referencing outputs | |
run: | | |
FRAGMENT_DIR="changes" | |
# Get branch name from the PR head ref and sanitize it | |
branch_name="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" # e.g., dependabot/pip/requests-2.28.1 | |
sanitized_name=$(echo "$branch_name" | sed 's|/|_|g') # Replaces all '/' with '_' | |
fragment_name="${sanitized_name}.misc.rst" # Construct fragment name, using .misc type | |
fragment_path="${FRAGMENT_DIR}/${fragment_name}" | |
# Get the first line of the commit message from the PR's head commit | |
commit_sha="git merge-base ${{ github.base_ref}} ${{ github.head_ref}}" | |
commit_message=$(git show -s --format=%B "$commit_sha" | head -1) | |
commit_message=$(git show -s --format=%B ${{ github.event.pull_request.head.sha }} | head -1) | |
echo "Branch name: $branch_name" | |
echo "Sanitized name: $sanitized_name" | |
echo "Expected fragment path: $fragment_path" | |
echo "Commit message: $commit_message" | |
created_fragment="false" | |
echo "Running towncrier check..." | |
if towncrier check; then | |
echo "Towncrier check passed: News fragment found or not required." | |
else | |
echo "Towncrier check failed (exit code $?): News fragment potentially missing." | |
# Check if the specific fragment file we would create *already exists* | |
if [[ -f "$fragment_path" ]]; then | |
echo "Fragment file '$fragment_path' already exists. Skipping creation." | |
else | |
echo "Fragment file '$fragment_path' does not exist. Creating..." | |
# Create the fragment since check failed AND file doesn't exist | |
towncrier create "$fragment_name" -c "$commit_message" | |
# Check if create command actually created the file (basic check) | |
if [[ -f "$fragment_path" ]]; then | |
echo "News fragment successfully created at '$fragment_path'." | |
created_fragment="true" | |
else | |
echo "ERROR: towncrier create command ran but fragment '$fragment_path' was not found!" | |
# Optionally fail the job here: exit 1 | |
fi | |
fi | |
fi | |
# Set the output variable for the next step | |
echo "fragment_created=${created_fragment}" >> $GITHUB_OUTPUT | |
- name: Commit and Push Fragment | |
if: steps.generate_fragment.outputs.fragment_created == 'true' | |
run: | | |
FRAGMENT_DIR="changes" | |
branch_name="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" | |
sanitized_name=$(echo "$branch_name" | sed 's|/|_|g') | |
fragment_name="${sanitized_name}.misc.rst" | |
fragment_path="${FRAGMENT_DIR}/${fragment_name}" | |
echo "Adding fragment: ${fragment_path}" | |
git add "${fragment_path}" | |
echo "Committing fragment..." | |
git commit -m "chore: Add news fragment for Dependabot update" -m "[dependabot skip]" -m "[skip ci]" | |
echo "Pushing changes to ${{ github.event.pull_request.head.ref }}..." | |
# Push the commit back to the PR branch (origin is set by actions/checkout) | |
git push origin ${{ github.event.pull_request.head.ref }} |