-
Notifications
You must be signed in to change notification settings - Fork 125
166 lines (137 loc) · 6.96 KB
/
update-OAS-version.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
name: Update OAS version and raise PR
on:
release:
types: [published]
jobs:
Update-yaml-version:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
ref: 'master'
- name: Install octokit dependencies
run: npm i
working-directory: ${{ github.workspace }}/.github/octokit
- name: Get github app access token
id: get_access_token
env:
GITHUB_APP_ID: ${{ secrets.XERO_PUBLIC_BOT_APP_ID }}
GITHUB_APP_PRIVATE_KEY: ${{ secrets.XERO_PUBLIC_BOT_KEY }}
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const { getAccessToken } = await import('${{ github.workspace }}/.github/octokit/index.js')
const token = await getAccessToken()
return token
- name: Fetch Latest release number
id: get_latest_release_number
run: |
latest_version=$(gh release view --json tagName --jq '.tagName')
echo "Latest release version is - $latest_version"
echo "releaseVersion=$latest_version" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{steps.get_access_token.outputs.result}}
- name: Set up branch name
id: identify_branch_name
run: |
branch_name='OAS-version-update-${{steps.get_latest_release_number.outputs.releaseVersion}}'
echo "branchName=$branch_name" >> $GITHUB_OUTPUT
- name: Checkout branch
run: |
if git ls-remote --heads origin ${{steps.identify_branch_name.outputs.branchName}} | grep -q "refs/heads/${{steps.identify_branch_name.outputs.branchName}}"; then
echo "checking out existing branch"
git fetch origin > /dev/null 2>&1
git checkout ${{steps.identify_branch_name.outputs.branchName}}
else
echo "branch does not exists, creating new branch"
echo "branchName *****>> ${{steps.identify_branch_name.outputs.branchName}}"
git checkout -b ${{steps.identify_branch_name.outputs.branchName}}
fi
- name: Update OAS version of the spec yaml files
run: |
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
yq eval --no-colors --prettyPrint ".info.version = \"${{steps.get_latest_release_number.outputs.releaseVersion}}\"" -i "$file"
echo "updated version in $file"
done
- name: Staging & commiting
id: detect-changes
run: |
CHANGES_DETECTED=$(git diff)
if [ -z "$CHANGES_DETECTED" ]; then
echo "no new changes, nothing to commit"
echo "changes_detected=false" >> $GITHUB_OUTPUT
else
echo "changes_detected=true" >> $GITHUB_OUTPUT
echo "running the git commands......"
git branch
echo "git status 1"
git status
echo 'staging the changes'
git add --all
echo 'git status'
git diff
echo "git config setup"
git config --global user.name "Github Actions"
git config --global user.email "actions@github.com"
echo 'commiting changes....'
git commit -m "chore: version update for all the yaml files"
fi
- name: Pushing the Branch
run: |
if [ "${{steps.detect-changes.outputs.changes_detected}}" == "true" ]; then
echo "pushing the code to ${{steps.identify_branch_name.outputs.branchName}}"
git branch
git push origin ${{steps.identify_branch_name.outputs.branchName}}
else
echo "branch is already up to date"
fi
- name: Create PR
run: |
if [ "${{steps.detect-changes.outputs.changes_detected}}" == "true" ]; then
echo "Creating PR in ${{steps.identify_branch_name.outputs.branchName}}"
gh pr create \
--title "OAS version update - ${{steps.get_latest_release_number.outputs.releaseVersion}}" \
--base master \
--head ${{steps.identify_branch_name.outputs.branchName}} \
--body "chore: version update for all the yaml files"
else
echo "PR is already up to date"
fi
env:
GH_TOKEN: ${{steps.get_access_token.outputs.result}}
- name: Find PR number to merge
id: identify_PR_number
run: |
BRANCH_NAME=${{steps.identify_branch_name.outputs.branchName}}
PR_NUMBER=$(gh pr list --head "$BRANCH_NAME" --json number --jq '. [0].number')
echo "PR number: $PR_NUMBER"
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{steps.get_access_token.outputs.result}}
- name: Auto Merge PR & delete branch
run: |
if [ -z "${{steps.identify_PR_number.outputs.PR_NUMBER}}" ]; then
echo "No PR found for branch: ${{steps.identify_branch_name.outputs.branchName}}"
exit 1
fi
echo "Merging the PR: ${{steps.identify_PR_number.outputs.PR_NUMBER}}....."
gh pr merge ${{steps.identify_PR_number.outputs.PR_NUMBER}} --merge --admin --delete-branch --repo ${{github.repository}}
env:
GH_TOKEN: ${{steps.get_access_token.outputs.result}}
- name: Verify PR is merged
run: |
pr_state=$(gh pr view ${{steps.identify_PR_number.outputs.PR_NUMBER}} --json state --jq '.state')
echo "pr state: $pr_state"
if [ $pr_state == "MERGED" ]; then
echo "PR ${{steps.identify_PR_number.outputs.PR_NUMBER}} has been merged"
else
echo "PR ${{steps.identify_PR_number.outputs.PR_NUMBER}} is not merged"
exit 1
fi
env:
GH_TOKEN: ${{steps.get_access_token.outputs.result}}