Skip to content

Commit 8bbf8e4

Browse files
PETOSS 588 | Pipeline | OAS version update for all spec yaml files (#677)
ci: Added the auto PR raise and merge logic for JIRA 588
1 parent 5c916cb commit 8bbf8e4

3 files changed

Lines changed: 209 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: 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)