• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15name: A workflow for updating discovery artifacts
16# Controls when the action will run.
17
18on:
19  schedule:
20    # * is a special character in YAML so you have to quote this string
21    # Run this Github Action every Tuesday at 7 AM UTC
22    - cron:  '0 7 * * 2'
23
24jobs:
25  build:
26    name: Update Discovery Artifacts PR
27    runs-on: ubuntu-latest
28    if: ${{github.repository == 'googleapis/google-api-python-client'}}
29    steps:
30      - name: Get current date
31        id: date
32        run: echo "::set-output name=current_date::$(date +'%Y-%m-%d')"
33
34      - name: Check out main branch
35        uses: actions/checkout@v2
36        with:
37          ref: refs/heads/main
38
39      - name: Create branch
40        run: |
41          git checkout -b update-discovery-artifacts-${{ steps.date.outputs.current_date }}
42
43      - name: Set up Python 3.9
44        uses: actions/setup-python@v2
45        with:
46          python-version: 3.9
47
48      - name: Install google-api-python-client
49        run: pip3 install -e .
50
51      - name: Install script dependencies
52        run: pip3 install -r requirements.txt
53        working-directory: ./scripts
54
55      # Apply a workaround to discovery.py to avoid DefaultCredentialsError
56      # which is raised when calling `build_from_document()` as a result of
57      # `google.auth.default()`. The workaround is to bypass the code block that
58      # attempts to load default credentials.
59      - name: Workaround to avoid DefaultCredentialsError in discovery.py
60        run: sed -i -e 's/if credentials is None/if False/g' googleapiclient/discovery.py
61
62      - name: Run Change Summary
63        run: python3 updatediscoveryartifacts.py
64        working-directory: ./scripts
65
66      - name: Commit changes
67        run: ./createcommits.sh
68        working-directory: ./scripts
69
70      - name: Push changes
71        run: git push -f --set-upstream origin update-discovery-artifacts-${{ steps.date.outputs.current_date }}
72
73      - name: Prepare summary for PR Body
74        id: pr_body
75        shell: bash
76        run: |
77          python3 buildprbody.py
78          output=$(cat temp/allapis.summary)
79          output="${output//'%'/'%25'}"
80          output="${output//$'\n'/'%0A'}"
81          output="${output//$'\r'/'%0D'}"
82          echo "::set-output name=change_summary::$output"
83        working-directory: ./scripts
84
85      - name: Create PR
86        uses: actions/github-script@v4.0.2
87        with:
88          github-token: ${{secrets.YOSHI_CODE_BOT_TOKEN}}
89          script: |
90            async function createPR () {
91              const { owner, repo } = context.repo
92              const branch = 'update-discovery-artifacts-${{ steps.date.outputs.current_date }}'
93              let prBody = `${{ steps.pr_body.outputs.change_summary }}`
94              const prTitle = 'chore: Update discovery artifacts'
95              const pullRequests = await github.pulls.list({
96                owner: owner,
97                repo: repo,
98                head: `${owner}:${branch}`,
99                state: 'open'
100              })
101
102              if (pullRequests.data.length === 1) {
103                prNumber = pullRequests.data[0].number
104                await github.pulls.update({
105                  owner: owner,
106                  repo: repo,
107                  pull_number: prNumber,
108                  title: prTitle,
109                  body: prBody
110                })
111                console.log('Updated PR')
112              } else {
113                const createPrResult = await github.pulls.create({
114                  owner: owner,
115                  repo: repo,
116                  base: 'main',
117                  head: `${owner}:${branch}`,
118                  title: prTitle,
119                  body: prBody
120                })
121                prNumber = createPrResult.data.number
122                console.log('Created PR')
123              }
124            }
125            createPR()
126