Skip to content

Commit

Permalink
Add CI step to create a issue from a PR
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Savian <[email protected]>
  • Loading branch information
vitorsavian committed Oct 22, 2024
1 parent 811de8b commit a195063
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/create-issue-from-pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Create linked issues for PRs without one

on:
pull_request:
types: [opened]

jobs:
create-issue:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write

steps:
- name: Update and install jq
run: |
sudo apt update
sudo apt install jq
- name: Setup env
run: |
echo "USER=${{ github.event.pull_request.user.login }}" >> $GITHUB_ENV
echo "ORG=k3s-io" >> $GITHUB_ENV
echo "REPO=k3s" >> $GITHUB_ENV
echo "TITLE=${{ github.event.pull_request.title }}" >> $GITHUB_ENV
- name: Check user membership
run: |
if gh api "orgs/${{ env.ORG }}/public_members/${{ env.USER }}" --silent; then
echo "${{ env.USER }} is a public member of ${{ env.ORG }}"
echo "MEMBER=true" >> $GITHUB_ENV
else
echo "${{ env.USER }} is not a public member of ${{ env.ORG }}"
echo "MEMBER=false" >> $GITHUB_ENV
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Check issue
if: env.MEMBER == 'false'
run: |
issue_links=$(echo "${{ github.event.pull_request.body }}" | grep -o 'https://github.com/${{ env.ORG }}/${{ env.REPO }}/issues/[0-9]\+' || true)
if [ -z "$issue_links" ]; then
echo "No linked issue found."
echo "ISSUE_EXISTS=false" >> $GITHUB_ENV
else
echo "Linked issue found: $issue_links"
echo "ISSUE_EXISTS=true" >> $GITHUB_ENV
echo "ISSUE_LINK=$issue_links" >> $GITHUB_ENV
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create an issue
if: (env.MEMBER == 'false') && (env.ISSUE_EXISTS == 'false')
run: |
new_issue=$(gh issue create --title "${{ env.TITLE }}" --body "${{ github.event.pull_request.body }}" --repo "${{ env.ORG }}/${{ env.REPO }}" 2>&1)
echo "Created a new issue: $new_issue"
echo "ISSUE_URL=$new_issue" >> $GITHUB_ENV
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Comment on PR with issue link
if: (env.MEMBER == 'false') && (env.ISSUE_EXISTS == 'false')
run: |
echo "${{ env.ISSUE_URL }}"
gh pr comment ${{ github.event.pull_request.number }} --body "Created a new issue to track the PR: ${{ env.ISSUE_URL }}" --repo "${{ env.ORG }}/${{ env.REPO }}"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit a195063

Please sign in to comment.