-
Notifications
You must be signed in to change notification settings - Fork 0
86 lines (74 loc) · 2.86 KB
/
pr-on-branch-push.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
name: Pull Request on Push to branch
on:
push:
branches-ignore:
- main
- dev
jobs:
create-pull-request:
runs-on: ubuntu-latest
permissions:
issues: read
contents: read
pull-requests: write
steps:
- name: Check out repository code
uses: actions/checkout@v2
with:
fetch-depth: 0
ref: dev
- name: Check for branch changes
id: check_branch_changes
run: |
NEW_BRANCH_SHA=$(git rev-parse "${{ github.sha }}")
echo "This is the new branch sha: $NEW_BRANCH_SHA"
DEV_BRANCH_SHA=$(git rev-parse "refs/heads/dev")
echo "This is the new branch sha: $DEV_BRANCH_SHA"
if [[ "${NEW_BRANCH_SHA}" == "${DEV_BRANCH_SHA}" ]]; then
echo "::set-output name=no_differences::true"
echo "No differences"
else
echo "::set-output name=no_differences::false"
echo "There are differences"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create PR
if: steps.check_branch_changes.outputs.no_differences == 'false'
run: |
echo "Get the list of PR for that branch that are open"
gh pr list -H ${{ github.ref_name}} -s "open"
PR_COUNT=$(gh pr list -H ${{ github.ref_name}} -s "open" | wc -l)
echo $PR_COUNT
echo "Checking if there are any PR already"
if [[ $PR_COUNT -gt 0 ]]; then
echo "Indeed there is at least one PR for this branch"
else
echo "There is no PR for this branch so we create one"
gh pr create -B $BASE_BRANCH -H $BRANCH_TO_MERGE --title "$PR_TITLE" --body "$PR_BODY"
fi
env:
GITHUB_TOKEN: ${{ secrets.GIT_WORKFLOW_TEST }}
PR_TITLE: "Autocreated PR: ${{ github.ref_name}}"
PR_BODY: "Created by Github action"
BASE_BRANCH: "dev"
BRANCH_TO_MERGE: ${{ github.ref}}
- name: Get PR number
if: steps.check_branch_changes.outputs.no_differences == 'false'
id: get_pr_number
run: |
PR_NUMBER=$(gh pr view ${{ github.ref_name }} --json number --jq '.number')
echo "PR Number: $PR_NUMBER"
echo "::set-output name=pr_number::$PR_NUMBER"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get the pull request data
if: steps.check_branch_changes.outputs.no_differences == 'false'
run: |
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/ines-gimeno-molina/workflows-test/pulls/$PR_NUMBER | jq -r '.issue_url'
env:
PR_NUMBER: ${{ steps.get_pr_number.outputs.pr_number}}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}