@@ -116,13 +116,19 @@ jobs:
116116 let isNewPR = false;
117117
118118 if (existingPRs.data.length > 0) {
119- // Update existing PR
120- ciPR = existingPRs.data[0];
121- await github.rest.pulls.update({
122- owner: context.repo.owner,
123- repo: context.repo.repo,
124- pull_number: ciPR.number,
125- body: `🤖 **Automated CI Test PR**
119+ // Filter to find the CI test PR (should be labeled with 'ci-test')
120+ const ciTestPR = existingPRs.data.find(pr =>
121+ pr.labels.some(label => label.name === 'ci-test')
122+ );
123+
124+ if (ciTestPR) {
125+ // Update existing PR
126+ ciPR = ciTestPR;
127+ await github.rest.pulls.update({
128+ owner: context.repo.owner,
129+ repo: context.repo.repo,
130+ pull_number: ciPR.number,
131+ body: `🤖 **Automated CI Test PR**
126132
127133This is an automated PR created to run CI tests for PR # ${prDetails.number} by @${prDetails.user}.
128134
@@ -135,10 +141,16 @@ This is an automated PR created to run CI tests for PR #${prDetails.number} by @
135141
136142---
137143_This PR was last updated in response to the \`/run-ci\` command in # ${prDetails.number}_`
138- });
139- } else {
144+ });
145+ } else {
146+ // Existing PR found but it's not labeled as a CI test PR
147+ // Treat it as a new PR scenario
148+ isNewPR = true;
149+ }
150+ }
151+
152+ if (isNewPR || existingPRs.data.length === 0) {
140153 // Create a new draft PR
141- isNewPR = true;
142154 ciPR = await github.rest.pulls.create({
143155 owner : context.repo.owner,
144156 repo : context.repo.repo,
@@ -160,13 +172,20 @@ This is an automated PR created to run CI tests for PR #${prDetails.number} by @
160172_This PR was created in response to the \` /run-ci\` command in #${prDetails.number}_` ,
161173 draft : true
162174 });
163-
164- // Add labels to the new PR
175+ isNewPR = true;
176+ }
177+
178+ // Ensure labels are present on the CI PR (handles both new and existing PRs)
179+ const currentLabels = ciPR.labels?.map(l => l.name) || [];
180+ const requiredLabels = ['ci-test', 'automated'];
181+ const missingLabels = requiredLabels.filter(label => !currentLabels.includes(label));
182+
183+ if (missingLabels.length > 0) {
165184 await github.rest.issues.addLabels({
166185 owner : context.repo.owner,
167186 repo : context.repo.repo,
168187 issue_number : ciPR.number,
169- labels : ['ci-test', 'automated']
188+ labels : missingLabels
170189 });
171190 }
172191
0 commit comments