Skip to content

Commit a429d47

Browse files
committed
chore: Add github action to update discovery artifacts
1 parent aff037a commit a429d47

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

.github/workflows/main.yml

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
# Triggers the workflow on pull request events but only for the master branch
25+
pull_request:
26+
branches: [ master ]
27+
28+
jobs:
29+
build:
30+
name: Update Discovery Artifacts PR
31+
runs-on: ubuntu-latest
32+
if: |
33+
${{github.event.pull_request.user.login == 'parthea' && github.event.pull_request.title == 'chore: Add github action to update discovery artifacts'}}
34+
steps:
35+
- name: Check out master branch
36+
uses: actions/checkout@v2
37+
with:
38+
ref: refs/heads/master
39+
40+
- name: Create branch
41+
run: |
42+
git checkout -b update-discovery-artifacts
43+
44+
- name: Set up Python 3.9
45+
uses: actions/setup-python@v1
46+
with:
47+
python-version: 3.9
48+
49+
- name: Install google-api-python-client
50+
run: pip3 install -e .
51+
52+
- name: Install script dependencies
53+
run: pip3 install -r requirements.txt
54+
working-directory: ./scripts
55+
56+
# Apply a workaround to discovery.py to avoid DefaultCredentialsError
57+
# which is raised when calling `build_from_document()` as a result of
58+
# `google.auth.default()`. The workaround is to bypass the code block that
59+
# attempts to load default credentials.
60+
- name: Workaround to avoid DefaultCredentialsError in discovery.py
61+
run: sed -i -e 's/if credentials is None/if False/g' googleapiclient/discovery.py
62+
63+
- name: Run Change Summary
64+
run: python3 updatediscoveryartifacts.py
65+
working-directory: ./scripts
66+
67+
- name: Commit changes
68+
run: ./createcommits.sh
69+
working-directory: ./scripts
70+
71+
- name: Push changes
72+
run: git push -f --set-upstream origin update-discovery-artifacts
73+
74+
- name: Prepare summary for PR Body
75+
id: pr_body
76+
shell: bash
77+
run: |
78+
python3 buildprbody.py
79+
output=$(cat temp/allapis.summary)
80+
output="${output//'%'/'%25'}"
81+
output="${output//$'\n'/'%0A'}"
82+
output="${output//$'\r'/'%0D'}"
83+
echo "::set-output name=change_summary::$output"
84+
working-directory: ./scripts
85+
86+
- name: Create PR
87+
uses: actions/[email protected]
88+
with:
89+
github-token: ${{secrets.YOSHI_CODE_BOT_TOKEN}}
90+
script: |
91+
async function createPR () {
92+
const { owner, repo } = context.repo
93+
const branch = 'update-discovery-artifacts'
94+
let prBody = `${{ steps.pr_body.outputs.change_summary }}`
95+
const prTitle = 'chore: Update discovery artifacts'
96+
const pullRequests = await github.pulls.list({
97+
owner: owner,
98+
repo: repo,
99+
head: `${owner}:${branch}`,
100+
state: 'open'
101+
})
102+
103+
if (pullRequests.data.length === 1) {
104+
prNumber = pullRequests.data[0].number
105+
await github.pulls.update({
106+
owner: owner,
107+
repo: repo,
108+
pull_number: prNumber,
109+
title: prTitle,
110+
body: prBody
111+
})
112+
console.log('Updated PR')
113+
} else {
114+
const createPrResult = await github.pulls.create({
115+
owner: owner,
116+
repo: repo,
117+
base: 'master',
118+
head: `${owner}:${branch}`,
119+
title: prTitle,
120+
body: prBody
121+
})
122+
prNumber = createPrResult.data.number
123+
console.log('Created PR')
124+
}
125+
}
126+
createPR()

0 commit comments

Comments
 (0)