-
Notifications
You must be signed in to change notification settings - Fork 0
120 lines (108 loc) · 4.56 KB
/
Copy pathworkflow-create-release.yml
File metadata and controls
120 lines (108 loc) · 4.56 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
111
112
113
114
115
116
117
118
119
120
name: Create Release
on:
workflow_call:
inputs:
tag-name:
description: "Tag name to publish."
required: true
type: string
previous-tag:
description: "Previous tag used for changelog links."
required: false
default: v0.0.0
type: string
merged-prs:
description: "JSON array with merged pull request metadata."
required: false
default: "[]"
type: string
secrets:
gh_token:
description: "Token passed from caller (defaults to the built-in GITHUB_TOKEN)."
required: false
jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Create GitHub release
env:
GH_TOKEN: ${{ secrets.gh_token || github.token }}
TAG_NAME: ${{ inputs.tag-name }}
PREVIOUS_TAG: ${{ inputs.previous-tag }}
MERGED_PRS: ${{ inputs.merged-prs }}
run: |
set -euo pipefail
tmp_notes="$(mktemp)"
trap 'rm -f "$tmp_notes"' EXIT
tag="${TAG_NAME:-}"
previous_tag="${PREVIOUS_TAG:-}"
merged_json="${MERGED_PRS:-}"
if [ -z "$merged_json" ]; then
merged_json='[]'
fi
if [ "$merged_json" = "[]" ]; then
echo "No merged PRs provided; collecting merged PRs between ${previous_tag}...${tag}"
if [ -n "$previous_tag" ]; then
merged_entries=()
commit_messages=$(gh api "repos/${GITHUB_REPOSITORY}/compare/${previous_tag}...${tag}" --jq '.commits[].commit.message' 2>/dev/null || true)
pr_numbers=$(grep -oE '#[0-9]+' <<<"$commit_messages" | tr -d '#' | sort -u || true)
for pr_number in $pr_numbers; do
pr_json=$(gh pr view "$pr_number" -R "$GITHUB_REPOSITORY" --json number,title,author --jq '{number:.number,title:.title,author:(.author.login // "bot")}' 2>/dev/null || true)
if [ -n "$pr_json" ]; then
merged_entries+=("$pr_json")
fi
done
# Fallback: check PRs associated with commits if no PRs were found via messages
if [ "${#merged_entries[@]}" -eq 0 ]; then
commit_shas=$(gh api "repos/${GITHUB_REPOSITORY}/compare/${previous_tag}...${tag}" --jq '.commits[].sha' 2>/dev/null || true)
for sha in $commit_shas; do
pr_json_list=$(gh api "repos/${GITHUB_REPOSITORY}/commits/${sha}/pulls" --jq '.[] | {number:.number,title:.title,author:(.user.login // "bot")}' 2>/dev/null || true)
if [ -n "$pr_json_list" ]; then
while IFS= read -r pr_line; do
merged_entries+=("$pr_line")
done <<<"$pr_json_list"
fi
done
fi
if [ "${#merged_entries[@]}" -gt 0 ]; then
merged_json=$(printf '%s\n' "${merged_entries[@]}" | jq -s 'unique_by(.number)')
else
merged_json='[]'
fi
else
echo "No previous tag provided; skipping automatic PR collection."
fi
fi
if [ "$merged_json" = "[]" ]; then
echo "No merged PRs identified between ${previous_tag}...${tag}"
fi
changes=$(jq -r --arg repo "$GITHUB_REPOSITORY" '
if length == 0 then "" else
map("- " + (.title // ("Pull request #" + .number)) + " by @" + ((.author // "bot")) +
" in [#" + .number + "](https://github.com/" + $repo + "/pull/" + .number + ")")
| .[]
end' <<<"$merged_json" || true)
{
echo "## What's Changed"
if [ -n "$changes" ]; then
printf "%s\n" "$changes"
else
echo "- No pull requests were merged in this release."
fi
if [ -n "$previous_tag" ]; then
echo
echo "**Full Changelog:** [${previous_tag}...${tag}](https://github.com/${GITHUB_REPOSITORY}/compare/${previous_tag}...${tag})"
fi
} >"$tmp_notes"
echo "Creating release for $tag"
gh release create "$tag" -R "$GITHUB_REPOSITORY" --title "$tag" --notes-file "$tmp_notes"
echo "::notice title=Release created::${tag} (previous ${previous_tag})"
- name: Release summary
run: |
{
echo "### Release"
echo "- Tag: ${{ inputs.tag-name }}"
echo "- Previous tag: ${{ inputs.previous-tag }}"
} >> "$GITHUB_STEP_SUMMARY"