@@ -24,6 +24,9 @@ PROJECT_ROOT="$SCRIPT_DIR/.."
2424# Source command utils for logging
2525source " $PROJECT_ROOT /command_utils.sh"
2626
27+ # Workflow file name
28+ WORKFLOW_FILE=" update-pr-stack.yml"
29+
2730# --- Helper Functions ---
2831cleanup () {
2932 echo >&2 " --- Cleaning up ---"
@@ -50,69 +53,109 @@ trap cleanup EXIT
5053
5154wait_for_workflow () {
5255 local pr_number=$1 # PR number that was merged
53- local merged_branch_name=$2 # The head branch name of the merged PR
56+ local merged_branch_name=$2 # The head branch name of the merged PR (unused now, but kept for context)
5457 local merge_commit_sha=$3 # The SHA of the merge commit
5558 local expected_conclusion=${4:- success} # Expected conclusion (success, failure, etc.)
56- local max_attempts=15 # Increased attempts (~5 mins max wait)
59+ local max_attempts=20 # Increased attempts (~7 mins max wait)
5760 local attempt=0
58- local run_id =" " # Reset run_id for each call
61+ local target_run_id =" "
5962
60- echo >&2 " Waiting for workflow triggered by merge of PR #$pr_number ($merged_branch_name ) with merge commit $merge_commit_sha ..."
63+ echo >&2 " Waiting for workflow ' $WORKFLOW_FILE ' triggered by merge of PR #$pr_number (merge commit $merge_commit_sha ) ..."
6164
6265 while [[ $attempt -lt $max_attempts ]]; do
6366 # Calculate sleep time: increases with attempts
6467 sleep_time=$(( (attempt + 1 ) * 2 ))
6568 echo >&2 " Attempt $(( attempt + 1 )) /$max_attempts : Checking for workflow run..."
6669
67- # Look for runs triggered by pull_request closure for the specific merge commit
68- # We filter by head_sha matching the merge commit, as the 'pull_request' event context might be tricky
69- # Note: This might be fragile if other workflows run on pull_request close.
70- # A more robust approach might involve filtering by workflow name AND the triggering commit/event details.
71- run_id=$( log_cmd gh run list \
72- --repo " $REPO_FULL_NAME " \
73- --json databaseId,headSha,event,status,conclusion \
74- --event pull_request \
75- --limit 5 \
76- --jq " .[] | select(.event == \" pull_request\" and .headSha == \" $merge_commit_sha \" ) | .databaseId" | head -n 1
77- )
78-
79- if [[ -z " $run_id " ]]; then
80- echo >&2 " Workflow run for merge commit $merge_commit_sha not found yet. Sleeping $sleep_time seconds."
81- else
82- echo >&2 " Found potential workflow run ID: $run_id "
83- # Check the status of the specific run
84- run_info=$( log_cmd gh run view " $run_id " --repo " $REPO_FULL_NAME " --json status,conclusion)
85- run_status=$( echo " $run_info " | jq -r ' .status' )
86- run_conclusion=$( echo " $run_info " | jq -r ' .conclusion' ) # Might be null if not completed
87-
88- echo >&2 " Workflow run $run_id status: $run_status , conclusion: $run_conclusion "
89-
90- if [[ " $run_status " == " completed" ]]; then
91- if [[ " $run_conclusion " == " $expected_conclusion " ]]; then
92- echo >&2 " Workflow $run_id completed with expected conclusion: $run_conclusion ."
93- return 0
70+ # If we haven't found the target run ID yet, search for it
71+ if [[ -z " $target_run_id " ]]; then
72+ echo >&2 " Searching for the specific workflow run..."
73+ # List recent runs for the specific workflow triggered by pull_request event
74+ candidate_run_ids=$( log_cmd gh run list \
75+ --repo " $REPO_FULL_NAME " \
76+ --workflow " $WORKFLOW_FILE " \
77+ --event pull_request \
78+ --limit 10 \
79+ --json databaseId --jq ' .[].databaseId' || echo " " ) # Get IDs, handle potential errors
80+
81+ if [[ -z " $candidate_run_ids " ]]; then
82+ echo >&2 " No recent '$WORKFLOW_FILE ' runs found for 'pull_request' event. Sleeping $sleep_time seconds."
83+ sleep $sleep_time
84+ attempt=$(( attempt + 1 ))
85+ continue # Go to next attempt
86+ fi
87+
88+ echo >&2 " Found candidate run IDs: $candidate_run_ids . Checking payloads..."
89+ for run_id in $candidate_run_ids ; do
90+ echo >&2 " Checking candidate run ID: $run_id "
91+ run_payload_json=$( log_cmd gh run view " $run_id " --repo " $REPO_FULL_NAME " --json eventPayload || echo " {}" ) # Fetch payload, default to empty JSON on error
92+
93+ # Check if the payload matches our merged PR
94+ payload_action=$( echo " $run_payload_json " | jq -r ' .eventPayload.action // ""' )
95+ payload_merged=$( echo " $run_payload_json " | jq -r ' .eventPayload.pull_request.merged // ""' )
96+ payload_pr_num=$( echo " $run_payload_json " | jq -r ' .eventPayload.pull_request.number // ""' )
97+ payload_merge_sha=$( echo " $run_payload_json " | jq -r ' .eventPayload.pull_request.merge_commit_sha // ""' )
98+
99+ # Debugging output
100+ # echo >&2 " Payload Action: $payload_action"
101+ # echo >&2 " Payload Merged: $payload_merged"
102+ # echo >&2 " Payload PR Num: $payload_pr_num"
103+ # echo >&2 " Payload Merge SHA: $payload_merge_sha"
104+
105+ if [[ " $payload_action " == " closed" && \
106+ " $payload_merged " == " true" && \
107+ " $payload_pr_num " == " $pr_number " && \
108+ " $payload_merge_sha " == " $merge_commit_sha " ]]; then
109+ echo >&2 " Found matching workflow run ID: $run_id "
110+ target_run_id=" $run_id "
111+ break # Found the run, exit the inner loop
94112 else
95- echo >&2 " Workflow $run_id completed with unexpected conclusion: $run_conclusion (expected: $expected_conclusion )"
96- log_cmd gh run view " $run_id " --repo " $REPO_FULL_NAME " --log || echo >&2 " Could not fetch logs for run $run_id "
97- return 1
113+ echo >&2 " Run $run_id does not match the merge event criteria."
98114 fi
99- elif [[ " $run_status " == " queued" || " $run_status " == " in_progress" || " $run_status " == " waiting" ]]; then
100- echo >&2 " Workflow $run_id is $run_status . Sleeping $sleep_time seconds."
115+ done
116+ fi
117+
118+ # If we still haven't found the run ID after checking candidates, wait and retry listing
119+ if [[ -z " $target_run_id " ]]; then
120+ echo >&2 " Target workflow run not found among recent runs. Sleeping $sleep_time seconds."
121+ sleep $sleep_time
122+ attempt=$(( attempt + 1 ))
123+ continue # Go to next attempt
124+ fi
125+
126+ # --- Monitor the identified target run ---
127+ echo >&2 " Monitoring workflow run ID: $target_run_id "
128+ run_info=$( log_cmd gh run view " $target_run_id " --repo " $REPO_FULL_NAME " --json status,conclusion)
129+ run_status=$( echo " $run_info " | jq -r ' .status' )
130+ run_conclusion=$( echo " $run_info " | jq -r ' .conclusion' ) # Might be null if not completed
131+
132+ echo >&2 " Workflow run $target_run_id status: $run_status , conclusion: $run_conclusion "
133+
134+ if [[ " $run_status " == " completed" ]]; then
135+ if [[ " $run_conclusion " == " $expected_conclusion " ]]; then
136+ echo >&2 " Workflow $target_run_id completed with expected conclusion: $run_conclusion ."
137+ return 0
101138 else
102- echo >&2 " Workflow $run_id has unexpected status : $run_status . Conclusion : $run_conclusion "
103- log_cmd gh run view " $run_id " --repo " $REPO_FULL_NAME " --log || echo >&2 " Could not fetch logs for run $run_id "
139+ echo >&2 " Workflow $target_run_id completed with unexpected conclusion : $run_conclusion (expected : $expected_conclusion ) "
140+ log_cmd gh run view " $target_run_id " --repo " $REPO_FULL_NAME " --log || echo >&2 " Could not fetch logs for run $target_run_id "
104141 return 1
105142 fi
143+ elif [[ " $run_status " == " queued" || " $run_status " == " in_progress" || " $run_status " == " waiting" ]]; then
144+ echo >&2 " Workflow $target_run_id is $run_status . Sleeping $sleep_time seconds."
145+ else
146+ echo >&2 " Workflow $target_run_id has unexpected status: $run_status . Conclusion: $run_conclusion "
147+ log_cmd gh run view " $target_run_id " --repo " $REPO_FULL_NAME " --log || echo >&2 " Could not fetch logs for run $target_run_id "
148+ return 1
106149 fi
107150
108151 sleep $sleep_time
109152 attempt=$(( attempt + 1 ))
110153 done
111154
112- echo >&2 " Timeout waiting for workflow run for merge commit $merge_commit_sha to complete with conclusion $expected_conclusion ."
155+ echo >&2 " Timeout waiting for workflow run triggered by merge of PR # $pr_number (merge commit $merge_commit_sha ) to complete with conclusion $expected_conclusion ."
113156 # List recent runs for debugging
114- echo >&2 " Recent runs:"
115- gh run list --repo " $REPO_FULL_NAME " --limit 10 || echo >&2 " Could not list recent runs."
157+ echo >&2 " Recent runs for workflow ' $WORKFLOW_FILE ' :"
158+ gh run list --repo " $REPO_FULL_NAME " --workflow " $WORKFLOW_FILE " -- limit 10 || echo >&2 " Could not list recent runs."
116159 return 1
117160}
118161
@@ -145,7 +188,7 @@ cp "$PROJECT_ROOT/command_utils.sh" .
145188# Create workflow file pointing to the local action
146189echo >&2 " Creating workflow file..."
147190mkdir -p .github/workflows
148- cat > .github/workflows/update-pr-stack.yml << ' EOF '
191+ cat > .github/workflows/" $WORKFLOW_FILE " << EOF
149192name: Update Stacked PRs on Squash Merge (E2E Test)
150193on:
151194 pull_request:
@@ -167,15 +210,15 @@ jobs:
167210 # Fetch all history for all branches and tags
168211 fetch-depth: 0
169212 # Use a PAT token for checkout to allow pushing updates
170- token: ${{ secrets.GITHUB_TOKEN }}
213+ token: \ $ {{ secrets.GITHUB_TOKEN }}
171214 - name: Update PR stack
172215 # Use the action from the current repository checkout
173216 uses: ./
174217 with:
175- github-token: ${{ secrets.GITHUB_TOKEN }}
218+ github-token: \ $ {{ secrets.GITHUB_TOKEN }}
176219EOF
177220
178- log_cmd git add action.yml update-pr-stack.sh command_utils.sh .github/workflows/update-pr-stack.yml
221+ log_cmd git add action.yml update-pr-stack.sh command_utils.sh .github/workflows/" $WORKFLOW_FILE "
179222log_cmd git commit -m " Add action and workflow files"
180223ACTION_COMMIT_SHA=$( git rev-parse HEAD)
181224
0 commit comments