Skip to content

Commit 57b0587

Browse files
chore: GitHub Actions workflow to generate new client library (#10179)
* feat: new client workflow * define inputs to workflow * initial new library script * improve input description * test correct backtick syntax * enclose backtick containing descriptions in double quotes * remove "less necessary" inputs * default values for non-string inputs * fix ubuntu version, remove java install * fix xargs and arguments * fix input-to-env list * correct underscores in input to new-client * add PR creation * fix id typo * add gh token to pr creation step * use YOSHI_CODE_BOT_TOKEN * use distribution_name instead of group_id * use debug output * remove wrong push option * sanitize token commit message * specify branch in gh pr create * escape newlines for pr message * try different newline approach * improve pr description * correct username to author the commits * use destination_name argument instead of cloud_api * prepare `destination-name` argument * correct env var setup * randomize branch name * correct random_id syntax * add distribution-name to arguments * remove dummy message step * include workflow location in commit message * use url for workflow link * update new-client README * move local prerequisites to bottom * explain advanced options not called in gh action * clarify advanced options note * simplify readme local links * use alternative warning format * move local example to the bottom * add links to gh action * highlight advanced options note with warning * add explanation on finding the generated PR * correct terminology/capitalization * improve top-to-bottom readability * fix "option" bias in doc * remove links for arguments * punctuation * remove redundant advanced options explanation * restore principles section
1 parent afe64b1 commit 57b0587

File tree

3 files changed

+352
-200
lines changed

3 files changed

+352
-200
lines changed
+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
name: Generate new GAPIC client library
2+
on:
3+
workflow_dispatch:
4+
# some inputs are ommited due to limit of 10 input arguments
5+
inputs:
6+
api_shortname:
7+
required: true
8+
type: string
9+
description: "`api_shortname`: Name for the new directory name and (default) artifact name"
10+
name_pretty:
11+
required: true
12+
type: string
13+
description: "`name_pretty`: The human-friendly name that appears in README.md"
14+
proto_path:
15+
required: true
16+
type: string
17+
description: |
18+
`proto_path`: Path to proto file from the root of the googleapis repository to the
19+
directory that contains the proto files (without the version).
20+
For example, to generate the library for 'google/maps/routing/v2',
21+
then you specify this value as 'google/maps/routing'
22+
product_docs:
23+
required: true
24+
type: string
25+
description: "`product_docs`: Documentation URL that appears in README.md"
26+
rest_docs:
27+
required: false
28+
type: string
29+
description: |
30+
`rest_docs`: If it exists, link to the REST Documentation for a service
31+
rpc_docs:
32+
required: false
33+
type: string
34+
description: |
35+
`rpc_docs`: If it exists, link to the RPC Documentation for a service
36+
api_description:
37+
required: true
38+
description: "`api_description`: Description that appears in README.md"
39+
transport:
40+
required: false
41+
type: choice
42+
default: grpc
43+
options:
44+
- grpc
45+
- http
46+
- both
47+
description: "`transport`: A label that appears in repo-metadata.json"
48+
destination_name:
49+
required: false
50+
type: string
51+
description: |
52+
`destination_name`: The directory name of the new library. By default it's
53+
java-<api_shortname>
54+
distribution_name:
55+
required: false
56+
type: string
57+
description: |
58+
`distribution_name`: Maven coordinates of the generated library. By default it's
59+
com.google.cloud:google-cloud-<api_shortname>
60+
jobs:
61+
generate:
62+
runs-on: ubuntu-22.04
63+
steps:
64+
- uses: actions/checkout@v3
65+
- uses: actions/setup-python@v4
66+
with:
67+
python-version: '3.9'
68+
cache: 'pip' # caching pip dependencies
69+
- name: Get current week within the year
70+
id: date
71+
run: echo "::set-output name=week_of_year::$(date +'%W' --utc)"
72+
- name: Install new-client.py dependencies
73+
run: pip install --require-hashes -r generation/new_client/requirements.txt
74+
- name: Generate
75+
id: generation
76+
run: |
77+
set -x
78+
arguments="--api_shortname=\"${API_SHORTNAME}\" \
79+
--proto-path=\"${PROTO_PATH}\" \
80+
--name-pretty=\"${NAME_PRETTY}\" \
81+
--product-docs=\"${PRODUCT_DOCS}\" \
82+
--api-description=\"${API_DESCRIPTION}\""
83+
84+
# helper function that appends a python argument only if specified in the GH action inputs
85+
append_argument() {
86+
py_arg=$1
87+
# env vars look exactly like new-client arguments but uppercase + underscores
88+
env_name=$(echo "${py_arg}" | sed 's/-/_/g' | sed -e 's/\([a-z]\)/\U\1/g')
89+
if [[ -n "${!env_name}" ]]; then
90+
# $(echo) is redundant but it works around a syntax highlighting problem in vim
91+
arguments=$(echo "${arguments} --${py_arg}=\"${!env_name}\"")
92+
fi
93+
}
94+
95+
declare -a optional_args=('transport' 'destination-name' 'distribution-name' 'group-id' 'rest-docs' 'rpc-docs')
96+
97+
for python_argument in "${optional_args[@]}"; do
98+
append_argument "${python_argument}"
99+
done
100+
echo "::set-output name=new_library_args::${arguments}"
101+
echo "${arguments} --googleapis-gen-url=\"${GOOGLEAPIS_GEN_URL}\"" | xargs python generation/new_client/new-client.py generate
102+
env:
103+
GOOGLEAPIS_GEN_URL: https://yoshi-approver:${{ secrets.YOSHI_CODE_BOT_TOKEN }}@github.com/googleapis/googleapis-gen.git
104+
API_SHORTNAME: ${{ github.event.inputs.api_shortname }}
105+
NAME_PRETTY: ${{ github.event.inputs.name_pretty }}
106+
PROTO_PATH: ${{ github.event.inputs.proto_path }}
107+
PRODUCT_DOCS: ${{ github.event.inputs.product_docs }}
108+
REST_DOCS: ${{ github.event.inputs.rest_docs }}
109+
RPC_DOCS: ${{ github.event.inputs.rpc_docs }}
110+
API_DESCRIPTION: ${{ github.event.inputs.api_description }}
111+
TRANSPORT: ${{ github.event.inputs.transport }}
112+
DESTINATION_NAME: ${{ github.event.inputs.destination_name }}
113+
DISTRIBUTION_NAME: ${{ github.event.inputs.distribution_name }}
114+
- name: Push to branch and create PR
115+
run: |
116+
set -x
117+
[ -z "`git config user.email`" ] && git config --global user.email "${USERNAME:-script}@google.com"
118+
[ -z "`git config user.name`" ] && git config --global user.name "${USERNAME:-script}"
119+
120+
# create and push to branch in origin
121+
# random_id allows multiple runs of this workflow
122+
random_id=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 5; echo)
123+
branch_name="new-library/${{ github.event.inputs.api_shortname }}-${random_id}"
124+
git checkout -b "${branch_name}"
125+
git add --all
126+
git commit -m "feat: [${API_SHORTNAME}] new module for ${API_SHORTNAME}
127+
128+
129+
Generated with https://github.com/googleapis/google-cloud-java/actions/workflows/generate_new_client.yaml
130+
131+
Command used:
132+
133+
\`\`\`
134+
python generation/new_client/new-client.py generate ${GENERATION_ARGUMENTS}
135+
\`\`\`"
136+
git remote add monorepo https://${{ github.actor }}:${{ github.token }}@github.com/${{ github.repository }}.git
137+
git fetch -q --unshallow monorepo
138+
git push -f monorepo "${branch_name}"
139+
140+
# create PR
141+
gh pr create --fill --head "${branch_name}"
142+
env:
143+
USERNAME: ${{ github.actor }}
144+
API_SHORTNAME: ${{ github.event.inputs.api_shortname }}
145+
GENERATION_ARGUMENTS: ${{ steps.generation.outputs.new_library_args }}
146+
GH_TOKEN: ${{ secrets.YOSHI_CODE_BOT_TOKEN }}
147+

.github/workflows/verify-client-generation.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
with:
3939
path: ~/.m2/repository
4040
key: ${{ runner.os }}-maven-unified-${{ steps.date.outputs.week_of_year }}
41-
- name: Install dependencies
41+
- name: Install new-client.py dependencies
4242
run: pip install --require-hashes -r generation/new_client/requirements.txt
4343
- name: Remove Client
4444
run: |

0 commit comments

Comments
 (0)