Skip to content

Commit 90f931d

Browse files
Copilotpranaygp
andcommitted
Remove timestamp from CI branch name and implement 1:1 PR relationship
Co-authored-by: pranaygp <[email protected]>
1 parent d9a48f2 commit 90f931d

File tree

1 file changed

+59
-24
lines changed

1 file changed

+59
-24
lines changed

.github/workflows/trigger-ci.yml

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,14 @@ jobs:
8282
with:
8383
fetch-depth: 0
8484

85-
- name: Create CI branch and PR
85+
- name: Create or update CI branch and PR
8686
if: steps.check-permissions.outputs.result == 'true'
8787
uses: actions/github-script@v7
8888
with:
8989
github-token: ${{ secrets.GITHUB_TOKEN }}
9090
script: |
9191
const prDetails = ${{ steps.pr-details.outputs.result }};
92-
const timestamp = new Date().getTime();
93-
const ciBranchName = `ci-test/${prDetails.number}-${timestamp}`;
92+
const ciBranchName = `ci-test/${prDetails.number}`;
9493
9594
// Add remote for the external fork if it's from a fork
9695
if (prDetails.head_repo_full_name !== `${context.repo.owner}/${context.repo.repo}`) {
@@ -102,17 +101,51 @@ jobs:
102101
await exec.exec('git', ['checkout', '-b', ciBranchName, `origin/${prDetails.head_ref}`]);
103102
}
104103
105-
// Push the new branch to origin
106-
await exec.exec('git', ['push', 'origin', ciBranchName]);
104+
// Push the branch to origin (force push to update if exists)
105+
await exec.exec('git', ['push', '-f', 'origin', ciBranchName]);
107106
108-
// Create a draft PR
109-
const newPR = await github.rest.pulls.create({
107+
// Check if a CI PR already exists for this original PR
108+
const existingPRs = await github.rest.pulls.list({
110109
owner: context.repo.owner,
111110
repo: context.repo.repo,
112-
title: `[CI Test] ${prDetails.title}`,
113-
head: ciBranchName,
114-
base: prDetails.base_ref,
115-
body: `🤖 **Automated CI Test PR**
111+
state: 'open',
112+
head: `${context.repo.owner}:${ciBranchName}`
113+
});
114+
115+
let ciPR;
116+
let isNewPR = false;
117+
118+
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**
126+
127+
This is an automated PR created to run CI tests for PR #${prDetails.number} by @${prDetails.user}.
128+
129+
**Original PR:** #${prDetails.number}
130+
**Last triggered by:** @${context.actor}
131+
**Source branch:** \`${prDetails.head_ref}\`
132+
**Source SHA:** \`${prDetails.head_sha}\`
133+
134+
⚠️ **This PR will be automatically closed once CI completes.** Do not merge this PR.
135+
136+
---
137+
_This PR was last updated in response to the \`/run-ci\` command in #${prDetails.number}_`
138+
});
139+
} else {
140+
// Create a new draft PR
141+
isNewPR = true;
142+
ciPR = await github.rest.pulls.create({
143+
owner: context.repo.owner,
144+
repo: context.repo.repo,
145+
title: `[CI Test] ${prDetails.title}`,
146+
head: ciBranchName,
147+
base: prDetails.base_ref,
148+
body: `🤖 **Automated CI Test PR**
116149
117150
This is an automated PR created to run CI tests for PR #${prDetails.number} by @${prDetails.user}.
118151
@@ -125,29 +158,31 @@ This is an automated PR created to run CI tests for PR #${prDetails.number} by @
125158
126159
---
127160
_This PR was created in response to the \`/run-ci\` command in #${prDetails.number}_`,
128-
draft: true
129-
});
161+
draft: true
162+
});
163+
164+
// Add labels to the new PR
165+
await github.rest.issues.addLabels({
166+
owner: context.repo.owner,
167+
repo: context.repo.repo,
168+
issue_number: ciPR.number,
169+
labels: ['ci-test', 'automated']
170+
});
171+
}
130172

131173
// Comment on the original PR
174+
const prAction = isNewPR ? 'created' : 'updated';
132175
await github.rest.issues.createComment({
133176
owner: context.repo.owner,
134177
repo: context.repo.repo,
135178
issue_number: context.issue.number,
136-
body: `✅ CI test triggered by @${context.actor}!
179+
body: `✅ CI test ${prAction} by @${context.actor}!
137180
138-
CI is now running in draft PR #${newPR.data.number}. You can monitor the progress there.
181+
CI is now running in draft PR #${ciPR.number}. You can monitor the progress there.
139182
140183
Once the tests complete, you can review the results and the draft PR will be automatically closed.`
141184
});
142185

143-
// Add label to the new PR
144-
await github.rest.issues.addLabels({
145-
owner: context.repo.owner,
146-
repo: context.repo.repo,
147-
issue_number: newPR.data.number,
148-
labels: ['ci-test', 'automated']
149-
});
150-
151-
core.setOutput('ci_pr_number', newPR.data.number);
186+
core.setOutput('ci_pr_number', ciPR.number);
152187
core.setOutput('ci_branch_name', ciBranchName);
153188

0 commit comments

Comments
 (0)