Skip to content

Commit

Permalink
[SVLS-4918] refactor tracer version updaters in terms of a python script
Browse files Browse the repository at this point in the history
  • Loading branch information
apiarian-datadog committed Jul 18, 2024
1 parent 2a802b7 commit a0b0220
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 276 deletions.
202 changes: 202 additions & 0 deletions .github/workflows/datadog_wrapper_tracer_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import argparse
import json
import os
import re
from typing import NamedTuple
from urllib.request import urlopen


class Config(NamedTuple):
name: str
version_variable: str
repo_name: str
major_version_equal_to: int | None


configs = {
"dotnet": Config(
name=".NET",
version_variable="DD_DOTNET_TRACER_VERSION",
repo_name="dd-trace-dotnet",
major_version_equal_to=None,
),
"node_v4": Config(
name="Node.js Tracer v4",
version_variable="DD_NODE_TRACER_VERSION_4",
repo_name="dd-trace-js",
major_version_equal_to=4,
),
"node_v5": Config(
name="Node.js Tracer v5",
version_variable="DD_NODE_TRACER_VERSION_5",
repo_name="dd-trace-js",
major_version_equal_to=5,
),
"java": Config(
name="Java",
version_variable="DD_JAVA_TRACER_VERSION",
repo_name="dd-trace-java",
major_version_equal_to=None,
),
"php": Config(
name="PHP",
version_variable="DD_PHP_TRACER_VERSION",
repo_name="dd-trace-php",
major_version_equal_to=None,
),
"python": Config(
name="Python",
version_variable="DD_PYTHON_TRACER_VERSION",
repo_name="dd-trace-py",
major_version_equal_to=None,
),
}


datadog_wrapper_filename = os.path.join(
os.path.dirname(__file__), "../../", "datadog_wrapper"
)


def main() -> None:
parser = argparse.ArgumentParser(
description=f"update tracer versions in {datadog_wrapper_filename}"
)
parser.add_argument(
"--tracer",
type=str,
required=True,
help="the tracer to update",
choices=sorted(configs),
)

args = parser.parse_args()

check_version(config=configs[args.tracer])


def check_version(*, config: Config) -> None:
current_version = get_current_version(version_variable=config.version_variable)
print(f"current version: {current_version}")

latest_version = get_latest_version(
repo_name=config.repo_name, major_version_equal_to=config.major_version_equal_to
)
print(f"latest version: {latest_version}")

if current_version == latest_version:
print("versions match, nothing to do")
return

print(
f"updating {config.version_variable} from {current_version} to {latest_version}"
)
update_version(
version_variable=config.version_variable,
current_version=current_version,
latest_version=latest_version,
)
record_update_for_pull_request(
tracer_name=config.name,
version_variable=config.version_variable,
current_version=current_version,
latest_version=latest_version,
)


def get_current_version(*, version_variable: str) -> str:
version_regex = re.compile(version_variable + r"=(\d+\.\d+\.\d+)\s*$")
with open(datadog_wrapper_filename, "r") as f:
for line in f:
line = line.strip()
if (m := version_regex.match(line)) is not None:
return m.group(1)

raise Exception(f"Could not find the current version for {version_variable}")


def get_latest_version(*, repo_name: str, major_version_equal_to: int | None) -> str:
with urlopen(f"https://api.github.com/repos/datadog/{repo_name}/releases") as r:
data = json.loads(r.read().decode("utf-8"))
print(data)

versions = sorted(
filter(
None,
(
extract_version(
release_entry=entry, major_version_equal_to=major_version_equal_to
)
for entry in data
),
),
key=version_sort_key,
)
return versions.pop()


def extract_version(
*, release_entry: dict, major_version_equal_to: int | None
) -> str | None:
tag_name = release_entry["tag_name"]
match = re.match(r"^v?(\d+\.\d+\.\d+)$", tag_name)
if match is None:
return None

version = match.group(1)

if major_version_equal_to is None:
return version

[major, _, _] = version.split(".")
if int(major) != major_version_equal_to:
return None

return None


def version_sort_key(version: str) -> tuple[int, int, int]:
parts = tuple(map(int, version.split(".")))
if len(parts) != 3:
raise ValueError(f"invalid version: {version}")
return parts


def update_version(
*, version_variable: str, current_version: str, latest_version: str
) -> None:
with open(datadog_wrapper_filename, "r") as f:
lines = f.readlines()

with open(datadog_wrapper_filename, "w") as f:
for line in lines:
f.write(
line.replace(
f"{version_variable}={current_version}",
f"{version_variable}={latest_version}",
)
)


def record_update_for_pull_request(
*,
tracer_name: str,
version_variable: str,
current_version: str,
latest_version: str,
) -> None:
output_filename = os.getenv("GITHUB_OUTPUT")
if output_filename is None:
raise Exception(
"Missing GITHUB_OUTPUT environment variable, are we not running in a github workflow?"
)

with open(output_filename, "a") as f:
f.write(f"pr_title=Update {tracer_name} Tracer to {latest_version}\n")
f.write(
f"pr_body=Updated {tracer_name} Tracer from {current_version} to {latest_version}\n"
)


if __name__ == "__main__":
main()
34 changes: 4 additions & 30 deletions .github/workflows/dotnet_tracer_update_version.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
name: Update Version for the .NET Tracer

on:
schedule:
- cron: '0 14 * * *' # 2:00 PM UTC which is morning in New York
# schedule:
# - cron: '0 14 * * *' # 2:00 PM UTC which is morning in New York
push:

jobs:
bump_version:
Expand All @@ -15,34 +16,7 @@ jobs:
- name: Modify build-packages
id: version
run: |
set -euo pipefail
CURRENT_VERSION=$(awk '/DD_DOTNET_TRACER_VERSION=/{print}' datadog_wrapper | awk -F '=' '{print $2}' | tr -d '"')
if [ -z "$CURRENT_VERSION" ]; then
echo "DD_DOTNET_TRACER_VERSION is not set"
exit 1
fi
TRACER_RESPONSE=$(curl -s "https://api.github.com/repos/datadog/dd-trace-dotnet/releases")
NEWEST_VERSION=$(echo "$TRACER_RESPONSE" | jq -r --arg pattern "[0-9]+\.[0-9]+\.[0-9]+" '.[] | select(.tag_name | test($pattern)) | .tag_name' | tr -d "v" | sort -V | tail -n 1)
if [ -z "$NEWEST_VERSION" ]; then
echo "Could not find a version for the .NET Tracer"
exit 1
fi
if [ "$NEWEST_VERSION" != "$CURRENT_VERSION" ]; then
echo "Updating DD_DOTNET_TRACER_VERSION from $CURRENT_VERSION to $NEWEST_VERSION"
sed -i -e "s/DD_DOTNET_TRACER_VERSION=$CURRENT_VERSION/DD_DOTNET_TRACER_VERSION=$NEWEST_VERSION/" ./datadog_wrapper
PR_TITLE="Update .NET Tracer to $NEWEST_VERSION"
PR_BODY="Updated .NET Tracer from $CURRENT_VERSION to $NEWEST_VERSION"
else
echo "DD_DOTNET_TRACER_VERSION is already up to date"
fi
echo "pr_title=$PR_TITLE" >> "$GITHUB_OUTPUT"
echo "pr_body=$PR_BODY" >> "$GITHUB_OUTPUT"
python .github/workflows/datadog_wrapper_tracer_update.py --tracer dotnet
- name: Create Pull Request
id: pr
Expand Down
34 changes: 4 additions & 30 deletions .github/workflows/java_tracer_update_version.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
name: Update Version for the Java Tracer

on:
schedule:
- cron: '0 14 * * *' # 2:00 PM UTC which is morning in New York
# schedule:
# - cron: '0 14 * * *' # 2:00 PM UTC which is morning in New York
push:

jobs:
bump_version:
Expand All @@ -15,34 +16,7 @@ jobs:
- name: Modify build-packages
id: version
run: |
set -euo pipefail
CURRENT_VERSION=$(awk '/DD_JAVA_TRACER_VERSION=/{print}' datadog_wrapper | awk -F '=' '{print $2}' | tr -d '"')
if [ -z "$CURRENT_VERSION" ]; then
echo "DD_JAVA_TRACER_VERSION is not set"
exit 1
fi
TRACER_RESPONSE=$(curl -s "https://api.github.com/repos/datadog/dd-trace-java/releases")
NEWEST_VERSION=$(echo "$TRACER_RESPONSE" | jq -r --arg pattern "[0-9]+\.[0-9]+\.[0-9]+" '.[] | select(.tag_name | test($pattern)) | .tag_name' | tr -d "v" | sort -V | tail -n 1)
if [ -z "$NEWEST_VERSION" ]; then
echo "Could not find a version for the Java Tracer"
exit 1
fi
if [ "$NEWEST_VERSION" != "$CURRENT_VERSION" ]; then
echo "Updating DD_JAVA_TRACER_VERSION from $CURRENT_VERSION to $NEWEST_VERSION"
sed -i -e "s/DD_JAVA_TRACER_VERSION=$CURRENT_VERSION/DD_JAVA_TRACER_VERSION=$NEWEST_VERSION/" ./datadog_wrapper
PR_TITLE="Update Java Tracer to $NEWEST_VERSION"
PR_BODY="Updated Java Tracer from $CURRENT_VERSION to $NEWEST_VERSION"
else
echo "DD_JAVA_TRACER_VERSION is already up to date"
fi
echo "pr_title=$PR_TITLE" >> "$GITHUB_OUTPUT"
echo "pr_body=$PR_BODY" >> "$GITHUB_OUTPUT"
python .github/workflows/datadog_wrapper_tracer_update.py --tracer java
- name: Create Pull Request
id: pr
Expand Down
89 changes: 0 additions & 89 deletions .github/workflows/node_tracer_update_versions.yml

This file was deleted.

Loading

0 comments on commit a0b0220

Please sign in to comment.