-
Notifications
You must be signed in to change notification settings - Fork 0
150 lines (123 loc) · 4.7 KB
/
record-changeset.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
name: Record Changeset
on:
workflow_dispatch:
inputs:
version_type:
description: "Change type"
required: true
type: choice
options:
- patch
- minor
- major
default: "patch"
jobs:
changeset:
name: Create Changeset
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
- name: Set BASE_BRANCH
run: |
if [[ "${GITHUB_REF#refs/heads/}" == "staging"* ]]; then
echo "BASE_BRANCH=staging" >> $GITHUB_ENV
else
echo "BASE_BRANCH=main" >> $GITHUB_ENV
fi
- name: Sync Base Branch
run: |
git fetch origin ${{ env.BASE_BRANCH }}
git merge origin/${{ env.BASE_BRANCH }} --no-edit
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Install Dependencies
run: yarn install --frozen-lockfile
- name: Run Changeset Command
id: changeset
env:
BASE_BRANCH: ${{ env.BASE_BRANCH }}
run: |
echo "=== Starting Changeset Creation ==="
# Get and log commit message
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
echo "Commit Message: ${COMMIT_MESSAGE}"
echo "Base Branch: ${BASE_BRANCH}"
echo "=== Creating Empty Changeset ==="
# Create the changeset file first
CHANGESET_OUTPUT=$(npx changeset add --empty)
echo "Changeset Command Output: ${CHANGESET_OUTPUT}"
echo "=== Extracting Filename ==="
# Get the generated changeset filename
CHANGESET_FILE=$(echo "$CHANGESET_OUTPUT" | grep -o '[a-z0-9\-]\+\.md')
echo "Generated Changeset Filename: ${CHANGESET_FILE}"
echo "=== Checking .changeset Directory ==="
ls -la .changeset || echo "Directory not found!"
echo "=== Writing Changeset Content ==="
# Modify the changeset file to include the package
cat > ".changeset/${CHANGESET_FILE}" << EOF
---
"@avaprotocol/sdk-js": ${{ github.event.inputs.version_type }}
---
${COMMIT_MESSAGE}
EOF
echo "=== Verifying Changeset File Content ==="
if [ -f ".changeset/${CHANGESET_FILE}" ]; then
echo "Changeset file content:"
cat ".changeset/${CHANGESET_FILE}"
else
echo "Error: Changeset file was not created!"
fi
echo "=== Setting Output Variable ==="
echo "changeset_file=${CHANGESET_FILE}" >> $GITHUB_OUTPUT
echo "=== Final Directory Check ==="
# Verify changeset creation
if [ ! -d ".changeset" ]; then
echo "Error: .changeset directory not created"
exit 1
fi
echo "=== Staging and Committing Changeset ==="
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
echo "Staging changeset file..."
git add ".changeset/${CHANGESET_FILE}"
git status
echo "Committing changeset..."
git commit -m "Add changeset for: ${COMMIT_MESSAGE} [skip ci]"
echo "Verifying commit..."
git log -1 --stat
echo "=== Changeset Creation Complete ==="
# Add this line to set the commit message as an output
echo "commit_message=${COMMIT_MESSAGE}" >> $GITHUB_OUTPUT
- name: Commit and Push Changes
id: commit_and_push
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
# Create and checkout new branch using changeset filename
BRANCH_NAME="changeset-$(basename ${{ steps.changeset.outputs.changeset_file }} .md)"
git checkout -b "$BRANCH_NAME"
git push origin "$BRANCH_NAME"
# Set outputs for later use
echo "commit_message=${{ steps.changeset.outputs.commit_message }}" >> $GITHUB_ENV
echo "branch_name=${BRANCH_NAME}" >> $GITHUB_ENV
- name: Create Pull Request
env:
GH_TOKEN: ${{ github.token }}
COMMIT_MESSAGE: ${{ steps.changeset.outputs.commit_message }}
BRANCH_NAME: ${{ env.branch_name }}
BASE_BRANCH: ${{ env.BASE_BRANCH }}
run: |
gh pr create \
--title "Add changeset for ${COMMIT_MESSAGE}" \
--body "This pull request adds a changeset for ${COMMIT_MESSAGE}. Please review." \
--base "$BASE_BRANCH" \
--head "$BRANCH_NAME"
permissions:
contents: write
pull-requests: write