Skip to content

Commit c9a1a8c

Browse files
authored
chore: Add github action to update discovery artifacts (#1187)
* chore: Add github action to update discovery artifacts * Add date to branch name * Remove test code
1 parent 3dd1527 commit c9a1a8c

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

.github/workflows/main.yml

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Copyright 2021 Google LLC
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: A workflow for updating discovery artifacts
16+
# Controls when the action will run.
17+
18+
on:
19+
schedule:
20+
# * is a special character in YAML so you have to quote this string
21+
# Run this Github Action every day at 7 AM UTC
22+
- cron: '* 7 * * *'
23+
24+
jobs:
25+
build:
26+
name: Update Discovery Artifacts PR
27+
runs-on: ubuntu-latest
28+
steps:
29+
- name: Get current date
30+
id: date
31+
run: echo "::set-output name=current_date::$(date +'%Y-%m-%d')"
32+
33+
- name: Check out master branch
34+
uses: actions/checkout@v2
35+
with:
36+
ref: refs/heads/master
37+
38+
- name: Create branch
39+
run: |
40+
git checkout -b update-discovery-artifacts-${{ steps.date.outputs.current_date }}
41+
42+
- name: Set up Python 3.9
43+
uses: actions/setup-python@v1
44+
with:
45+
python-version: 3.9
46+
47+
- name: Install google-api-python-client
48+
run: pip3 install -e .
49+
50+
- name: Install script dependencies
51+
run: pip3 install -r requirements.txt
52+
working-directory: ./scripts
53+
54+
# Apply a workaround to discovery.py to avoid DefaultCredentialsError
55+
# which is raised when calling `build_from_document()` as a result of
56+
# `google.auth.default()`. The workaround is to bypass the code block that
57+
# attempts to load default credentials.
58+
- name: Workaround to avoid DefaultCredentialsError in discovery.py
59+
run: sed -i -e 's/if credentials is None/if False/g' googleapiclient/discovery.py
60+
61+
- name: Run Change Summary
62+
run: python3 updatediscoveryartifacts.py
63+
working-directory: ./scripts
64+
65+
- name: Commit changes
66+
run: ./createcommits.sh
67+
working-directory: ./scripts
68+
69+
- name: Push changes
70+
run: git push -f --set-upstream origin update-discovery-artifacts-${{ steps.date.outputs.current_date }}
71+
72+
- name: Prepare summary for PR Body
73+
id: pr_body
74+
shell: bash
75+
run: |
76+
python3 buildprbody.py
77+
output=$(cat temp/allapis.summary)
78+
output="${output//'%'/'%25'}"
79+
output="${output//$'\n'/'%0A'}"
80+
output="${output//$'\r'/'%0D'}"
81+
echo "::set-output name=change_summary::$output"
82+
working-directory: ./scripts
83+
84+
- name: Create PR
85+
uses: actions/[email protected]
86+
with:
87+
github-token: ${{secrets.YOSHI_CODE_BOT_TOKEN}}
88+
script: |
89+
async function createPR () {
90+
const { owner, repo } = context.repo
91+
const branch = 'update-discovery-artifacts-${{ steps.date.outputs.current_date }}'
92+
let prBody = `${{ steps.pr_body.outputs.change_summary }}`
93+
const prTitle = 'chore: Update discovery artifacts'
94+
const pullRequests = await github.pulls.list({
95+
owner: owner,
96+
repo: repo,
97+
head: `${owner}:${branch}`,
98+
state: 'open'
99+
})
100+
101+
if (pullRequests.data.length === 1) {
102+
prNumber = pullRequests.data[0].number
103+
await github.pulls.update({
104+
owner: owner,
105+
repo: repo,
106+
pull_number: prNumber,
107+
title: prTitle,
108+
body: prBody
109+
})
110+
console.log('Updated PR')
111+
} else {
112+
const createPrResult = await github.pulls.create({
113+
owner: owner,
114+
repo: repo,
115+
base: 'master',
116+
head: `${owner}:${branch}`,
117+
title: prTitle,
118+
body: prBody
119+
})
120+
prNumber = createPrResult.data.number
121+
console.log('Created PR')
122+
}
123+
}
124+
createPR()

0 commit comments

Comments
 (0)