Skip to content

Commit 0a470c7

Browse files
Add github action to update discovery artifacts
1 parent 91b61d3 commit 0a470c7

File tree

4 files changed

+473
-0
lines changed

4 files changed

+473
-0
lines changed

.github/workflows/main.yml

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
name: A workflow for updating discovery artifacts
2+
# Controls when the action will run.
3+
4+
on:
5+
# Triggers the workflow on pull request events but only for the master branch
6+
pull_request:
7+
branches: [ master ]
8+
9+
# Allows you to run this workflow manually from the Actions tab
10+
workflow_dispatch:
11+
12+
jobs:
13+
build:
14+
name: Update Discovery Artifacts PR
15+
runs-on: ubuntu-latest
16+
if: ${{github.event.pull_request.user.login == 'parthea' && github.event.pull_request.title == 'Add github action to update discovery artifacts'}}
17+
steps:
18+
- name: Check out working directory for changes
19+
uses: actions/checkout@v2
20+
with:
21+
ref: refs/heads/master
22+
path: ./branch
23+
24+
- name: Check out branch with scripts
25+
uses: actions/checkout@v2
26+
with:
27+
ref: refs/heads/add-github-action-to-update-discovery-artifacts
28+
path: ./branch_with_scripts
29+
30+
- name: Check out master
31+
uses: actions/checkout@v2
32+
with:
33+
ref: refs/heads/master
34+
path: ./main
35+
36+
- name: Create branch
37+
run: |
38+
git checkout -b update-discovery-artifacts
39+
working-directory: ./branch
40+
41+
- name: Clone discovery artifacts from discovery-artifact manager
42+
uses: actions/checkout@v2
43+
with:
44+
repository: googleapis/discovery-artifact-manager
45+
path: ./discovery-artifact-manager
46+
47+
- name: Copy scripts
48+
run: |
49+
cp ../branch_with_scripts/changesummary.py changesummary.py
50+
cp ../branch_with_scripts/create_commits.sh create_commits.sh
51+
cp ../branch_with_scripts/build_pr_body.py build_pr_body.py
52+
working-directory: ./branch
53+
54+
- name: Copy discovery artifacts
55+
run: |
56+
cp ../discovery-artifact-manager/discoveries/*.json googleapiclient/discovery_cache/documents/
57+
working-directory: ./branch
58+
59+
- name: Get changed discovery artifacts
60+
run: git diff origin/master --name-only -- '*.json' |
61+
sed 's/.*\///' > changed_files
62+
working-directory: ./branch
63+
64+
- name: Set up Python 3.9
65+
uses: actions/setup-python@v1
66+
with:
67+
python-version: 3.9
68+
69+
- name: Install pandas
70+
run: pip3 install pandas
71+
72+
- name: Install google-api-python-client
73+
run: pip3 install -e .
74+
working-directory: ./branch
75+
76+
- name: Patch to run the next step
77+
run: sed -i -e 's/if credentials is None/if False/g' googleapiclient/discovery.py
78+
working-directory: ./branch
79+
80+
- name: Update the docs
81+
run: python3 describe.py
82+
working-directory: ./branch
83+
84+
- name: Run Change Summary
85+
run: python3 changesummary.py
86+
working-directory: ./branch
87+
88+
- name: Commit changes
89+
run: ./create_commits.sh
90+
working-directory: ./branch
91+
92+
- name: Push changes
93+
run: git push -f --set-upstream origin update-discovery-artifacts
94+
working-directory: ./branch
95+
96+
- name: Prepare summary for PR Body
97+
id: pr_body
98+
shell: bash
99+
run: |
100+
python3 build_pr_body.py
101+
output=$(cat temp/allapis.summary)
102+
output="${output//'%'/'%25'}"
103+
output="${output//$'\n'/'%0A'}"
104+
output="${output//$'\r'/'%0D'}"
105+
echo "::set-output name=change_summary::$output"
106+
working-directory: ./branch
107+
108+
- name: Create PR
109+
uses: actions/[email protected]
110+
with:
111+
github-token: ${{secrets.YOSHI_CODE_BOT_TOKEN}}
112+
script: |
113+
const {owner, repo} = context.repo
114+
const branch = 'update-discovery-artifacts'
115+
let pr_body = `${{ steps.pr_body.outputs.change_summary }}`
116+
let pr_title = 'chore: Update discovery artifacts'
117+
const pull_requests = await github.pulls.list({
118+
owner: owner,
119+
repo: repo,
120+
head: `${owner}:${branch}`,
121+
state: 'open'
122+
});
123+
124+
if (pull_requests.data.length == 1) {
125+
prNumber = pull_requests.data[0].number
126+
await github.pulls.update({
127+
owner: owner,
128+
repo: repo,
129+
pull_number: prNumber,
130+
title: pr_title,
131+
body: pr_body
132+
});
133+
console.log('Updated PR')
134+
}
135+
else {
136+
const createPrResult = await github.pulls.create({
137+
owner: owner,
138+
repo: repo,
139+
base: "master",
140+
head: `${owner}:${branch}`,
141+
title: pr_title,
142+
body: pr_body
143+
});
144+
prNumber = createPrResult.data.number
145+
console.log('Created PR')
146+
}

build_pr_body.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import numpy as np
2+
import os
3+
import pandas as pd
4+
5+
def get_commit(Name, Version):
6+
url = 'https://github.com/googleapis/google-api-python-client/commit/'
7+
sha = None
8+
api_link = ""
9+
10+
file_path = "".join([directory, "/", Name,".sha"])
11+
if os.path.exists(file_path):
12+
with open(file_path, 'r') as f:
13+
sha = f.readline().rstrip()
14+
if sha:
15+
api_link = "".join([api_link, "[{0}]({1}{2})".format(" [More details]", url, sha)])
16+
17+
return api_link
18+
19+
if __name__== "__main__":
20+
directory = 'temp'
21+
dataframe = pd.read_csv("temp/allapis.dataframe")
22+
dataframe['Commit'] = np.vectorize(get_commit)(dataframe['Name'], dataframe['Version'])
23+
24+
stable_and_breaking = dataframe[dataframe['IsStable'] & dataframe['IsBreaking'].notnull()][['Name','Version','Commit']].drop_duplicates().agg(''.join, axis=1).values
25+
prestable_and_breaking = dataframe[(dataframe['IsStable'] == False) & dataframe['IsBreaking'].notnull()][['Name','Version','Commit']].drop_duplicates().agg(''.join, axis=1).values
26+
all_apis = dataframe[['Summary','Commit']].drop_duplicates().agg(''.join, axis=1)
27+
28+
with open("".join([directory,"/", "allapis.summary"]), 'w') as f:
29+
if (len(stable_and_breaking) > 0):
30+
f.writelines(["## Breaking changes were detected in the following stable APIs:\n", \
31+
"\n".join(stable_and_breaking), \
32+
"\n\n"])
33+
34+
if (len(prestable_and_breaking) > 0):
35+
f.writelines(["## Breaking changes were detected in the following pre-stable APIs:\n", \
36+
"\n".join(prestable_and_breaking), \
37+
"\n\n"])
38+
39+
if (len(all_apis) > 0):
40+
f.writelines(["## Discovery Artifact Change Summary:\n", \
41+
"\n".join(all_apis), \
42+
"\n"])
43+

0 commit comments

Comments
 (0)