Skip to content

Commit b584676

Browse files
authored
Add missing actions and scripts (#50)
1 parent 08ff3de commit b584676

File tree

14 files changed

+298437
-0
lines changed

14 files changed

+298437
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
#
18+
---
19+
name: "Approve Workflow Run"
20+
description: "Approve a Workflow run that has been submitted by a non-committer"
21+
inputs:
22+
gh-token:
23+
description: "The GitHub token for use with the CLI"
24+
required: true
25+
repository:
26+
description: "The GitHub repository"
27+
required: true
28+
default: "apache/kafka"
29+
run_id:
30+
description: "The Workflow Run ID"
31+
required: true
32+
pr_number:
33+
description: "The Pull Request number"
34+
required: true
35+
commit_sha:
36+
description: "The SHA of the commit the run is for"
37+
required: true
38+
39+
runs:
40+
using: "composite"
41+
steps:
42+
- name: Approve Workflow Run
43+
shell: bash
44+
env:
45+
GH_TOKEN: ${{ inputs.gh-token }}
46+
REPO: ${{ inputs.repository }}
47+
RUN_ID: ${{ inputs.run_id }}
48+
PR_NUMBER: ${{ inputs.pr_number }}
49+
COMMIT_SHA: ${{ inputs.commit_sha }}
50+
run: |
51+
echo "Approving workflow run $RUN_ID for PR $PR_NUMBER at SHA $COMMIT_SHA";
52+
gh api --method POST \
53+
-H 'Accept: application/vnd.github+json' \
54+
-H 'X-GitHub-Api-Version: 2022-11-28' \
55+
/repos/$REPO/actions/runs/$RUN_ID/approve
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
#
18+
---
19+
name: "Update Commit Status Check"
20+
description: "Update the status of a commit check using the GH CLI"
21+
inputs:
22+
# Composite actions do not support typed parameters. Everything is treated as a string
23+
# See: https://github.com/actions/runner/issues/2238
24+
gh-token:
25+
description: "The GitHub token for use with the CLI"
26+
required: true
27+
repository:
28+
description: "The GitHub repository"
29+
required: true
30+
default: "apache/kafka"
31+
commit_sha:
32+
description: "The SHA of the commit we are updating"
33+
required: true
34+
url:
35+
description: "The URL of the status check"
36+
required: false
37+
default: ""
38+
description:
39+
description: "The text to display next to the check"
40+
default: ""
41+
required: false
42+
context:
43+
description: "The name of the status check"
44+
required: true
45+
state:
46+
description: "The state of the check. Can be one of: error, failure, pending, success"
47+
required: true
48+
49+
runs:
50+
using: "composite"
51+
steps:
52+
- name: Update Check
53+
shell: bash
54+
env:
55+
GH_TOKEN: ${{ inputs.gh-token }}
56+
REPO: ${{ inputs.repository }}
57+
COMMIT_SHA: ${{ inputs.commit_sha }}
58+
STATE: ${{ inputs.state }}
59+
URL: ${{ inputs.url }}
60+
DESCRIPTION: ${{ inputs.description }}
61+
CONTEXT: ${{ inputs.context }}
62+
run: |
63+
gh api --method POST -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \
64+
/repos/$REPO/statuses/$COMMIT_SHA \
65+
-f "state=$STATE" -f "target_url=$URL" \
66+
-f "description=$DESCRIPTION" \
67+
-f "context=$CONTEXT"
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
#
18+
---
19+
name: "Run Tests with Gradle"
20+
description: "Run specified Gradle test tasks with configuration for timeout and test catalog."
21+
inputs:
22+
# Composite actions do not support typed parameters. Everything is treated as a string
23+
# See: https://github.com/actions/runner/issues/2238
24+
test-task:
25+
description: "The test suite to run. Either 'test' or 'quarantinedTest'."
26+
required: true
27+
timeout-minutes:
28+
description: "The timeout for the tests, in minutes."
29+
required: true
30+
test-catalog-path:
31+
description: "The file path of the test catalog file."
32+
required: true
33+
build-scan-artifact-name:
34+
description: "The name to use for archiving the build scan."
35+
required: true
36+
outputs:
37+
gradle-exitcode:
38+
description: "The result of the Gradle test task."
39+
value: ${{ steps.run-tests.outputs.exitcode }}
40+
runs:
41+
using: "composite"
42+
steps:
43+
- name: Run JUnit Tests (${{ inputs.test-task }})
44+
# Gradle flags
45+
# --build-cache: Let Gradle restore the build cache
46+
# --no-scan: Don't attempt to publish the scan yet. We want to archive it first.
47+
# --continue: Keep running even if a test fails
48+
# -PcommitId Prevent the Git SHA being written into the jar files (which breaks caching)
49+
shell: bash
50+
id: run-tests
51+
env:
52+
TIMEOUT_MINUTES: ${{ inputs.timeout-minutes}}
53+
TEST_CATALOG: ${{ inputs.test-catalog-path }}
54+
TEST_TASK: ${{ inputs.test-task }}
55+
run: |
56+
set +e
57+
./.github/scripts/thread-dump.sh &
58+
timeout ${TIMEOUT_MINUTES}m ./gradlew --build-cache --continue --no-scan \
59+
-PtestLoggingEvents=started,passed,skipped,failed \
60+
-PmaxParallelForks=2 \
61+
-PmaxTestRetries=1 -PmaxTestRetryFailures=10 \
62+
-PmaxQuarantineTestRetries=3 -PmaxQuarantineTestRetryFailures=0 \
63+
-Pkafka.test.catalog.file=$TEST_CATALOG \
64+
-PcommitId=xxxxxxxxxxxxxxxx \
65+
$TEST_TASK
66+
exitcode="$?"
67+
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT
68+
- name: Archive build scan (${{ inputs.test-task }})
69+
if: always()
70+
uses: actions/upload-artifact@v4
71+
with:
72+
name: ${{ inputs.build-scan-artifact-name }}
73+
path: ~/.gradle/build-scan-data
74+
compression-level: 9
75+
if-no-files-found: ignore
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
#
18+
---
19+
name: "Gradle Setup"
20+
description: "Setup Java and Gradle"
21+
inputs:
22+
# Composite actions do not support typed parameters. Everything is treated as a string
23+
# See: https://github.com/actions/runner/issues/2238
24+
java-version:
25+
description: "Java version to use"
26+
default: "17"
27+
gradle-cache-read-only:
28+
description: "Should the Gradle cache be read-only?"
29+
default: "true"
30+
gradle-cache-write-only:
31+
description: "Should the Gradle cache be write-only?"
32+
default: "false"
33+
develocity-access-key:
34+
description: "Optional access key for uploading build scans to Develocity"
35+
default: ""
36+
runs:
37+
using: "composite"
38+
steps:
39+
- name: Setup Java
40+
uses: actions/setup-java@v4
41+
with:
42+
distribution: temurin
43+
java-version: ${{ inputs.java-version }}
44+
- name: Setup Gradle
45+
uses: gradle/actions/setup-gradle@d156388eb19639ec20ade50009f3d199ce1e2808 # v4.1.0
46+
env:
47+
GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED: true
48+
with:
49+
gradle-version: wrapper
50+
develocity-access-key: ${{ inputs.develocity-access-key }}
51+
develocity-token-expiry: 4
52+
cache-read-only: ${{ inputs.gradle-cache-read-only }}
53+
cache-write-only: ${{ inputs.gradle-cache-write-only }}
54+
# Cache downloaded JDKs in addition to the default directories.
55+
gradle-home-cache-includes: |
56+
caches
57+
notifications
58+
jdks
59+
cache-cleanup: on-success
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
#
18+
---
19+
name: "Python Setup"
20+
description: "Setup Python and install dependencies"
21+
runs:
22+
using: "composite"
23+
steps:
24+
- name: Setup Python
25+
uses: actions/setup-python@v5
26+
with:
27+
python-version: 3.12
28+
- name: Pip install
29+
shell: bash
30+
run: pip install -r .github/scripts/requirements.txt

.github/scripts/checkstyle.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one or more
2+
# contributor license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright ownership.
4+
# The ASF licenses this file to You under the Apache License, Version 2.0
5+
# (the "License"); you may not use this file except in compliance with
6+
# the License. You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from glob import glob
17+
import logging
18+
import os
19+
import os.path
20+
import sys
21+
from typing import Tuple, Optional
22+
import xml.etree.ElementTree
23+
24+
25+
logger = logging.getLogger()
26+
logger.setLevel(logging.DEBUG)
27+
handler = logging.StreamHandler(sys.stderr)
28+
handler.setLevel(logging.DEBUG)
29+
logger.addHandler(handler)
30+
31+
32+
def get_env(key: str) -> str:
33+
value = os.getenv(key)
34+
logger.debug(f"Read env {key}: {value}")
35+
return value
36+
37+
38+
def parse_report(workspace_path, fp) -> Tuple[int, int]:
39+
stack = []
40+
errors = []
41+
file_count = 0
42+
error_count = 0
43+
for (event, elem) in xml.etree.ElementTree.iterparse(fp, events=["start", "end"]):
44+
if event == "start":
45+
stack.append(elem)
46+
if elem.tag == "file":
47+
file_count += 1
48+
errors.clear()
49+
if elem.tag == "error":
50+
logger.debug(f"Found checkstyle error: {elem.attrib}")
51+
errors.append(elem)
52+
error_count += 1
53+
elif event == "end":
54+
if elem.tag == "file" and len(errors) > 0:
55+
filename = elem.get("name")
56+
rel_path = os.path.relpath(filename, workspace_path)
57+
logger.debug(f"Outputting errors for file: {elem.attrib}")
58+
for error in errors:
59+
line = error.get("line")
60+
col = error.get("column")
61+
severity = error.get("severity")
62+
message = error.get('message')
63+
title = f"Checkstyle {severity}"
64+
print(f"::notice file={rel_path},line={line},col={col},title={title}::{message}")
65+
stack.pop()
66+
else:
67+
logger.error(f"Unhandled xml event {event}: {elem}")
68+
return file_count, error_count
69+
70+
71+
if __name__ == "__main__":
72+
"""
73+
Parse checkstyle XML reports and generate GitHub annotations.
74+
75+
See: https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#setting-a-notice-message
76+
"""
77+
if not os.getenv("GITHUB_WORKSPACE"):
78+
print("This script is intended to by run by GitHub Actions.")
79+
exit(1)
80+
81+
reports = glob(pathname="**/checkstyle/*.xml", recursive=True)
82+
logger.debug(f"Found {len(reports)} checkstyle reports")
83+
total_file_count = 0
84+
total_error_count = 0
85+
86+
workspace_path = get_env("GITHUB_WORKSPACE") # e.g., /home/runner/work/apache/kafka
87+
88+
for report in reports:
89+
with open(report, "r") as fp:
90+
logger.debug(f"Parsing report file: {report}")
91+
file_count, error_count = parse_report(workspace_path, fp)
92+
if error_count == 1:
93+
logger.debug(f"Checked {file_count} files from {report} and found 1 error")
94+
else:
95+
logger.debug(f"Checked {file_count} files from {report} and found {error_count} errors")
96+
total_file_count += file_count
97+
total_error_count += error_count
98+
exit(0)

0 commit comments

Comments
 (0)