Skip to content

Test: testing the pr comment reply #7

Test: testing the pr comment reply

Test: testing the pr comment reply #7

Workflow file for this run

name: Convert Issue to Pull Request
on:
issues:
types: [opened]
jobs:
create-pr:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
contents: write
steps:
- uses: actions/checkout@v3
- name: Check if issue creator is code owner
id: check_owner
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE_CREATOR: ${{ github.event.issue.user.login }}
run: |
# Function to extract usernames/teams from CODEOWNERS file
extract_owners() {
if [ -f "$1" ]; then
# Extract all owners, removing comments and empty lines
# Convert @org/team to @team for simplification
grep -v '^#' "$1" | grep -v '^$' | \
sed 's/@[^/]*\//@/' | \
grep -o '@[^ ]*' | sort -u | tr -d '@'
fi
}
# Check various CODEOWNERS locations
OWNERS=""
for location in ".github/CODEOWNERS" "docs/CODEOWNERS" "CODEOWNERS"; do
OWNERS="$OWNERS $(extract_owners $location)"
done
# If no CODEOWNERS file found, fail the check
if [ -z "$OWNERS" ]; then
echo "No CODEOWNERS file found"
echo "is_owner=false" >> $GITHUB_OUTPUT
exit 0
fi
# Check if issue creator is in the owners list
if echo "$OWNERS" | grep -q "\b${ISSUE_CREATOR}\b"; then
echo "Issue creator is a code owner"
echo "is_owner=true" >> $GITHUB_OUTPUT
else
echo "Issue creator is not a code owner"
echo "is_owner=false" >> $GITHUB_OUTPUT
fi
- name: Comment on issue if not code owner
if: steps.check_owner.outputs.is_owner == 'false'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE_NUMBER: ${{ github.event.issue.number }}
run: |
gh issue comment $ISSUE_NUMBER --body "Only code owners can automatically create PRs from issues."
- name: Create Branch and PR
if: steps.check_owner.outputs.is_owner == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Create and checkout new branch based on issue number
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
BRANCH_NAME="issue-${{ github.event.issue.number }}"
git checkout -b $BRANCH_NAME
# Create empty commit to initialize branch
git commit --allow-empty -m "Initialize PR for issue #${{ github.event.issue.number }}"
git push origin $BRANCH_NAME
# Create PR using GitHub CLI
gh pr create \
--title "${{ github.event.issue.title }}" \
--body "${{ github.event.issue.body }}" \
--base main \
--head $BRANCH_NAME