Skip to content

Commit edb8230

Browse files
committed
build: add pr auto upsert
1 parent 18d5c6c commit edb8230

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: Auto Upsert Release PR
2+
3+
on:
4+
# trigger on push to dev branch
5+
push:
6+
branches:
7+
- dev
8+
# trigger on manual workflow_dispatch
9+
workflow_dispatch:
10+
11+
concurrency:
12+
group: pr-upsert-${{ github.ref }}
13+
cancel-in-progress: true
14+
15+
permissions:
16+
contents: write
17+
pull-requests: write
18+
19+
jobs:
20+
pr-upsert:
21+
name: Upsert PR
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: actions/checkout@v3
25+
with:
26+
ref: "dev"
27+
# fetch all history so that git log can get all commit messages
28+
fetch-depth: 0
29+
30+
# create a PR from dev to main, with title in form: Release <semver>
31+
# where, <semver> is the next version number to be released, based on the last release in git tag
32+
- name: Upsert PR Content
33+
uses: actions/github-script@v6
34+
with:
35+
github-token: ${{ github.token }}
36+
script: |
37+
const prTitle = "Release to Production"
38+
let body = `> *This PR is automatically created by actions defined in this repository. To see the run log of this action, please click [here](/${{ github.repository }}/actions/runs/${{ github.run_id }})*`
39+
const existedPR = await github.rest.pulls.list({
40+
owner: context.repo.owner,
41+
repo: context.repo.repo,
42+
state: 'open',
43+
head: `${context.repo.owner}:dev`,
44+
base: 'main'
45+
})
46+
if (existedPR.data.length > 0) {
47+
core.info(`PR already exists: ${existedPR.data[0].html_url}. Updating body...`)
48+
await github.rest.pulls.update({
49+
owner: context.repo.owner,
50+
repo: context.repo.repo,
51+
pull_number: existedPR.data[0].number,
52+
body: body
53+
})
54+
core.info(`PR updated: ${existedPR.data[0].html_url}`)
55+
return
56+
}
57+
const pr = await github.rest.pulls.create({
58+
owner: context.repo.owner,
59+
repo: context.repo.repo,
60+
title: prTitle,
61+
body: body,
62+
head: context.ref,
63+
base: 'main',
64+
draft: true
65+
})

0 commit comments

Comments
 (0)