Skip to content

Commit 2e51416

Browse files
feat: action to create new PR for post (#48)
Signed-off-by: Tsung-Ju Lii <[email protected]>
1 parent 8de6884 commit 2e51416

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Create New Post PR
2+
description: Creates a new PR. The PR's body will be used to populate a new post in my blog.
3+
4+
on:
5+
workflow_dispatch:
6+
inputs:
7+
name:
8+
type: string
9+
required: true
10+
description: Name of the branch to create. It will be lowercased and all spaces will be replaced by dashes.
11+
12+
jobs:
13+
create-branch-and-pr:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout repository
18+
uses: actions/checkout@v4
19+
20+
- name: Create new branch
21+
id: create_branch
22+
run: |
23+
git config --global user.name 'github-actions[bot]'
24+
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
25+
branch_name=$(echo "${{ github.event.inputs.name }}" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
26+
echo "Branch name: ${branch_name}"
27+
git checkout -b "$branch_name"
28+
git push origin "$branch_name"
29+
echo "branch_name=$branch_name" >> $GITHUB_OUTPUT
30+
31+
- name: Create Pull Request
32+
id: create_pr
33+
uses: peter-evans/create-pull-request@v7
34+
with:
35+
token: ${{ secrets.GITHUB_TOKEN }}
36+
branch: ${{ steps.create_branch.outputs.branch_name }}
37+
base: main
38+
title: posts/${{ steps.create_branch.outputs.branch_name }}
39+
body-path: '${{ github.workspace }}/.github/PULL_REQUEST_TEMPLATE/post.md'
40+
labels: 'post'

.github/workflows/create_post_from_pr.yaml renamed to .github/workflows/generate_post_from_pr.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
name: Create Post from PR
1+
name: Generate post from PR
2+
description: When a PR that has label `post` is merged, this action will generate a new post from the PR's body.
23

34
on:
45
pull_request:
56
types: [labeled, closed]
67

78
jobs:
8-
create_post:
9+
generate_post:
910
if: ${{ (github.event.pull_request.merged == true && github.event.label.name == 'post') }}
1011
runs-on: ubuntu-latest
1112

@@ -16,7 +17,8 @@ jobs:
1617
- name: Create post file
1718
id: create_post
1819
run: |
19-
PR_TITLE=$(echo "${{ github.event.pull_request.title }}" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
20+
PR_TITLE=${{ github.event.pull_request.title }}
21+
PR_TITLE=${PR_TITLE#post/}
2022
DATE=$(date +'%Y-%m-%d')
2123
PR_TITLE="${DATE}-${PR_TITLE}"
2224
PR_BODY=${{ github.event.pull_request.body }}

0 commit comments

Comments
 (0)