Skip to content

Commit 6c6c817

Browse files
committed
Added action
1 parent c8bbc63 commit 6c6c817

3 files changed

Lines changed: 154 additions & 68 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: JAIPilot Generate
2+
3+
on:
4+
pull_request:
5+
types:
6+
- opened
7+
- synchronize
8+
- reopened
9+
10+
permissions:
11+
contents: write
12+
pull-requests: write
13+
14+
concurrency:
15+
group: jaipilot-generate-${{ github.event.pull_request.number }}
16+
cancel-in-progress: true
17+
18+
jobs:
19+
generate-tests:
20+
# Skip bot-triggered reruns to avoid infinite loops, and skip fork PRs
21+
# because this workflow pushes commits back to the source branch.
22+
if: ${{ github.actor != 'github-actions[bot]' && github.event.pull_request.head.repo.full_name == github.repository }}
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: Checkout PR branch
26+
uses: actions/checkout@v4
27+
with:
28+
ref: ${{ github.head_ref }}
29+
30+
- name: Run JAIPilot generate and push changes
31+
uses: ./
32+
with:
33+
jaipilot-license-key: ${{ secrets.JAIPILOT_LICENSE_KEY }}

action.yml

Lines changed: 106 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
name: "JAIPilot PR Test Action"
2-
description: "Run JAIPilot on pull requests, execute verification, and optionally send results to an API."
1+
name: "JAIPilot Generate and Commit"
2+
description: "Run jaipilot generate for all non-test Java classes and commit generated tests back to the same branch."
33
author: "JAIPilot"
44
branding:
55
icon: "check-circle"
@@ -18,43 +18,37 @@ inputs:
1818
description: "Installer script URL for JAIPilot."
1919
required: false
2020
default: "https://jaipilot.com/install.sh"
21-
jaipilot-command:
22-
description: "JAIPilot command to run (for example: verify, generate, fix)."
23-
required: false
24-
default: "verify"
25-
jaipilot-args:
26-
description: "Space-separated additional arguments passed to JAIPilot."
27-
required: false
28-
default: ""
29-
jaipilot-jwt-token:
30-
description: "Optional JAIPILOT_JWT_TOKEN for backend-assisted commands."
21+
jaipilot-license-key:
22+
description: "JAIPILOT_LICENSE_KEY used for backend-assisted generate."
3123
required: false
3224
default: ""
33-
fail-on-error:
34-
description: "If true, fail the workflow when JAIPilot exits non-zero."
25+
fail-on-generate-error:
26+
description: "If true, fail the workflow when one or more classes fail generation."
3527
required: false
3628
default: "true"
37-
api-callback-url:
38-
description: "Optional callback URL to receive a result payload."
29+
commit-message:
30+
description: "Commit message used for generated test changes."
3931
required: false
40-
default: ""
41-
api-callback-method:
42-
description: "HTTP method for callback request."
32+
default: "chore: jaipilot generate tests"
33+
git-user-name:
34+
description: "Git author name used for the generated commit."
4335
required: false
44-
default: "POST"
45-
api-callback-token:
46-
description: "Optional bearer token for callback request."
36+
default: "github-actions[bot]"
37+
git-user-email:
38+
description: "Git author email used for the generated commit."
4739
required: false
48-
default: ""
49-
api-callback-body:
50-
description: "JSON request body sent to callback endpoint."
51-
required: false
52-
default: "{}"
40+
default: "41898282+github-actions[bot]@users.noreply.github.com"
5341

5442
outputs:
55-
exit-code:
56-
description: "JAIPilot process exit code."
57-
value: ${{ steps.run-jaipilot.outputs.exit-code }}
43+
processed-classes:
44+
description: "Number of non-test Java classes processed."
45+
value: ${{ steps.generate.outputs.processed-classes }}
46+
failed-classes:
47+
description: "Number of classes that failed generation."
48+
value: ${{ steps.generate.outputs.failed-classes }}
49+
commit-sha:
50+
description: "Commit SHA pushed by this action. Empty when no changes were committed."
51+
value: ${{ steps.commit.outputs.commit-sha }}
5852

5953
runs:
6054
using: "composite"
@@ -72,8 +66,8 @@ runs:
7266
curl -fsSL "${{ inputs.install-script-url }}" | bash
7367
echo "${HOME}/.local/bin" >> "${GITHUB_PATH}"
7468
75-
- name: Run JAIPilot
76-
id: run-jaipilot
69+
- name: Run jaipilot generate for all non-test Java classes
70+
id: generate
7771
shell: bash
7872
run: |
7973
set -euo pipefail
@@ -85,47 +79,97 @@ runs:
8579
fi
8680
cd "$workdir"
8781
88-
if [ -n "${{ inputs.jaipilot-jwt-token }}" ]; then
89-
export JAIPILOT_JWT_TOKEN="${{ inputs.jaipilot-jwt-token }}"
82+
if [ -n "${{ inputs.jaipilot-license-key }}" ]; then
83+
export JAIPILOT_LICENSE_KEY="${{ inputs.jaipilot-license-key }}"
84+
# jaipilot generate reads JAIPILOT_JWT_TOKEN today; map license key input to it.
85+
export JAIPILOT_JWT_TOKEN="${{ inputs.jaipilot-license-key }}"
86+
fi
87+
if [ -n "${JAIPILOT_LICENSE_KEY:-}" ] && [ -z "${JAIPILOT_JWT_TOKEN:-}" ]; then
88+
# Allow callers to provide JAIPILOT_LICENSE_KEY directly at workflow level.
89+
export JAIPILOT_JWT_TOKEN="${JAIPILOT_LICENSE_KEY}"
90+
fi
91+
if [ -z "${JAIPILOT_JWT_TOKEN:-}" ]; then
92+
echo "Missing JAIPILOT_LICENSE_KEY. Set input 'jaipilot-license-key' or JAIPILOT_LICENSE_KEY env var." >&2
93+
exit 1
9094
fi
9195
92-
command_name="${{ inputs.jaipilot-command }}"
93-
args_raw="${{ inputs.jaipilot-args }}"
96+
mapfile -t java_files < <(
97+
find . -type f -name "*.java" \
98+
! -path "*/src/test/*" \
99+
! -name "*Test.java" \
100+
! -name "*Tests.java" \
101+
! -name "*IT.java" \
102+
! -name "*ITCase.java" \
103+
| LC_ALL=C sort
104+
)
94105
95-
set +e
96-
if [ -n "$args_raw" ]; then
97-
read -r -a args <<< "$args_raw"
98-
jaipilot "$command_name" "${args[@]}"
99-
else
100-
jaipilot "$command_name"
106+
if [ "${#java_files[@]}" -eq 0 ]; then
107+
echo "No non-test Java classes found."
108+
echo "processed-classes=0" >> "${GITHUB_OUTPUT}"
109+
echo "failed-classes=0" >> "${GITHUB_OUTPUT}"
110+
echo "JAIPILOT_GENERATE_FAILED=0" >> "${GITHUB_ENV}"
111+
exit 0
101112
fi
102-
exit_code=$?
103-
set -e
104113
105-
echo "exit-code=$exit_code" >> "${GITHUB_OUTPUT}"
114+
processed=0
115+
failed=0
106116
107-
if [ "$exit_code" -ne 0 ] && [ "${{ inputs.fail-on-error }}" = "true" ]; then
108-
exit "$exit_code"
109-
fi
117+
for class_path in "${java_files[@]}"; do
118+
processed=$((processed + 1))
119+
echo "[$processed/${#java_files[@]}] jaipilot generate $class_path"
120+
if ! jaipilot generate "$class_path"; then
121+
failed=$((failed + 1))
122+
echo "Generation failed for: $class_path" >&2
123+
fi
124+
done
110125
111-
- name: Send optional callback
112-
if: ${{ inputs.api-callback-url != '' }}
126+
echo "processed-classes=$processed" >> "${GITHUB_OUTPUT}"
127+
echo "failed-classes=$failed" >> "${GITHUB_OUTPUT}"
128+
echo "JAIPILOT_GENERATE_FAILED=$failed" >> "${GITHUB_ENV}"
129+
130+
- name: Commit and push generated changes
131+
id: commit
113132
shell: bash
114133
run: |
115134
set -euo pipefail
116135
117-
callback_url="${{ inputs.api-callback-url }}"
118-
callback_method="${{ inputs.api-callback-method }}"
119-
callback_body="${{ inputs.api-callback-body }}"
120-
callback_token="${{ inputs.api-callback-token }}"
136+
workdir="${{ inputs.working-directory }}"
137+
cd "$workdir"
138+
139+
if [ -z "$(git status --porcelain)" ]; then
140+
echo "No generated changes to commit."
141+
echo "commit-sha=" >> "${GITHUB_OUTPUT}"
142+
exit 0
143+
fi
144+
145+
git config user.name "${{ inputs.git-user-name }}"
146+
git config user.email "${{ inputs.git-user-email }}"
121147
122-
auth_header=()
123-
if [ -n "$callback_token" ]; then
124-
auth_header=(-H "Authorization: Bearer $callback_token")
148+
git add -A
149+
if git diff --cached --quiet; then
150+
echo "No staged changes after git add."
151+
echo "commit-sha=" >> "${GITHUB_OUTPUT}"
152+
exit 0
153+
fi
154+
155+
git commit -m "${{ inputs.commit-message }}"
156+
157+
target_branch="${GITHUB_HEAD_REF:-${GITHUB_REF_NAME:-}}"
158+
if [ -z "$target_branch" ]; then
159+
echo "Unable to detect target branch (GITHUB_HEAD_REF/GITHUB_REF_NAME empty)." >&2
160+
exit 1
125161
fi
126162
127-
curl -fsS -X "$callback_method" \
128-
-H "Content-Type: application/json" \
129-
"${auth_header[@]}" \
130-
--data "$callback_body" \
131-
"$callback_url"
163+
git push origin "HEAD:${target_branch}"
164+
echo "commit-sha=$(git rev-parse HEAD)" >> "${GITHUB_OUTPUT}"
165+
166+
- name: Fail when generation errors occurred
167+
if: ${{ inputs.fail-on-generate-error == 'true' }}
168+
shell: bash
169+
run: |
170+
set -euo pipefail
171+
failed="${JAIPILOT_GENERATE_FAILED:-0}"
172+
if [ "$failed" -gt 0 ]; then
173+
echo "jaipilot generate failed for $failed classes." >&2
174+
exit 1
175+
fi

docs/github-action-publishing.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# JAIPilot GitHub Action: Create and Publish
1+
# JAIPilot GitHub Action: Generate and Publish
22

33
This repository now includes a publishable root Action in [`action.yml`](../action.yml).
44

55
## Use the action in a workflow
66

77
```yaml
8-
name: JAIPilot PR Check
8+
name: JAIPilot Generate Tests
99

1010
on:
1111
pull_request:
@@ -15,20 +15,29 @@ jobs:
1515
jaipilot:
1616
runs-on: ubuntu-latest
1717
permissions:
18-
contents: read
18+
contents: write
1919
pull-requests: write
2020

2121
steps:
2222
- name: Checkout
2323
uses: actions/checkout@v4
24+
with:
25+
# Needed so the action can commit to the PR branch.
26+
ref: ${{ github.head_ref }}
2427

25-
- name: Run JAIPilot
28+
- name: Run JAIPilot generate for all non-test Java classes
2629
uses: <OWNER>/<REPO>@action-v1
2730
with:
28-
jaipilot-command: verify
29-
jaipilot-jwt-token: ${{ secrets.JAIPILOT_JWT_TOKEN }}
31+
jaipilot-license-key: ${{ secrets.JAIPILOT_LICENSE_KEY }}
3032
```
3133
34+
The action automatically:
35+
36+
- finds all `.java` files excluding test classes (`*Test.java`, `*Tests.java`, `*IT.java`, `*ITCase.java`) and files under `src/test`
37+
- runs `jaipilot generate` for each class
38+
- commits generated test changes
39+
- pushes the commit back to the same branch that triggered the workflow
40+
3241
## Publish a new action release
3342

3443
1. Open Actions tab.

0 commit comments

Comments
 (0)