Skip to content

Commit 862a020

Browse files
committed
Add GitCompare workflow and CommitHunter script for build comparison
This implementation have added workflow which is for to run the github action and have script file which is to parse the java -version out of the comment in the pull request. Fixes: #1043 Signed-off-by: Prashantkumar Khatri <khatri2105104@st.jmi.ac.in> Moved commit_hunter.sh from .github to root folder Enhance GitCompare workflow: add error handling, improve output logging, and ensure permissions for pull requests Update GitCompare command parsing: change command prefix to '.ch_gitcompare' and improve error messages for invalid formats
1 parent aeae2c6 commit 862a020

File tree

2 files changed

+244
-0
lines changed

2 files changed

+244
-0
lines changed

.github/workflows/gitcompare.yml

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
name: GitCompare
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
jobs:
8+
parse-comment:
9+
if: github.event.issue.pull_request && startsWith(github.event.comment.body, '.ch_gitcompare')
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
issues: write
14+
pull-requests: write
15+
outputs:
16+
good_build: ${{ steps.parse.outputs.good_build }}
17+
bad_build: ${{ steps.parse.outputs.bad_build }}
18+
steps:
19+
- name: Parse comment
20+
id: parse
21+
env:
22+
COMMENT_BODY: ${{ github.event.comment.body }}
23+
run: |
24+
echo "Comment body: $COMMENT_BODY"
25+
26+
# Extract good_build and bad_build from the command
27+
# Remove the .ch_gitcompare command
28+
COMMENT=${COMMENT_BODY#.ch_gitcompare}
29+
30+
# Parse --good_build argument (capture everything until --bad_build)
31+
if [[ $COMMENT =~ --good_build[[:space:]]+(.*)[[:space:]]+--bad_build[[:space:]]+(.*) ]]; then
32+
GOOD_BUILD="${BASH_REMATCH[1]}"
33+
BAD_BUILD="${BASH_REMATCH[2]}"
34+
else
35+
echo "Error: Invalid format. Please use: .ch_gitcompare --good_build <good_build> --bad_build <bad_build>"
36+
exit 1
37+
fi
38+
39+
# Clean up whitespace and remove outer quotes
40+
GOOD_BUILD=$(echo "$GOOD_BUILD" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | sed 's/^"//;s/"$//')
41+
BAD_BUILD=$(echo "$BAD_BUILD" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | sed 's/^"//;s/"$//')
42+
43+
echo "Good build: '$GOOD_BUILD'"
44+
echo "Bad build: '$BAD_BUILD'"
45+
46+
# Validate builds
47+
if [ -z "$GOOD_BUILD" ] || [ -z "$BAD_BUILD" ]; then
48+
echo "Error: Invalid format. Please use: .ch_gitcompare --good_build <good_build> --bad_build <bad_build>"
49+
exit 1
50+
fi
51+
52+
# Set outputs
53+
{
54+
echo "good_build<<EOF"
55+
echo "$GOOD_BUILD"
56+
echo "EOF"
57+
} >> $GITHUB_OUTPUT
58+
59+
{
60+
echo "bad_build<<EOF"
61+
echo "$BAD_BUILD"
62+
echo "EOF"
63+
} >> $GITHUB_OUTPUT
64+
65+
compare:
66+
needs: parse-comment
67+
runs-on: ubuntu-latest
68+
permissions:
69+
contents: read
70+
issues: write
71+
pull-requests: write
72+
steps:
73+
- name: Checkout repository
74+
uses: actions/checkout@v4
75+
76+
- name: Run CommitHunter
77+
id: run_commit_hunter
78+
env:
79+
GOOD_BUILD: ${{ needs.parse-comment.outputs.good_build }}
80+
BAD_BUILD: ${{ needs.parse-comment.outputs.bad_build }}
81+
run: |
82+
cd CommitHunter
83+
echo "Running commit_hunter.sh with:"
84+
echo "Good build: $GOOD_BUILD"
85+
echo "Bad build: $BAD_BUILD"
86+
bash commit_hunter.sh "$GOOD_BUILD" "$BAD_BUILD" > output.txt
87+
echo "CommitHunter output:"
88+
cat output.txt
89+
90+
- name: Parse URLs from output
91+
id: parse_urls
92+
run: |
93+
if [ -f "./CommitHunter/output.txt" ]; then
94+
echo "Found output file, parsing URLs..."
95+
cat ./CommitHunter/output.txt
96+
97+
url_openj9=$(grep "OpenJ9:" ./CommitHunter/output.txt | awk '{print $2}')
98+
url_omr=$(grep "OMR:" ./CommitHunter/output.txt | awk '{print $2}')
99+
url_jcl=$(grep "JCL:" ./CommitHunter/output.txt | awk '{print $2}')
100+
101+
echo "Parsed URLs:"
102+
echo "OpenJ9: $url_openj9"
103+
echo "OMR: $url_omr"
104+
echo "JCL: $url_jcl"
105+
106+
echo "url_openj9=$url_openj9" >> $GITHUB_OUTPUT
107+
echo "url_omr=$url_omr" >> $GITHUB_OUTPUT
108+
echo "url_jcl=$url_jcl" >> $GITHUB_OUTPUT
109+
else
110+
echo "Error: output.txt not found in CommitHunter directory"
111+
ls -la ./CommitHunter/
112+
exit 1
113+
fi
114+
115+
- name: Prepare comment
116+
run: |
117+
echo "## 🔍 GitCompare Results" > comment.md
118+
echo "" >> comment.md
119+
echo "### OpenJ9 Changes" >> comment.md
120+
echo "${{ steps.parse_urls.outputs.url_openj9 }}" >> comment.md
121+
echo "" >> comment.md
122+
echo "### OMR Changes" >> comment.md
123+
echo "${{ steps.parse_urls.outputs.url_omr }}" >> comment.md
124+
echo "" >> comment.md
125+
echo "### JCL Changes" >> comment.md
126+
echo "${{ steps.parse_urls.outputs.url_jcl }}" >> comment.md
127+
echo "" >> comment.md
128+
echo "> 💡 **Note:** These links show the differences between the specified builds."
129+
130+
echo "Prepared comment:"
131+
cat comment.md
132+
133+
- name: Comment on PR
134+
env:
135+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
136+
GITHUB_REPOSITORY: ${{ github.repository }}
137+
ISSUE_NUMBER: ${{ github.event.issue.number }}
138+
run: |
139+
pip install PyGithub
140+
python3 <<EOF
141+
import os
142+
import sys
143+
from github import Github
144+
145+
try:
146+
with open("comment.md", "r", encoding="utf-8") as f:
147+
comment_body = f.read()
148+
149+
token = os.environ["GITHUB_TOKEN"]
150+
repo_name = os.environ["GITHUB_REPOSITORY"]
151+
issue_number = int(os.environ["ISSUE_NUMBER"])
152+
153+
g = Github(token)
154+
repo = g.get_repo(repo_name)
155+
issue = repo.get_issue(number=issue_number)
156+
157+
comment = issue.create_comment(comment_body)
158+
print(f"✅ Comment created successfully: {comment.html_url}")
159+
160+
except Exception as e:
161+
print(f"❌ Error creating comment: {e}")
162+
print(f"Error type: {type(e).__name__}")
163+
sys.exit(1)
164+
EOF

CommitHunter/commit_hunter.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
3+
parse_java_version() {
4+
local version_output="$1"
5+
local result=""
6+
7+
# Debug output
8+
echo "Parsing version output: $version_output" >&2
9+
10+
if [[ $version_output =~ openjdk[[:space:]]+version[[:space:]]+\"?([0-9]+)\.[0-9]+\.[0-9]+-beta ]]; then
11+
result+="openjdk_version=${BASH_REMATCH[1]}\n"
12+
fi
13+
14+
if [[ $version_output =~ OpenJ9[[:space:]]*-[[:space:]]*([0-9a-f]+) ]]; then
15+
result+="openj9_commit=${BASH_REMATCH[1]}\n"
16+
fi
17+
18+
if [[ $version_output =~ OMR[[:space:]]*-[[:space:]]*([0-9a-f]+) ]]; then
19+
result+="omr_commit=${BASH_REMATCH[1]}\n"
20+
fi
21+
22+
if [[ $version_output =~ JCL[[:space:]]*-[[:space:]]*([0-9a-f]+) ]]; then
23+
result+="jcl_commit=${BASH_REMATCH[1]}\n"
24+
fi
25+
26+
echo -e "$result"
27+
}
28+
29+
generate_compare_urls() {
30+
local good_info="$1"
31+
local bad_info="$2"
32+
33+
declare -A good
34+
declare -A bad
35+
36+
while IFS='=' read -r key value; do
37+
if [[ -n $key ]]; then
38+
good[$key]="$value"
39+
fi
40+
done <<< "$good_info"
41+
42+
while IFS='=' read -r key value; do
43+
if [[ -n $key ]]; then
44+
bad[$key]="$value"
45+
fi
46+
done <<< "$bad_info"
47+
48+
echo "OpenJ9: https://github.com/eclipse-openj9/openj9/compare/${good[openj9_commit]}...${bad[openj9_commit]}"
49+
echo "OMR: https://github.com/eclipse-omr/omr/compare/${good[omr_commit]}...${bad[omr_commit]}"
50+
echo "JCL: https://github.com/ibmruntimes/openj9-openjdk-jdk${good[openjdk_version]}/compare/${good[jcl_commit]}...${bad[jcl_commit]}"
51+
}
52+
53+
if [ $# -ne 2 ]; then
54+
echo "Usage: $0 <good_build> <bad_build>"
55+
exit 1
56+
fi
57+
58+
GOOD_BUILD="$1"
59+
BAD_BUILD="$2"
60+
61+
# Debug output
62+
echo "Good build: $GOOD_BUILD" >&2
63+
echo "Bad build: $BAD_BUILD" >&2
64+
65+
GOOD_INFO=$(parse_java_version "$GOOD_BUILD")
66+
BAD_INFO=$(parse_java_version "$BAD_BUILD")
67+
68+
# Debug output
69+
echo "Good info: $GOOD_INFO" >&2
70+
echo "Bad info: $BAD_INFO" >&2
71+
72+
required_fields=("openjdk_version" "openj9_commit" "omr_commit" "jcl_commit")
73+
for field in "${required_fields[@]}"; do
74+
if ! echo "$GOOD_INFO" | grep -q "^$field=" || ! echo "$BAD_INFO" | grep -q "^$field="; then
75+
echo "Error: Missing required field: $field" >&2
76+
exit 1
77+
fi
78+
done
79+
80+
generate_compare_urls "$GOOD_INFO" "$BAD_INFO"

0 commit comments

Comments
 (0)