Skip to content

Commit

Permalink
chore: Support recreating PRs with simple comments (#48)
Browse files Browse the repository at this point in the history
* new workflow

* diff

* bash shell

* cancel in progress

* add log

* remove it flag

* sed help

* gha safe

* go back one commit to test

* config

* small name change

* further simplification

* create output

* generation

* base

* generation flow

* improvement

* another improvement

* generation improvements

* remove pr trigger

* generate slates too

* recreate on recreate
  • Loading branch information
lucaspopp-wbd authored Jun 11, 2024
1 parent ca13baf commit 0fdc785
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
20 changes: 18 additions & 2 deletions .github/workflows/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ name: generate

on:
workflow_dispatch:
workflow_call:
inputs:
pr-base:
required: true
type: string
outputs:
pr-number:
description: pr number
value: ${{ jobs.generate.outputs.pr-number }}

permissions:
actions: read
Expand All @@ -20,6 +29,8 @@ jobs:

generate:
runs-on: ubuntu-latest
outputs:
pr-number: ${{ steps.create-pr.outputs.pull-request-number }}
steps:

- uses: actions/checkout@v4
Expand All @@ -29,14 +40,18 @@ jobs:
- name: Determine base branch name
id: pr-base
run: |
echo "name=${{ github.ref_name }}" | tee -a "$GITHUB_OUTPUT"
if [[ "${{ github.event_name }}" = "issue_comment" ]]; then
echo "name=${{ inputs.pr-base }}" | tee -a "$GITHUB_OUTPUT"
else
echo "name=${{ github.ref_name }}" | tee -a "$GITHUB_OUTPUT"
fi
- name: Determine head branch name
id: pr-head
env:
BASE: ${{ steps.pr-base.outputs.name }}
run: |
DATE=$(date '+%Y-%m-%d')
DATE=$(date '+%s')
echo "name=gen/$BASE/$DATE" | tee -a "$GITHUB_OUTPUT"
- name: Re-generate isp
Expand All @@ -61,6 +76,7 @@ jobs:
fi
- name: Create Pull Request
id: create-pr
if: steps.changes.outputs.should-commit == 'true'
uses: peter-evans/create-pull-request@v6
with:
Expand Down
59 changes: 59 additions & 0 deletions .github/workflows/pr-comment-handler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: pr-comment-handler

on:
issue_comment:
types: [created, edited]

permissions:
contents: write
pull-requests: write

defaults:
run:
shell: bash

jobs:

handle-comment:
runs-on: ubuntu-latest
if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, 'recreate-me') }}
outputs:
pr-head: ${{ steps.pr-details.outputs.head }}
steps:

- uses: actions/checkout@v4

- name: Determine PR details
id: pr-details
run: |
details=$(gh pr view ${{ github.event.issue.number }} --json 'baseRefName,headRefName' 2>&1)
headRefName=$(printf '%s' "$details" | jq -rc '.headRefName')
baseRefName=$(printf '%s' "$details" | jq -rc '.baseRefName')
if [[ "$headRefName" = "null" ]] || [[ "$baseRefName" = "null" ]]; then
echo "::error::Failed to parse pr details"
echo "$details"
exit 1
fi
echo "head=$headRefName" | tee -a "$GITHUB_OUTPUT"
echo "base=$baseRefName" | tee -a "$GITHUB_OUTPUT"
create-new:
needs: [handle-comment]
uses: ./.github/workflows/generate.yml
with:
pr-base: ${{ needs.handle-comment.outputs.pr-head }}
secrets: inherit

close-old:
needs: [handle-comment, create-new]
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v4

- name: Close old PR
run: |
gh pr close ${{ github.event.issue.number }} \
-c "Re-created as #${{ needs.create-new.outputs.pr-number }}"

0 comments on commit 0fdc785

Please sign in to comment.