Skip to content

Commit

Permalink
Find version with script
Browse files Browse the repository at this point in the history
  • Loading branch information
hestonhoffman committed Jul 25, 2024
1 parent b734f8f commit 1142ba1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
30 changes: 16 additions & 14 deletions .github/workflows/bump_synthetics_worker_version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@ jobs:
pull-requests: write # to create pull request
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Generate a token
id: generate-token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ vars.DOCS_GH_APP_ID }}
private-key: ${{ secrets.DOCS_GH_APP_PRIVATE_KEY }}
- id: set-worker-version
name: Load latest worker version
uses: actions/github-script@v7

- uses: actions/[email protected]
with:
github-token: ${{ steps.generate-token.outputs.token }}
result-encoding: string
script: |
const tags = await github.rest.repos.listTags({
owner: 'hestonhoffman',
repo: 'changed-lines'
})
const regex = /^(?:(?:[0-9]*)[.](?:[0-9]*)[.](?:[0-9]*))$/;
latestTag = tags.data.find(value => regex.test(value))
console.log(latestTag)
return latestTag
python-version: '3.11'

- name: Find latest synthetic-worker version
id: set-worker-version
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
run: |
python local/bin/py/synthetics_worker_version.py
- name: echo worker version
run: echo ${{ steps.set-worker-version.outputs.worker_version }}

51 changes: 51 additions & 0 deletions local/bin/py/synthetics-worker-version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import requests
import re
from os import environ

class InvalidTokenError(Exception):
'''
Represents an error when no token is provided
'''

def get_data():
url = 'https://api.github.com/repos/DataDog/synthetics-worker/git/refs/tags'
headers = {
'Authorization': f'Bearer {token}',
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28'
}
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception('Failed to get tags')
return response.json()

def get_tags(data):
versions = []
for tag in data:
version = tag.get('ref', '').split('/')[-1]
if version:
versions.append(version)
return versions

def return_latest_version(tags):
valid_versions = []
pattern = r"^(?:(?:[0-9]*)[.](?:[0-9]*)[.](?:[0-9]*))$"
valid_versions = [version for version in tags if re.match(pattern, version)]
return sorted(valid_versions)[-1]

'''
Gets the latest version tag from the synthetics-worker repo
'''
if __name__ == "__main__":
token = environ.get('GH_TOKEN')
github_output = environ.get('GITHUB_OUTPUT')
if token is None:
raise InvalidTokenError('No token provided')
data = get_data()
tags = get_tags(data)
latest_version = return_latest_version(tags)
print(f'Latest version: {latest_version}')

# Write the latest version to the output file
with open(github_output, 'w', encoding='utf-8') as f:
f.write(f'worker_version={latest_version}\n')

0 comments on commit 1142ba1

Please sign in to comment.