Repository navigation
Expand file tree
/
Copy pathautomation-create-pull-request.yaml
More file actions
110 lines (93 loc) · 3.85 KB
/
Copy pathautomation-create-pull-request.yaml
File metadata and controls
110 lines (93 loc) · 3.85 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
name: Generate Merge Log and Open Pull Request
on:
workflow_call:
inputs:
repository-name:
description: 'Repository name'
required: true
type: string
main-branch:
description: 'Main branch name'
required: true
type: string
develop-branch:
description: 'Develop branch name'
required: true
type: string
env:
GH_TOKEN: ${{ github.token }}
jobs:
generate-merge-log-and-open-pr:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v7
with:
fetch-depth: 0
- name: Fetch Unmerged PRs
id: fetch-unmerged-prs
run: |
git fetch --all
# Get all the Pull Request head commits
git fetch origin "+refs/pull/*/head:refs/remotes/origin/pr/*" &>/dev/null
# Generate a list of all PR's contained in the specified branches
git branch -r --merged origin/${{inputs.develop-branch}} | grep "origin/pr/*" | sort > inthis.txt
git branch -r --merged origin/${{inputs.main-branch}} | grep "origin/pr/*" | sort > notinthis.txt
# Clean up after getting all the PR head commits
git remote prune origin &>/dev/null
# Compare the lists of PR's contained in branches, and output result
comm -23 inthis.txt notinthis.txt | sed "s, origin/pr/,,g" > pr-numbers.txt
# Clean up temp files
rm inthis.txt
rm notinthis.txt
- name: Retrieve PR Info
id: retrieve-pr-info
run: |
while read -r pr_number; do
response=$(curl -s -o resp.json -H "Authorization: Bearer ${{ github.token }}" "https://api.github.com/repos/${{ inputs.repository-name }}/pulls/$pr_number")
if [[ $(cat resp.json | jq -r '.message') != "Not Found" ]]; then
state=$(cat resp.json | jq -r '.state')
merged=$(cat resp.json | jq -r '.merged')
if [[ $state == "closed" && $merged == "true" ]]; then
title=$(cat resp.json | jq -r '.title')
date=$(cat resp.json | jq -r '.merged_at')
user_handle=$(cat resp.json | jq -r '.user.login')
merge_commit_sha=$(cat resp.json | jq -r '.merge_commit_sha')
echo "#### $title (#$pr_number)"
echo "Merge commit SHA: $merge_commit_sha"
echo "Author: @$user_handle"
echo "Merged at: $date"
echo ""
fi
fi
done < pr-numbers.txt > prs.txt
rm pr-numbers.txt
- name: Fetch non-PR commits
id: fetch-nonpr-commits
run: |
git fetch --all
git log --no-merges --format="%s%nSHA: %H%nAuthor: %ae%nDate: %ad%n" --first-parent origin/${{ inputs.main-branch }}..origin/${{ inputs.develop-branch }} > commits.txt
- name: Create Body Text
id: create-body-text
run: |
touch body.txt
if [[ -s prs.txt ]]; then
echo "### Pull Requests" >> body.txt
cat prs.txt >> body.txt
echo >> body.txt
fi
if [[ -s commits.txt ]]; then
echo "### Non-PR commits" >> body.txt
cat commits.txt >> body.txt
echo >> body.txt
fi
if [[ ! -s body.txt ]]; then
echo "::warning::No changes found in ${{ inputs.develop-branch }} not found in ${{ inputs.main-branch }}. Skipping PR creation"
exit 0
fi
- name: Create PR
id: create-pr
run: |
if [[ -s body.txt ]]; then
gh pr create --title "Automated PR - Merging ${{ inputs.develop-branch }} into ${{ inputs.main-branch }}" -F body.txt -B ${{ inputs.main-branch }} -H ${{ inputs.develop-branch }}
fi