1
+ #! /bin/bash -e
2
+
3
+ UPDATE_YAML=" yq eval -i"
4
+ GIT=git
5
+ GH=gh
6
+ FILES=" ${FILES:- ./ data/ registry/* .yml} "
7
+
8
+
9
+ if [[ -n " $GITHUB_ACTIONS " ]]; then
10
+ # Ensure that we're starting from a clean state
11
+ git reset --hard origin/main
12
+ elif [[ " $1 " != " -f" ]]; then
13
+ # Do a dry-run when script it executed locally, unless the
14
+ # force flag is specified (-f).
15
+ echo " Doing a dry-run when run locally. Use -f as the first argument to force execution."
16
+ UPDATE_YAML=" yq eval"
17
+ GIT=" echo > DRY RUN: git "
18
+ GH=" echo > DRY RUN: gh "
19
+ else
20
+ # Local execution with -f flag (force real vs. dry run)
21
+ shift
22
+ fi
23
+
24
+ body=" "
25
+
26
+ for yaml_file in ${FILES} ; do
27
+ echo $yaml_file
28
+ # Check if yq is installed
29
+ if ! command -v yq & > /dev/null; then
30
+ echo " yq could not be found, please install yq."
31
+ exit 1
32
+ fi
33
+
34
+ # Function to get latest version based on registry
35
+ get_latest_version () {
36
+ package_name=$1
37
+ registry=$2
38
+
39
+ case $registry in
40
+ npm)
41
+ curl -s " https://registry.npmjs.org/${package_name} /latest" | jq -r ' .version'
42
+ ;;
43
+ packagist)
44
+ curl -s " https://repo.packagist.org/p2/${package_name} .json" | jq -r " .packages.\" ${package_name} \" [0].version"
45
+ ;;
46
+ gems)
47
+ curl -s " https://rubygems.org/api/v1/versions/${package_name} /latest.json" | jq -r ' .version'
48
+ ;;
49
+ go)
50
+ go list -m --versions " $package_name " | awk ' { if (NF > 1) print $NF ; else print "" }'
51
+ ;;
52
+ go-collector)
53
+ go list -m --versions " $package_name " | awk ' { if (NF > 1) print $NF ; else print "" }'
54
+ ;;
55
+ nuget)
56
+ lower_case_package_name=$( echo " $package_name " | tr ' [:upper:]' ' [:lower:]' )
57
+ curl -s " https://api.nuget.org/v3/registration5-gz-semver2/${lower_case_package_name} /index.json" | gunzip | jq -r ' .items[0].upper'
58
+ ;;
59
+ hex)
60
+ curl -s " https://hex.pm/api/packages/$package_name " | jq -r ' .releases | max_by(.inserted_at) | .version'
61
+ ;;
62
+ * )
63
+ echo " Registry not supported."
64
+ ;;
65
+ esac
66
+ }
67
+
68
+ # Read package details
69
+ name=$( yq eval ' .package.name' " $yaml_file " )
70
+ registry=$( yq eval ' .package.registry' " $yaml_file " )
71
+ current_version=$( yq eval ' .package.version' " $yaml_file " )
72
+
73
+ if [ -z " $name " ] || [ -z " $registry " ]; then
74
+ echo " ${yaml_file} : Package name and/or registry are missing in the YAML file."
75
+ else
76
+ # Get latest version
77
+ latest_version=$( get_latest_version " $name " " $registry " )
78
+
79
+ if [ " $latest_version " == " Registry not supported." ]; then
80
+ echo " ${yaml_file} ($registry ): Registry not supported." ;
81
+ elif [ -z " $latest_version " ]; then
82
+ echo " ${yaml_file} ($registry ): Could not get latest version from registry."
83
+ elif [ -z " $current_version " ]; then
84
+ ${UPDATE_YAML} " .package.version = \" $latest_version \" " $yaml_file
85
+ row=" ${yaml_file} ($registry ): Version field was missing. Populated with the latest version: $latest_version "
86
+ echo " ${row} "
87
+ body=" ${body} \n- ${row} "
88
+ elif [ " $latest_version " != " $current_version " ]; then
89
+ ${UPDATE_YAML} " .package.version = \" $latest_version \" " " $yaml_file "
90
+ row=" ($registry ): Updated version from $current_version to $latest_version in $yaml_file "
91
+ echo " ${yaml_file} ${row} "
92
+ body=" ${body} \n- ${row} "
93
+ else
94
+ echo " ${yaml_file} ($registry ): Version is already up to date."
95
+ fi
96
+ fi
97
+ done ;
98
+
99
+ # We use the sha1 over all version updates to uniquely identify the PR.
100
+ tag=$( echo body | sha1sum | awk ' {print $1;}' )
101
+ message=" Auto-update registry versions (${tag} )"
102
+ branch=" opentelemetrybot/auto-update-registry-${tag} "
103
+
104
+
105
+ existing_pr_count=$( gh pr list --state all --search " in:title $message " | wc -l)
106
+ if [ " $existing_pr_count " -gt 0 ]; then
107
+ echo " PR(s) already exist for '$message '"
108
+ gh pr list --state all --search " \" $message \" in:title"
109
+ echo " So we won't create another. Exiting."
110
+ exit 0
111
+ fi
112
+
113
+ $GIT checkout -b " $branch "
114
+ $GIT commit -a -m " $message "
115
+ $GIT push --set-upstream origin " $branch "
116
+
117
+ body_file=$( mktemp)
118
+ echo -en " ${body} " >> " ${body_file} "
119
+
120
+ echo " Submitting auto-update PR '$message '."
121
+ $GH pr create --title " $message " --body-file " ${body_file} "
0 commit comments