From abbc09da7f92f176a669e72d52653b80f074c12e Mon Sep 17 00:00:00 2001 From: Vitor Bandeira Date: Mon, 29 Jul 2024 21:17:12 +0000 Subject: [PATCH 1/9] ci: new gha to update rules Signed-off-by: Vitor Bandeira --- .../github-actions-manual-update-rules.yml | 52 ----------- .../workflows/github-actions-update-rules.yml | 89 +++++++++---------- .github/workflows/jenkins-check-build.sh | 59 ++++++++++++ 3 files changed, 102 insertions(+), 98 deletions(-) delete mode 100644 .github/workflows/github-actions-manual-update-rules.yml create mode 100755 .github/workflows/jenkins-check-build.sh diff --git a/.github/workflows/github-actions-manual-update-rules.yml b/.github/workflows/github-actions-manual-update-rules.yml deleted file mode 100644 index fc10dae30d..0000000000 --- a/.github/workflows/github-actions-manual-update-rules.yml +++ /dev/null @@ -1,52 +0,0 @@ -name: Manually Trigger Update Rules -on: - workflow_dispatch: - inputs: - type: - description: 'Type of update (overwrite or normal)' - required: true - default: 'normal' - -jobs: - update: - runs-on: ubuntu-latest - strategy: - fail-fast: false - steps: - - name: Check out repository code recursively - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - uses: actions/setup-python@v4 - with: - python-version: "3.10" - - name: Install Python Packages - run: | - pip install firebase-admin - - name: Execute Python Script Update - env: - CREDS_FILE: ${{ secrets.CREDS_FILE }} - API_BASE_URL: ${{ secrets.API_BASE_URL }} - run: | - if [[ "${{ github.event.inputs.type }}" == "overwrite" ]]; then - python flow/util/updateRules.py --keyFile "${CREDS_FILE}" --apiURL ${API_BASE_URL} --commitSHA $(git rev-parse HEAD) --overwrite - else - python flow/util/updateRules.py --keyFile "${CREDS_FILE}" --apiURL ${API_BASE_URL} --commitSHA $(git rev-parse HEAD) - fi - - name: Push updated rules - id: remote-update - run: | - git config --local user.email "github-actions[bot]@users.noreply.github.com" - git config --local user.name "github-actions[bot]" - if [ -n "$(git status --porcelain)" ]; then - echo "has_update=true" >> "$GITHUB_OUTPUT" - else - echo "has_update=false" >> "$GITHUB_OUTPUT" - fi - git add . - git commit --signoff -m "flow: update rules based on new golden reference" - - if: "steps.remote-update.outputs.has_update == 'true'" - name: update rules pr - id: remote-update-pr - run: | - git push diff --git a/.github/workflows/github-actions-update-rules.yml b/.github/workflows/github-actions-update-rules.yml index c728f90df9..feacbf91e7 100644 --- a/.github/workflows/github-actions-update-rules.yml +++ b/.github/workflows/github-actions-update-rules.yml @@ -1,71 +1,68 @@ -name: Create draft PR for updated rules +name: Update Rules + on: - repository_dispatch: - types: - - set-new-golden + pull_request: + types: [labeled] jobs: - update: + update-rules: + if: contains(github.event.label.name, 'Update Rules') runs-on: ubuntu-latest - strategy: - fail-fast: false steps: - - name: Check out repository code recursively + - name: Check out PR code uses: actions/checkout@v3 with: - fetch-depth: 0 - - name: Git prep - run: | - git config --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*" - git fetch - git checkout "origin/pr/${{ github.event.client_payload.branch }}" - - uses: actions/setup-python@v4 + ref: ${{ github.event.pull_request.head.ref }} + repository: ${{ github.event.pull_request.head.repo.full_name }} + token: ${{ secrets.GITHUB_TOKEN }} + fetch-depth: 1 + + - name: Set up Python 3.10 + uses: actions/setup-python@v4 with: python-version: "3.10" + - name: Install Python Packages run: | pip install firebase-admin + + - name: Query Jenkins Build Status + id: query_jenkins + run: | + .github/workflows/jenkins-check-build.sh "${{ github.event.pull_request.number }}" + - name: Execute Python Script Update env: CREDS_FILE: ${{ secrets.CREDS_FILE }} API_BASE_URL: ${{ secrets.API_BASE_URL }} run: | - echo ${{ github.event_name }} - echo ${{ github.event.client_payload.type }} - if [[ "${{ github.event_name }}" == "repository_dispatch" && "${{ github.event.client_payload.type }}" == "overwrite" ]]; then - python flow/util/updateRules.py --keyFile "${CREDS_FILE}" --apiURL ${API_BASE_URL} --commitSHA ${{ github.event.client_payload.commitsha }} --overwrite - else - python flow/util/updateRules.py --keyFile "${CREDS_FILE}" --apiURL ${API_BASE_URL} --commitSHA ${{ github.event.client_payload.commitsha }} + set -e + COMMIT_SHA=$(git rev-parse HEAD) + if [[ "${{ github.event.label.name }}" == "Update Rules Overwrite" ]]; then + python flow/util/updateRules.py --keyFile "${CREDS_FILE}" \ + --apiURL "${API_BASE_URL}" \ + --commitSHA "${COMMIT_SHA}" \ + --overwrite + else + python flow/util/updateRules.py --keyFile "${CREDS_FILE}" \ + --apiURL "${API_BASE_URL}" \ + --commitSHA "${COMMIT_SHA}" fi - - name: Push updated rules - id: remote-update + + - name: Check for changes and commit + id: check-and-commit run: | git config --local user.email "github-actions[bot]@users.noreply.github.com" git config --local user.name "github-actions[bot]" if [ -n "$(git status --porcelain)" ]; then - echo "has_update=true" >> "$GITHUB_OUTPUT" + git add . + git commit --signoff -m "flow: update rules" + echo "has_update=true" >> "$GITHUB_ENV" else - echo "has_update=false" >> "$GITHUB_OUTPUT" + echo "has_update=false" >> "$GITHUB_ENV" fi - git add . - git commit --signoff -m "flow: update rules based on new golden reference" - - if: "github.event.client_payload.branch != 'master'" - name: update rules pr - id: remote-update-pr + + - name: Push changes + if: env.has_update == 'true' run: | - git push origin "HEAD:refs/pull/${{ github.event.client_payload.branch }}/head" - - if: "steps.remote-update.outputs.has_update == 'true' && github.event.client_payload.branch == 'master'" - name: Create Draft PR - uses: peter-evans/create-pull-request@v5 - with: - token: ${{ github.token }} - signoff: true - delete-branch: true - title: "[BOT] Update rules" - reviewers: | - vvbandeira - maliberty - draft: true - branch: bot-update-rules - commit-message: | - [BOT] Update rules + git push diff --git a/.github/workflows/jenkins-check-build.sh b/.github/workflows/jenkins-check-build.sh new file mode 100755 index 0000000000..efde231717 --- /dev/null +++ b/.github/workflows/jenkins-check-build.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash + +# Check if exactly one argument is provided +if [ "$#" -ne 1 ]; then + echo "Error: Only argument required is the PR number." + exit 1 +fi + +# Check if the argument is numerical +if ! [[ "$1" =~ ^[0-9]+$ ]]; then + echo "Error: The argument must be a numerical value." + exit 1 +fi +PR_NUMBER="$1" + +JENKINS_URL="https://jenkins.openroad.tools/job/OpenROAD-flow-scripts-Public" +PR_BUILD_URL="${JENKINS_URL}/job/PR-${PR_NUMBER}-merge" + +while true; do + + if ! curl -s "${PR_BUILD_URL}/api/json?pretty" | jq . > /dev/null 2>&1 ; then + echo "Error while checking PR ${PR_NUMBER}" + exit 1 + fi + + echo "Checking if build is in queue..." + in_queue=$(curl -s "${PR_BUILD_URL}/api/json?pretty" | jq -r '.inQueue') + if [ "$in_queue" == "true" ]; then + echo "Build still in queue. Retrying..." + sleep 30 + continue + fi + echo "Build not in queue." + + echo "Getting latest build ID..." + LAST_BUILD_ID=$(curl -s "${PR_BUILD_URL}/api/json?pretty" | jq -r '.lastBuild.number') + if [ "$LAST_BUILD_ID" == "null" ]; then + echo "Build id is null." + exit 1 + fi + if [ -z "$LAST_BUILD_ID" ]; then + echo "No build found. Retrying..." + sleep 30 + continue + fi + echo "Latest build id ${LAST_BUILD_ID}" + + echo "Checking if build is done..." + BUILD_URL="${PR_BUILD_URL}/${LAST_BUILD_ID}/api/json?pretty" + in_progress=$(curl -s "$BUILD_URL" | jq -r '.inProgress') + if [ "$in_progress" == "false" ]; then + echo "Build complete. Running Python script..." + break + else + echo "Build still in progress. Waiting for completion..." + sleep 30 + fi + +done From 869b789dc42d990ad55806582e8e8c2ca158924c Mon Sep 17 00:00:00 2001 From: Vitor Bandeira Date: Mon, 29 Jul 2024 23:42:51 +0000 Subject: [PATCH 2/9] testing, revert before merge Signed-off-by: Vitor Bandeira --- .github/workflows/github-actions-update-rules.yml | 4 ++++ .github/workflows/jenkins-check-build.sh | 1 + 2 files changed, 5 insertions(+) diff --git a/.github/workflows/github-actions-update-rules.yml b/.github/workflows/github-actions-update-rules.yml index feacbf91e7..68f1eaf816 100644 --- a/.github/workflows/github-actions-update-rules.yml +++ b/.github/workflows/github-actions-update-rules.yml @@ -38,12 +38,16 @@ jobs: run: | set -e COMMIT_SHA=$(git rev-parse HEAD) + echo "PR has commit sha: $COMMIT_SHA" + COMMIT_SHA=abbc09da7f92f176a669e72d52653b80f074c12e if [[ "${{ github.event.label.name }}" == "Update Rules Overwrite" ]]; then + echo "Calling updateRules.py with overwrite." python flow/util/updateRules.py --keyFile "${CREDS_FILE}" \ --apiURL "${API_BASE_URL}" \ --commitSHA "${COMMIT_SHA}" \ --overwrite else + echo "Calling updateRules.py" python flow/util/updateRules.py --keyFile "${CREDS_FILE}" \ --apiURL "${API_BASE_URL}" \ --commitSHA "${COMMIT_SHA}" diff --git a/.github/workflows/jenkins-check-build.sh b/.github/workflows/jenkins-check-build.sh index efde231717..750a6b0900 100755 --- a/.github/workflows/jenkins-check-build.sh +++ b/.github/workflows/jenkins-check-build.sh @@ -34,6 +34,7 @@ while true; do echo "Getting latest build ID..." LAST_BUILD_ID=$(curl -s "${PR_BUILD_URL}/api/json?pretty" | jq -r '.lastBuild.number') + LAST_BUILD_ID=1 if [ "$LAST_BUILD_ID" == "null" ]; then echo "Build id is null." exit 1 From ef3f87d7a294534207fcc65135fbe7a0d4dc4889 Mon Sep 17 00:00:00 2001 From: Vitor Bandeira Date: Tue, 30 Jul 2024 00:13:45 +0000 Subject: [PATCH 3/9] fixup Signed-off-by: Vitor Bandeira --- .github/workflows/github-actions-update-rules.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/github-actions-update-rules.yml b/.github/workflows/github-actions-update-rules.yml index 68f1eaf816..3ef8e90f9e 100644 --- a/.github/workflows/github-actions-update-rules.yml +++ b/.github/workflows/github-actions-update-rules.yml @@ -42,7 +42,7 @@ jobs: COMMIT_SHA=abbc09da7f92f176a669e72d52653b80f074c12e if [[ "${{ github.event.label.name }}" == "Update Rules Overwrite" ]]; then echo "Calling updateRules.py with overwrite." - python flow/util/updateRules.py --keyFile "${CREDS_FILE}" \ + python flow/util/updateRules.py --keyFile ${CREDS_FILE} \ --apiURL "${API_BASE_URL}" \ --commitSHA "${COMMIT_SHA}" \ --overwrite From b489ad1e64dc347296fe18dcfede0592df3513fd Mon Sep 17 00:00:00 2001 From: Vitor Bandeira Date: Tue, 30 Jul 2024 00:20:29 +0000 Subject: [PATCH 4/9] gha: remove label at the end Signed-off-by: Vitor Bandeira --- .github/workflows/github-actions-update-rules.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/github-actions-update-rules.yml b/.github/workflows/github-actions-update-rules.yml index 3ef8e90f9e..0726c10e84 100644 --- a/.github/workflows/github-actions-update-rules.yml +++ b/.github/workflows/github-actions-update-rules.yml @@ -70,3 +70,10 @@ jobs: if: env.has_update == 'true' run: | git push + + - name: Remove label from PR + if: always() + uses: actions-ecosystem/action-remove-labels@v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + labels: "${{ github.event.label.name }}" From 26f6289d112b5830ed99a8b1b2dcb25ce1782bf5 Mon Sep 17 00:00:00 2001 From: Vitor Bandeira Date: Tue, 30 Jul 2024 00:26:53 +0000 Subject: [PATCH 5/9] gha: use file instead of string Signed-off-by: Vitor Bandeira --- .github/workflows/github-actions-update-rules.yml | 5 +++-- flow/util/updateRules.py | 11 ++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/.github/workflows/github-actions-update-rules.yml b/.github/workflows/github-actions-update-rules.yml index 0726c10e84..9fc1fb79f8 100644 --- a/.github/workflows/github-actions-update-rules.yml +++ b/.github/workflows/github-actions-update-rules.yml @@ -40,15 +40,16 @@ jobs: COMMIT_SHA=$(git rev-parse HEAD) echo "PR has commit sha: $COMMIT_SHA" COMMIT_SHA=abbc09da7f92f176a669e72d52653b80f074c12e + echo "${CREDS_FILE}" > creds.json if [[ "${{ github.event.label.name }}" == "Update Rules Overwrite" ]]; then echo "Calling updateRules.py with overwrite." - python flow/util/updateRules.py --keyFile ${CREDS_FILE} \ + python flow/util/updateRules.py --keyFile creds.json \ --apiURL "${API_BASE_URL}" \ --commitSHA "${COMMIT_SHA}" \ --overwrite else echo "Calling updateRules.py" - python flow/util/updateRules.py --keyFile "${CREDS_FILE}" \ + python flow/util/updateRules.py --keyFile creds.json \ --apiURL "${API_BASE_URL}" \ --commitSHA "${COMMIT_SHA}" fi diff --git a/flow/util/updateRules.py b/flow/util/updateRules.py index c565b2afbe..2f7f4ed560 100644 --- a/flow/util/updateRules.py +++ b/flow/util/updateRules.py @@ -11,9 +11,6 @@ from genRuleFile import update_rules from genRuleFile import get_metrics -# make sure the working dir is flow/ -os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) - # Create the argument parser parser = argparse.ArgumentParser(description='Process some integers.') @@ -26,7 +23,8 @@ args = parser.parse_args() # Initialize Firebase Admin SDK with service account credentials -cred = credentials.Certificate(json.loads(args.keyFile)) +with open(args.keyFile) as file: + cred = credentials.Certificate(json.load(file)) firebase_admin.initialize_app(cred) # Initialize Firestore client @@ -34,6 +32,9 @@ api_base_url = args.apiURL +# make sure the working dir is flow/ +os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) + runFilename = f'rules-base.json' for designsDir, dirs, files in sorted(os.walk('designs', topdown=False)): @@ -58,4 +59,4 @@ "base", # variant metrics, # metrics needed for update, default is {} in case of file args.overwrite # overwrite flag, default is false - ) \ No newline at end of file + ) From 05cf66d9e81684b13e6f78bda833a5947fdace22 Mon Sep 17 00:00:00 2001 From: Vitor Bandeira Date: Tue, 30 Jul 2024 00:35:23 +0000 Subject: [PATCH 6/9] test Signed-off-by: Vitor Bandeira --- .github/workflows/github-actions-update-rules.yml | 1 + flow/util/updateRules.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/.github/workflows/github-actions-update-rules.yml b/.github/workflows/github-actions-update-rules.yml index 9fc1fb79f8..2527b95ae6 100644 --- a/.github/workflows/github-actions-update-rules.yml +++ b/.github/workflows/github-actions-update-rules.yml @@ -41,6 +41,7 @@ jobs: echo "PR has commit sha: $COMMIT_SHA" COMMIT_SHA=abbc09da7f92f176a669e72d52653b80f074c12e echo "${CREDS_FILE}" > creds.json + cat creds.json if [[ "${{ github.event.label.name }}" == "Update Rules Overwrite" ]]; then echo "Calling updateRules.py with overwrite." python flow/util/updateRules.py --keyFile creds.json \ diff --git a/flow/util/updateRules.py b/flow/util/updateRules.py index 2f7f4ed560..bda6516afb 100644 --- a/flow/util/updateRules.py +++ b/flow/util/updateRules.py @@ -24,6 +24,8 @@ # Initialize Firebase Admin SDK with service account credentials with open(args.keyFile) as file: + print(args.keyFile) + print(file.read()) cred = credentials.Certificate(json.load(file)) firebase_admin.initialize_app(cred) @@ -37,6 +39,8 @@ runFilename = f'rules-base.json' +exit(0) + for designsDir, dirs, files in sorted(os.walk('designs', topdown=False)): dirList = designsDir.split(os.sep) if len(dirList) != 3: From 8ebba809632ce95100f252d4f12b6202ae18b4c5 Mon Sep 17 00:00:00 2001 From: Vitor Bandeira Date: Tue, 30 Jul 2024 00:39:56 +0000 Subject: [PATCH 7/9] fix-label-name Signed-off-by: Vitor Bandeira --- .github/workflows/github-actions-update-rules.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/github-actions-update-rules.yml b/.github/workflows/github-actions-update-rules.yml index 2527b95ae6..b1518ff338 100644 --- a/.github/workflows/github-actions-update-rules.yml +++ b/.github/workflows/github-actions-update-rules.yml @@ -76,6 +76,8 @@ jobs: - name: Remove label from PR if: always() uses: actions-ecosystem/action-remove-labels@v1 + env: + LABEL: ${{ github.event.label.name }} with: github_token: ${{ secrets.GITHUB_TOKEN }} - labels: "${{ github.event.label.name }}" + labels: "${LABEL}" From 548bb740c893d9bdc874a2c4ef8bee61a23950bc Mon Sep 17 00:00:00 2001 From: Vitor Bandeira Date: Tue, 30 Jul 2024 00:43:00 +0000 Subject: [PATCH 8/9] test Signed-off-by: Vitor Bandeira --- flow/util/updateRules.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/flow/util/updateRules.py b/flow/util/updateRules.py index bda6516afb..b0d8730dbe 100644 --- a/flow/util/updateRules.py +++ b/flow/util/updateRules.py @@ -24,8 +24,6 @@ # Initialize Firebase Admin SDK with service account credentials with open(args.keyFile) as file: - print(args.keyFile) - print(file.read()) cred = credentials.Certificate(json.load(file)) firebase_admin.initialize_app(cred) From d056ad44ed09ff06c1c88568b9ecd969f09917ef Mon Sep 17 00:00:00 2001 From: Vitor Bandeira Date: Tue, 30 Jul 2024 00:49:17 +0000 Subject: [PATCH 9/9] test Signed-off-by: Vitor Bandeira --- .github/workflows/github-actions-update-rules.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/github-actions-update-rules.yml b/.github/workflows/github-actions-update-rules.yml index b1518ff338..2748dd3130 100644 --- a/.github/workflows/github-actions-update-rules.yml +++ b/.github/workflows/github-actions-update-rules.yml @@ -41,6 +41,7 @@ jobs: echo "PR has commit sha: $COMMIT_SHA" COMMIT_SHA=abbc09da7f92f176a669e72d52653b80f074c12e echo "${CREDS_FILE}" > creds.json + ls -lh creds.json cat creds.json if [[ "${{ github.event.label.name }}" == "Update Rules Overwrite" ]]; then echo "Calling updateRules.py with overwrite."