Skip to content

Commit e9ce76a

Browse files
authored
Merge pull request #23212 from github/repo-sync
repo sync
2 parents 60af6a5 + 506ef84 commit e9ce76a

File tree

2 files changed

+68
-61
lines changed

2 files changed

+68
-61
lines changed
Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
name: OpenAPI generate decorated schema files
1+
name: Sync OpenAPI schema
22

3-
# **What it does**: On 'Update OpenAPI Descriptions' PRs opened by github-openapi-bot, this workflow runs the script to generate the decorated OpenAPI files and commit them to the PR.
4-
# **Why we have it**: So we can consume OpenAPI changes, decorate them, and publish them to the REST API docs.
3+
# **What it does**: Once a day, this workflow syncs the dereferenced files from github/rest-api-description and creates a pull request if there are updates to any of the static files we generate from the OpenAPI.
4+
# **Why we have it**: So we can consume OpenAPI changes.
55
# **Who does it impact**: Anyone making OpenAPI changes in `github/github`, and wanting to get them published on the docs site.
66

77
on:
8-
pull_request:
9-
# This prevents the workflow from running continuously. We only want
10-
# this workflow to run once on the initial open.
11-
types:
12-
- opened
8+
workflow_dispatch:
9+
schedule:
10+
- cron: '20 16 * * *' # Run every day at 16:20 UTC / 8:20 PST
1311

1412
permissions:
1513
contents: write
@@ -22,75 +20,67 @@ concurrency:
2220

2321
jobs:
2422
generate-decorated-files:
25-
if: >-
26-
${{
27-
github.repository == 'github/docs-internal' &&
28-
github.event.pull_request.user.login == 'github-openapi-bot'
29-
}}
23+
if: github.repository == 'github/docs-internal'
3024
runs-on: ubuntu-latest
3125
steps:
26+
- if: ${{ env.FREEZE == 'true' }}
27+
run: |
28+
echo 'The repo is currently frozen! Exiting this workflow.'
29+
exit 1 # prevents further steps from running
30+
3231
- name: Label pull requests with 'github-openapi-bot'
3332
uses: andymckay/labeler@e6c4322d0397f3240f0e7e30a33b5c5df2d39e90
3433
with:
3534
add-labels: 'github-openapi-bot'
3635

3736
- name: Checkout repository code
3837
uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
39-
with:
40-
# actions/checkout by default will leave you in a detached head state
41-
# so we need to specify the PR head ref explicitly since we're making
42-
# changes that we want to commit to the branch.
43-
ref: ${{ github.event.pull_request.head.ref }}
44-
# Using a PAT is necessary so that the new commit will trigger the
45-
# CI in the PR. (Events from GITHUB_TOKEN don't trigger new workflows.)
46-
token: ${{ secrets.DOCUBOT_REPO_PAT }}
47-
48-
- uses: ./.github/actions/node-npm-setup
4938

50-
- name: Decorate the dereferenced OpenAPI schemas
51-
run: script/rest/update-files.js --decorate-only
39+
# Check out a nested repository inside of previous checkout
40+
- name: Checkout rest-api-description repo
41+
uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
42+
with:
43+
# By default, only the most recent commit of the `main` branch
44+
# will be checked out
45+
repository: github/rest-api-description
46+
path: rest-api-description
5247

53-
- name: Check if pull request should be closed
54-
id: close-pr
55-
env:
56-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
57-
PR_URL: ${{ github.event.pull_request.html_url }}
48+
- name: Copy dereferenced OpenAPI files
49+
id: rest-api-description
5850
run: |
59-
echo "If there are no changes, exit"
60-
NUM_FILES_CHANGED=$(git diff --name-only -- lib/rest/static/decorated | wc -l)
61-
if [[ $NUM_FILES_CHANGED -eq 0 ]]
62-
then
63-
echo "No decorated file changes found"
64-
gh pr comment "$PR_URL" --body "🤖 This pull request has no changes to lib/rest/static/decorated, so it is being closed automatically."
65-
gh pr close "$PR_URL" --delete-branch
66-
echo "NO_DECORATED=true" >> $GITHUB_OUTPUT
67-
exit 0
68-
fi
51+
mkdir ./lib/rest/static/dereferenced
52+
find rest-api-description/descriptions-next -type f -name "*.deref.json" -exec sh -c 'cp $1 ./lib/rest/static/dereferenced' sh {} \;
53+
cd rest-api-description
54+
HEAD_SHA=$(git rev-parse HEAD) >> $GITHUB_OUTPUT
6955
70-
- name: Check in the decorated files
71-
if: ${{ steps.close-pr.outputs.NO_DECORATED != 'true' }}
72-
uses: EndBug/add-and-commit@050a66787244b10a4874a2a5f682130263edc192
73-
with:
74-
# The arguments for the `git add` command
75-
add: '["lib/rest/static/apps", "lib/rest/static/decorated", "lib/webhooks/static/decorated", "lib/redirects/static/client-side-rest-api-redirects.json"]'
56+
- uses: ./.github/actions/node-npm-setup
7657

77-
# The message for the commit
78-
message: 'Add decorated OpenAPI schema files'
58+
- name: Decorate the dereferenced OpenAPI schemas
59+
run: script/rest/update-files.js --decorate-only --open-source
7960

61+
# This action performs the diff and if no files have been change
62+
# the action exits silently.
63+
- name: Create pull request
64+
id: create-pull-request
65+
uses: peter-evans/create-pull-request@2b011faafdcbc9ceb11414d64d0573f37c774b04
8066
env:
81-
# Disable pre-commit hooks; they don't play nicely with add-and-commit
67+
# Disable pre-commit hooks; they don't play nicely here
8268
HUSKY: '0'
83-
84-
- name: Remove the dereferenced files
85-
if: ${{ steps.close-pr.outputs.NO_DECORATED != 'true' }}
86-
uses: EndBug/add-and-commit@050a66787244b10a4874a2a5f682130263edc192
8769
with:
88-
# The arguments for the `git add` command
89-
remove: '["lib/rest/static/dereferenced/*"]'
90-
91-
# The message for the commit
92-
message: 'Removed dereferenced OpenAPI schema files'
70+
# Need to use a token with repo and workflow scopes for this step.
71+
# Token should be a PAT because actions performed with GITHUB_TOKEN
72+
# don't trigger other workflows and this action force pushes updates
73+
# from the default branch.
74+
token: ${{ secrets.OCTOMERGER_PAT_WITH_REPO_AND_WORKFLOW_SCOPE }}
75+
add-paths: |
76+
- lib/rest/static/apps
77+
- lib/rest/static/decorated
78+
- lib/webhooks/static/decorated
79+
- lib/redirects/static/client-side-rest-api-redirects.json
80+
commit-message: 'Add decorated OpenAPI schema files'
81+
title: Update OpenAPI Description
82+
body: |
83+
'👋 humans. This PR updates the OpenAPI description with the latest changes. (Synced from github/rest-api-description@${{ steps.rest-api-description.outputs.HEAD_SHA }})
9384
94-
env:
95-
# Disable pre-commit hooks; they don't play nicely with add-and-commit
96-
HUSKY: '0'
85+
If CI does not pass or other problems arise, contact #docs-engineering on slack.'
86+
branch: openapi-update-${{ steps.rest-api-description.outputs.HEAD_SHA }})

script/rest/update-files.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import rimraf from 'rimraf'
1515

1616
import { decorate } from './utils/decorator.js'
1717
import { validateVersionsOptions } from './utils/get-openapi-schemas.js'
18+
import { allVersions } from '../../lib/all-versions.js'
1819

1920
const TEMP_DOCS_DIR = path.join(process.cwd(), 'openapiTmp')
2021
const DOCS_DEREF_OPENAPI_DIR = path.join(process.cwd(), 'lib/rest/static/dereferenced')
@@ -37,6 +38,7 @@ program
3738
'Keeps the dereferenced files after the script runs. You will need to delete them manually.'
3839
)
3940
.option('-n --next', 'Generate the next OpenAPI calendar-date version.')
41+
.option('-s --open-source', 'Generate the OpenAPI schema from github/rest-api-description')
4042
.parse(process.argv)
4143

4244
const {
@@ -46,6 +48,7 @@ const {
4648
includeDeprecated,
4749
keepDereferencedFiles,
4850
next,
51+
openSource,
4952
} = program.opts()
5053

5154
main()
@@ -57,6 +60,20 @@ async function main() {
5760
await getBundledFiles()
5861
}
5962

63+
// When we get the dereferenced OpenAPI files from the open-source
64+
// github/rest-api-description repo, we need to remove any versions
65+
// that are deprecated.
66+
if (openSource) {
67+
const currentOpenApiVersions = Object.values(allVersions).map((elem) => elem.openApiVersionName)
68+
const allSchemas = await readdir(DOCS_DEREF_OPENAPI_DIR)
69+
allSchemas.forEach((schema) => {
70+
// if the schema does not start with a current version name, delete it
71+
if (!currentOpenApiVersions.some((version) => schema.startsWith(version))) {
72+
rimraf.sync(path.join(DOCS_DEREF_OPENAPI_DIR, schema))
73+
}
74+
})
75+
}
76+
6077
const schemas = await readdir(DOCS_DEREF_OPENAPI_DIR)
6178
// Decorate the dereferenced files in a format ingestible by docs.github.com
6279
await decorate(schemas)

0 commit comments

Comments
 (0)