forked from amplication/amplication
-
Notifications
You must be signed in to change notification settings - Fork 0
87 lines (79 loc) · 3.07 KB
/
fork-to-branch.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
name: Fork to Branch
on:
issue_comment:
types: [created]
jobs:
check-comment:
runs-on: ubuntu-latest
if: ${{ github.event.issue.pull_request }}
outputs:
trigger-comment: ${{ steps.check.outputs.triggered }}
steps:
- uses: khan/[email protected]
id: check
with:
trigger: "create branch from fork"
prefix_only: "true"
create_branch:
runs-on: ubuntu-latest
needs: [check-comment]
if: ${{ needs.check-comment.outputs.trigger-comment == 'true' }}
steps:
- uses: actions/github-script@v3
id: get-pr
with:
script: |
const request = {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
}
core.info(`Getting PR #${request.pull_number} from ${request.owner}/${request.repo}`)
try {
const result = await github.pulls.get(request)
return result.data
} catch (err) {
core.setFailed(`Request failed with error ${err}`)
}
- name: checkout repository
uses: actions/checkout@v2
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ fromJSON(steps.get-pr.outputs.result).head.ref }}
repository: ${{ fromJSON(steps.get-pr.outputs.result).head.repo.full_name }}
- name: check if branch exists
env:
REF: ${{ fromJSON(steps.get-pr.outputs.result).head.ref }}
PR_NUMBER: ${{ github.event.issue.number }}
run: |
existed_in_repo=$(git ls-remote --heads origin pr/${{ env.REF }}-${{ env.PR_NUMBER }})
if [[ -z ${existed_in_repo} ]]
then
echo "branch pr/${{ env.REF }}-${{ env.PR_NUMBER }} is not exists"
else
git push origin --delete pr/${{ env.REF }}-${{ env.PR_NUMBER }}
echo "branch pr/${{ env.REF }}-${{ env.PR_NUMBER }} deleted"
fi
- name: git remote add upstream
env:
OWNER: ${{ fromJSON(steps.get-pr.outputs.result).head.repo.owner.login }}
REF: ${{ fromJSON(steps.get-pr.outputs.result).head.ref }}
PR_NUMBER: ${{ github.event.issue.number }}
run: |
git remote add upstream https://github.com/amplication/amplication.git
git fetch --all
git checkout ${{ env.REF }}
git checkout -b pr/${{ env.REF }}-${{ env.PR_NUMBER }}
git push -f --set-upstream upstream pr/${{ env.REF }}-${{ env.PR_NUMBER }}
echo "Forked branch ${{ env.OWNER }}:${{ env.REF }} has been pushed to branch pr/${{ env.REF }}-${{ env.PR_NUMBER }}"
- name: create comment
uses: peter-evans/create-or-update-comment@v2
env:
REF: ${{ fromJSON(steps.get-pr.outputs.result).head.ref }}
PR_NUMBER: ${{ github.event.issue.number }}
with:
issue-number: ${{ env.PR_NUMBER }}
body: |
The branch 'pr/${{ env.REF }}-${{ env.PR_NUMBER }}' was created!
reactions: "+1"