From 1142ba1a851cf81be8e965ea697a2661eda742b0 Mon Sep 17 00:00:00 2001 From: hestonhoffman Date: Thu, 25 Jul 2024 10:46:13 -0700 Subject: [PATCH] Find version with script --- .../bump_synthetics_worker_version.yml | 30 ++++++----- local/bin/py/synthetics-worker-version.py | 51 +++++++++++++++++++ 2 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 local/bin/py/synthetics-worker-version.py diff --git a/.github/workflows/bump_synthetics_worker_version.yml b/.github/workflows/bump_synthetics_worker_version.yml index fe60f47c01b37..ee44e6659a6b8 100644 --- a/.github/workflows/bump_synthetics_worker_version.yml +++ b/.github/workflows/bump_synthetics_worker_version.yml @@ -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/setup-python@v5.1.0 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 \ No newline at end of file + 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 }} + diff --git a/local/bin/py/synthetics-worker-version.py b/local/bin/py/synthetics-worker-version.py new file mode 100644 index 0000000000000..d6525c66c6310 --- /dev/null +++ b/local/bin/py/synthetics-worker-version.py @@ -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') \ No newline at end of file