Skip to content

Commit 4f30dd6

Browse files
committed
[PETOSS-829] added OAS-release actions and PR- Health check
1 parent b5ca731 commit 4f30dd6

10 files changed

Lines changed: 438 additions & 0 deletions

File tree

.github/octokit/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {Octokit} from "@octokit/rest";
2+
import {createAppAuth} from "@octokit/auth-app"
3+
4+
export const getAccessToken = async () => {
5+
6+
const {GITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY} = process.env
7+
8+
const octoKitInstance = new Octokit({
9+
authStrategy: createAppAuth,
10+
auth: {
11+
appId: GITHUB_APP_ID,
12+
privateKey: GITHUB_APP_PRIVATE_KEY
13+
}
14+
});
15+
16+
const {data: installations} = await octoKitInstance.rest.apps.listInstallations()
17+
18+
if(!installations.length) {
19+
throw new Error("No Installations found for this github app")
20+
}
21+
22+
const installationId = installations[0].id;
23+
24+
const installationAccessToken = await octoKitInstance.rest.apps.createInstallationAccessToken({installation_id: installationId})
25+
26+
return installationAccessToken.data.token
27+
}

.github/octokit/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "xero-octokit",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"@octokit/auth-app": "^7.1.1",
14+
"@octokit/rest": "^21.0.2"
15+
}
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "openapi-release",
3+
"private": true,
4+
"devDependencies": {
5+
"@semantic-release/git": "^10.0.1",
6+
"@semantic-release/github": "^11.0.0",
7+
"semantic-release": "^24.1.1"
8+
}
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const config = {
2+
branches: ['master'],
3+
tagFormat: "${version}",
4+
plugins: [
5+
['@semantic-release/release-notes-generator',
6+
{
7+
preset: 'angular',
8+
writerOpts: {
9+
headerPartial: '## What\'s changed',
10+
footerPartial: '\n**Full Changelog**:https://github.com/XeroAPI/Xero-OpenAPI/compare/{{previousTag}}...{{version}}'
11+
}
12+
}
13+
],
14+
'@semantic-release/commit-analyzer',
15+
'@semantic-release/github',
16+
'@semantic-release/git',
17+
18+
]
19+
}
20+
module.exports = config;

.github/workflows/OAS-release.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Create OAS release
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
OAS-release:
8+
runs-on: ubuntu-latest
9+
permissions:
10+
contents: write
11+
issues: write
12+
pull-requests: write
13+
14+
steps:
15+
16+
- name: Checkout Repository
17+
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
20+
21+
- name: Set up Node environment
22+
uses: actions/setup-node@v4
23+
with:
24+
node-version: 20
25+
26+
- name: install node package
27+
run: npm i
28+
working-directory: ${{ github.workspace }}/.github/semantic-release
29+
30+
- name: Perform release
31+
run: |
32+
npx semantic-release
33+
env:
34+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
35+
working-directory: ${{ github.workspace }}/.github/semantic-release
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
name: OAS PR health check
2+
3+
on:
4+
pull_request:
5+
types: [opened, reopened, synchronize]
6+
branches:
7+
- master
8+
push:
9+
branches:
10+
- master
11+
12+
jobs:
13+
pr-health-check:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: write
17+
pull-requests: write
18+
actions: read
19+
20+
steps:
21+
- name: Checkout Repository
22+
uses: actions/checkout@v4
23+
with:
24+
fetch-depth: 0
25+
26+
- name: Set up Node environment
27+
uses: actions/setup-node@v4
28+
with:
29+
node-version: 20
30+
31+
- name: Install dependencies
32+
run: npm ci
33+
34+
- name: Run commitlint on PR commits
35+
if: github.event_name == 'pull_request'
36+
run: |
37+
npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verbose
38+
39+
# ***** YAML Linting steps are disabled for now *****
40+
41+
42+
43+
# - name: Install Open API yaml linter
44+
# run: npm i -g @quobix/vacuum
45+
46+
# - name: Run open API validation for yamls
47+
# run: |
48+
# docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.3.1 validate -i /local/xero_accounting.yaml
49+
# docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.3.1 validate -i /local/xero_assets.yaml
50+
# docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.3.1 validate -i /local/xero_bankfeeds.yaml
51+
# docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.3.1 validate -i /local/xero_files.yaml
52+
# docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.3.1 validate -i /local/xero-app-store.yaml
53+
# docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.3.1 validate -i /local/xero-finance.yaml
54+
# docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.3.1 validate -i /local/xero-identity.yaml
55+
# docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.3.1 validate -i /local/xero-payroll-au.yaml
56+
# docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.3.1 validate -i /local/xero-payroll-nz.yaml
57+
# docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.3.1 validate -i /local/xero-payroll-uk.yaml
58+
# docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.3.1 validate -i /local/xero-projects.yaml
59+
# working-directory: ${{ github.workspace }}
60+
61+
# - name: Run linting
62+
# run: |
63+
# vacuum lint -d -e xero_accounting.yaml xero_assets.yaml xero_bankfeeds.yaml xero_files.yaml xero-app-store.yaml xero-finance.yaml xero-identity.yaml xero-payroll-au.yaml xero-payroll-nz.yaml xero-payroll-uk.yaml xero-projects.yaml
64+
65+
66+
67+
# ****** Linting steps are disabled for now *******
68+
69+
- name: Install octokit dependencies
70+
run: npm i
71+
working-directory: ${{ github.workspace }}/.github/octokit
72+
73+
- name: Get github app access token
74+
id: get_access_token
75+
env:
76+
GITHUB_APP_ID: ${{ secrets.XERO_GITHUB_APP_ID }}
77+
GITHUB_APP_PRIVATE_KEY: ${{ secrets.XERO_GITHUB_APPLICATION_KEY }}
78+
uses: actions/github-script@v7
79+
with:
80+
result-encoding: string
81+
script: |
82+
const { getAccessToken } = await import('${{ github.workspace }}/.github/octokit/index.js')
83+
const token = await getAccessToken()
84+
return token
85+
86+
- name: Trigger all SDK workflow
87+
uses: codex-/return-dispatch@v2
88+
id: return_dispatch
89+
with:
90+
token: ${{ steps.get_access_token.outputs.result }}
91+
repo: xeroapi-sdk-codegen
92+
owner: xero-internal
93+
workflow: pr.yml
94+
ref: refs/heads/master
95+
workflow_inputs: >
96+
{
97+
"branch_name": "${{ github.event_name == 'pull_request' && github.head_ref || 'master' }}"
98+
}
99+
100+
- name: Checking SDK health status
101+
uses: Codex-/await-remote-run@v1.12.2
102+
with:
103+
token: ${{ steps.get_access_token.outputs.result }}
104+
repo: xeroapi-sdk-codegen
105+
owner: xero-internal
106+
run_id: ${{ steps.return_dispatch.outputs.run_id }}
107+
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
name: Update OAS version and raise PR
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
Update-yaml-version:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: write
12+
pull-requests: write
13+
14+
steps:
15+
- name: Checkout Repository
16+
uses: actions/checkout@v4
17+
with:
18+
ref: 'master'
19+
20+
- name: Install octokit dependencies
21+
run: npm i
22+
working-directory: ${{ github.workspace }}/.github/octokit
23+
24+
- name: Get github app access token
25+
id: get_access_token
26+
env:
27+
GITHUB_APP_ID: ${{ secrets.XERO_PUBLIC_BOT_APP_ID }}
28+
GITHUB_APP_PRIVATE_KEY: ${{ secrets.XERO_PUBLIC_BOT_KEY }}
29+
uses: actions/github-script@v7
30+
with:
31+
result-encoding: string
32+
script: |
33+
const { getAccessToken } = await import('${{ github.workspace }}/.github/octokit/index.js')
34+
const token = await getAccessToken()
35+
return token
36+
37+
- name: Fetch Latest release number
38+
id: get_latest_release_number
39+
run: |
40+
latest_version=$(gh release view --json tagName --jq '.tagName')
41+
echo "Latest release version is - $latest_version"
42+
echo "releaseVersion=$latest_version" >> $GITHUB_OUTPUT
43+
env:
44+
GH_TOKEN: ${{steps.get_access_token.outputs.result}}
45+
46+
47+
- name: Set up branch name
48+
id: identify_branch_name
49+
run: |
50+
branch_name='OAS-version-update-${{steps.get_latest_release_number.outputs.releaseVersion}}'
51+
echo "branchName=$branch_name" >> $GITHUB_OUTPUT
52+
53+
- name: Checkout branch
54+
run: |
55+
if git ls-remote --heads origin ${{steps.identify_branch_name.outputs.branchName}} | grep -q "refs/heads/${{steps.identify_branch_name.outputs.branchName}}"; then
56+
echo "checking out existing branch"
57+
git fetch origin > /dev/null 2>&1
58+
git checkout ${{steps.identify_branch_name.outputs.branchName}}
59+
else
60+
echo "branch does not exists, creating new branch"
61+
echo "branchName *****>> ${{steps.identify_branch_name.outputs.branchName}}"
62+
git checkout -b ${{steps.identify_branch_name.outputs.branchName}}
63+
fi
64+
65+
- name: Update OAS version of the spec yaml files
66+
run: |
67+
for file in xero_accounting.yaml xero_assets.yaml xero_bankfeeds.yaml xero_files.yaml xero-app-store.yaml xero-finance.yaml xero-identity.yaml xero-payroll-au.yaml xero-payroll-nz.yaml xero-payroll-uk.yaml xero-projects.yaml; do
68+
yq eval --no-colors --prettyPrint ".info.version = \"${{steps.get_latest_release_number.outputs.releaseVersion}}\"" -i "$file"
69+
echo "updated version in $file"
70+
done
71+
72+
- name: Staging & commiting
73+
id: detect-changes
74+
run: |
75+
CHANGES_DETECTED=$(git diff)
76+
if [ -z "$CHANGES_DETECTED" ]; then
77+
echo "no new changes, nothing to commit"
78+
echo "changes_detected=false" >> $GITHUB_OUTPUT
79+
else
80+
echo "changes_detected=true" >> $GITHUB_OUTPUT
81+
echo "running the git commands......"
82+
83+
git branch
84+
85+
echo "git status 1"
86+
git status
87+
88+
echo 'staging the changes'
89+
git add --all
90+
91+
echo 'git status'
92+
git diff
93+
94+
echo "git config setup"
95+
git config --global user.name "Github Actions"
96+
git config --global user.email "actions@github.com"
97+
98+
echo 'commiting changes....'
99+
git commit -m "chore: version update for all the yaml files"
100+
fi
101+
102+
- name: Pushing the Branch
103+
run: |
104+
if [ "${{steps.detect-changes.outputs.changes_detected}}" == "true" ]; then
105+
echo "pushing the code to ${{steps.identify_branch_name.outputs.branchName}}"
106+
git branch
107+
git push origin ${{steps.identify_branch_name.outputs.branchName}}
108+
else
109+
echo "branch is already up to date"
110+
fi
111+
112+
- name: Create PR
113+
run: |
114+
if [ "${{steps.detect-changes.outputs.changes_detected}}" == "true" ]; then
115+
echo "Creating PR in ${{steps.identify_branch_name.outputs.branchName}}"
116+
gh pr create \
117+
--title "OAS version update - ${{steps.get_latest_release_number.outputs.releaseVersion}}" \
118+
--base master \
119+
--head ${{steps.identify_branch_name.outputs.branchName}} \
120+
--body "chore: version update for all the yaml files"
121+
else
122+
echo "PR is already up to date"
123+
fi
124+
env:
125+
GH_TOKEN: ${{steps.get_access_token.outputs.result}}
126+
127+
- name: Find PR number to merge
128+
id: identify_PR_number
129+
run: |
130+
BRANCH_NAME=${{steps.identify_branch_name.outputs.branchName}}
131+
PR_NUMBER=$(gh pr list --head "$BRANCH_NAME" --json number --jq '. [0].number')
132+
echo "PR number: $PR_NUMBER"
133+
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_OUTPUT
134+
env:
135+
GH_TOKEN: ${{steps.get_access_token.outputs.result}}
136+
137+
138+
- name: Auto Merge PR & delete branch
139+
run: |
140+
141+
if [ -z "${{steps.identify_PR_number.outputs.PR_NUMBER}}" ]; then
142+
echo "No PR found for branch: ${{steps.identify_branch_name.outputs.branchName}}"
143+
exit 1
144+
fi
145+
146+
echo "Merging the PR: ${{steps.identify_PR_number.outputs.PR_NUMBER}}....."
147+
148+
gh pr merge ${{steps.identify_PR_number.outputs.PR_NUMBER}} --merge --admin --delete-branch --repo ${{github.repository}}
149+
150+
env:
151+
GH_TOKEN: ${{steps.get_access_token.outputs.result}}
152+
153+
- name: Verify PR is merged
154+
run: |
155+
pr_state=$(gh pr view ${{steps.identify_PR_number.outputs.PR_NUMBER}} --json state --jq '.state')
156+
157+
echo "pr state: $pr_state"
158+
159+
if [ $pr_state == "MERGED" ]; then
160+
echo "PR ${{steps.identify_PR_number.outputs.PR_NUMBER}} has been merged"
161+
else
162+
echo "PR ${{steps.identify_PR_number.outputs.PR_NUMBER}} is not merged"
163+
exit 1
164+
fi
165+
env:
166+
GH_TOKEN: ${{steps.get_access_token.outputs.result}}

0 commit comments

Comments
 (0)