Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace release label validator script #519

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions .github/workflows/create-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,20 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: "./scripts/check_artifacts_existence.sh ${{ inputs.name }} ${{ inputs.name }}"

- name: Validate Labels
- name: Setup python
uses: actions/setup-python@v4
with:
python-version: '3.9'
cache: 'pip'

- name: Install requirements
run: pip install -r scripts/python/requirements.txt

- name: Validate labels
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./scripts/label_validator.sh "RELEASE"
REPOSITORY: ${{ env.KYMA_BTP_MANAGER_REPO }}
run: python3 scripts/python/release_label_validator.py

bump-sec-scanners-config:
name: Bump sec-scanners-config
Expand Down
52 changes: 52 additions & 0 deletions scripts/python/release_label_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import requests
import os
import yaml
import sys

with open('.github/release.yml', 'r') as file:
try:
release_yaml = yaml.safe_load(file)
label_pool = []
for category in release_yaml['changelog']['categories']:
label_pool.extend(category['labels'])
except yaml.YAMLError as exc:
print(exc)

print(f"One of these labels is required on PR: {label_pool}")
token = os.getenv('GITHUB_TOKEN')
repo = os.getenv('REPOSITORY')

response = requests.get(f'https://api.github.com/repos/{repo}/releases/latest', headers={'Authorization': f'token {token}'})
response.raise_for_status()
latest_release = response.json()
latest_release_date = latest_release['created_at']

response = requests.get(f'https://api.github.com/repos/{repo}/pulls?state=closed&sort=updated&direction=desc', headers={'Authorization': f'token {token}'})
response.raise_for_status()
all_closed_prs = response.json()

prs_since_last_release = [
pr for pr in all_closed_prs
if pr['merged_at'] is not None and pr['merged_at'] > latest_release_date
]

valid_prs = []
invalid_prs = []
for pr in prs_since_last_release:
labels = [label['name'] for label in pr['labels']]
common_labels = set(labels).intersection(label_pool)
if len(common_labels) != 1:
invalid_prs.append(pr['html_url'])
else:
valid_prs.append(pr['html_url'])

print("\nThese PRs have exactly one required label:")
print('\n'.join([f"PR: {pr}" for pr in valid_prs]))


if invalid_prs:
print("\nThese PRs don't have exactly one required label:")
print('\n'.join([f"PR: {pr}" for pr in invalid_prs]))
sys.exit(1)

print("\nAll PRs have exactly one required label")
3 changes: 2 additions & 1 deletion scripts/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pyyaml
jwt
jwt
requests
2 changes: 1 addition & 1 deletion scripts/testing/run_e2e_module_upgrade_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ elif [[ $# -eq 2 ]]; then
GITHUB_URL=https://api.github.com/repos/${REPOSITORY}
LATEST_RELEASE=$(curl -sS "${GITHUB_URL}/releases/latest" | jq -r '.tag_name')
NEW_MODULE_IMAGE_NAME=$1
OLD_MODULE_IMAGE_NAME=${NEW_MODULE_IMAGE_NAME/:*/:v$LATEST_RELEASE}
OLD_MODULE_IMAGE_NAME=${NEW_MODULE_IMAGE_NAME/:*/:$LATEST_RELEASE}
CI=${2-manual} # if called from any workflow "ci" is expected here
else
echo "wrong number of arguments" && exit 1
Expand Down