Skip to content

Commit c7bc74e

Browse files
committed
test - client generation action
1 parent 27be5b4 commit c7bc74e

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
name: 'Generate Client'
2+
3+
description: |-
4+
Runs the client generator in 'elastic-client-generator-java' and pushes changes to an upstream branch in the 'elasticsearch-java' client repository.
5+
6+
The 'branch' used to run this action specifies the branch the 'elasticsearch-specification'
7+
branch and the target branch for the resulting Pull Request in the 'elasticsearch-java' repository.
8+
9+
If the 'target_branch' input is set, all changes will be pushed to the specified branch in the 'elasticsearch-java'
10+
repository and no Pull Request is created.
11+
Changes already present in that branch are pulled before running the client generator.
12+
13+
on:
14+
workflow_dispatch:
15+
inputs:
16+
target_branch:
17+
description: >-
18+
Target branch in the 'elasticsearch-java' repository. Leave empty to create a new branch and PR.
19+
required: false
20+
default: ''
21+
type: string
22+
dry_run:
23+
description: >-
24+
Perform a dry run. Don't push changes to the client repository.
25+
required: false
26+
default: false
27+
type: boolean
28+
29+
env:
30+
# Build configuration.
31+
BUILD_CONFIG: Release
32+
CACHE_PATTERNS: '["**/*.[cf]sproj*", "**/*.Build.props"]'
33+
# GIT user configuration.
34+
GIT_USER: 'Laura Trotta'
35+
36+
# Branches.
37+
BASE_BRANCH: ${{ github.ref_name }}
38+
CLIENT_BRANCH: ${{ inputs.target_branch != '' && inputs.target_branch || github.ref_name }}
39+
40+
jobs:
41+
generate:
42+
name: Generate client '${{ github.ref_name }}' -> '${{ inputs.target_branch != '' && inputs.target_branch || github.ref_name }}'
43+
runs-on: ubuntu-latest
44+
steps:
45+
- name: Checkout 'elastic-client-generator-java' @ '${{ env.BASE_BRANCH }}'
46+
uses: actions/checkout@v4
47+
with:
48+
repository: ${{ github.repository }}
49+
token: ${{ secrets.PAT }}
50+
ref: ${{ env.BASE_BRANCH }}
51+
path: elastic-client-generator-java
52+
53+
- name: Checkout 'elasticsearch-specification' @ '${{ env.BASE_BRANCH }}'
54+
uses: actions/checkout@v4
55+
with:
56+
repository: elastic/elasticsearch-specification
57+
token: ${{ secrets.PAT }}
58+
ref: ${{ env.BASE_BRANCH }}
59+
path: elasticsearch-specification
60+
61+
- name: Checkout 'elasticsearch-java' @ '${{ env.CLIENT_BRANCH }}'
62+
uses: actions/checkout@v4
63+
with:
64+
repository: elastic/elasticsearch-java
65+
token: ${{ secrets.PAT }}
66+
ref: ${{ env.CLIENT_BRANCH }}
67+
path: elasticsearch-java
68+
69+
- name: Setup Node.js
70+
uses: actions/setup-node@v4
71+
with:
72+
node-version-file: elasticsearch-specification/.nvmrc
73+
cache: npm
74+
cache-dependency-path: 'elasticsearch-specification/**/package-lock.json'
75+
76+
- name: Setup JVM 17 for 'elasticsearch-java'
77+
uses: actions/setup-java@v3
78+
with:
79+
java-version: '17'
80+
distribution: 'temurin'
81+
82+
- name: Generate specification output
83+
working-directory: elasticsearch-specification
84+
run: |
85+
make setup
86+
make generate
87+
88+
- name: Link generator to client
89+
working-directory: elastic-client-generator-java
90+
run: |
91+
make link-java-client
92+
93+
- name: Gradle build 'elastic-client-generator-java'
94+
working-directory: elastic-client-generator-java
95+
run: ./gradlew build
96+
97+
- name: Generate client
98+
working-directory: elasticsearch-java
99+
run: make rm-es generate-client
100+
101+
- name: Push changes to client repository
102+
id: push
103+
if: ${{ !inputs.dry_run }}
104+
working-directory: elasticsearch-java
105+
env:
106+
COMMIT_MESSAGE: '[codegen] update to latest spec'
107+
run: |
108+
set -e
109+
110+
git status --porcelain
111+
if [ -z "$(git status --porcelain)" ]; then
112+
echo "No changes to commit."
113+
exit 0
114+
fi
115+
116+
git config user.name "$GIT_USER"
117+
git config user.email "$GIT_MAIL"
118+
119+
target_branch="${{ inputs.target_branch }}"
120+
121+
if [ -z "$target_branch" ]; then
122+
target_branch="regenerate-${{ env.BASE_BRANCH }}"
123+
124+
git checkout -B "$target_branch"
125+
git add -A
126+
git commit -m "$COMMIT_MESSAGE"
127+
git push --force origin "$target_branch"
128+
129+
# Expose branch_name when we created a regenerate branch (so PR step runs).
130+
echo "branch_name=$target_branch" >> $GITHUB_OUTPUT
131+
132+
exit 0
133+
fi
134+
135+
git add -A
136+
137+
last_commit_msg=$(git log -1 --pretty=%B || true)
138+
139+
if [ "$last_commit_msg" = "$COMMIT_MESSAGE" ]; then
140+
# Amend the previous commit.
141+
git commit --amend -m "$COMMIT_MESSAGE"
142+
else
143+
git commit -m "$COMMIT_MESSAGE"
144+
fi
145+
146+
git push --force-with-lease origin "$target_branch"
147+
148+
- name: Create pull request
149+
if: ${{ !inputs.dry_run && steps.push.outputs.branch_name != '' }}
150+
working-directory: elasticsearch-java
151+
env:
152+
GH_TOKEN: ${{ secrets.PAT }}
153+
run: |
154+
gh pr create \
155+
--head "${{ steps.push.outputs.branch_name }}" \
156+
--base "${{ env.BASE_BRANCH }}" \
157+
--title "${{ (env.BASE_BRANCH != 'main' && format('[{0}] Regenerate client', env.BASE_BRANCH)) || 'Regenerate client' }}" \
158+
--body "As titled." \
159+
--label "skip-backport" \
160+
--assignee "${{ github.actor }}" || true

0 commit comments

Comments
 (0)