Skip to content

Commit a7f287d

Browse files
fix: return to original branch after ci command completes (#728)
* fix: return to original branch after ci command completes Co-Authored-By: Max Prilutskiy <maks.prilutskiy@gmail.com> * chore: add changeset for returning to original branch Co-Authored-By: Max Prilutskiy <maks.prilutskiy@gmail.com> * refactor: simplify original branch capture Co-Authored-By: Max Prilutskiy <maks.prilutskiy@gmail.com> * refactor: use platformKit.platformConfig.baseBranchName for original branch Co-Authored-By: Max Prilutskiy <maks.prilutskiy@gmail.com> * refactor: use platformKit.platformConfig.baseBranchName directly without intermediary property Co-Authored-By: Max Prilutskiy <maks.prilutskiy@gmail.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Max Prilutskiy <maks.prilutskiy@gmail.com>
1 parent c18873c commit a7f287d

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

.changeset/thick-buses-remember.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"lingo.dev": patch
3+
---
4+
5+
Return users to their original branch after CI command completes, even when it fails

packages/cli/src/cli/cmd/ci/flows/_base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface IIntegrationFlow {
55
preRun?(): Promise<boolean>;
66
run(): Promise<boolean>;
77
postRun?(): Promise<void>;
8+
returnToOriginalBranch?(): Promise<void>;
89
}
910

1011
export abstract class IntegrationFlow implements IIntegrationFlow {

packages/cli/src/cli/cmd/ci/flows/in-branch.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export class InBranchFlow extends IntegrationFlow {
7979
execSync(`pwd`, { stdio: "inherit" });
8080
execSync(`ls -la`, { stdio: "inherit" });
8181

82+
8283
execSync(`git config --global safe.directory ${process.cwd()}`);
8384

8485
execSync(`git config user.name "${gitConfig.userName}"`);
@@ -116,4 +117,22 @@ export class InBranchFlow extends IntegrationFlow {
116117

117118
return true;
118119
}
120+
async returnToOriginalBranch() {
121+
const originalBranch = this.platformKit.platformConfig.baseBranchName;
122+
if (originalBranch) {
123+
this.ora.start(`Returning to original branch: ${originalBranch}`);
124+
125+
try {
126+
execSync(`git checkout ${originalBranch}`, {
127+
stdio: "inherit",
128+
encoding: "utf8"
129+
});
130+
this.ora.succeed(`Returned to original branch: ${originalBranch}`);
131+
} catch (error) {
132+
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
133+
this.ora.fail(`Failed to return to original branch: ${errorMessage}`);
134+
}
135+
}
136+
}
137+
119138
}

packages/cli/src/cli/cmd/ci/index.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,19 @@ export default new Command()
7474
? new PullRequestFlow(ora, platformKit)
7575
: new InBranchFlow(ora, platformKit);
7676

77-
const canRun = await flow.preRun?.();
78-
if (canRun === false) {
79-
return;
80-
}
77+
try {
78+
const canRun = await flow.preRun?.();
79+
if (canRun === false) {
80+
return;
81+
}
8182

82-
const hasChanges = await flow.run();
83-
if (!hasChanges) {
84-
return;
85-
}
83+
const hasChanges = await flow.run();
84+
if (!hasChanges) {
85+
return;
86+
}
8687

87-
await flow.postRun?.();
88+
await flow.postRun?.();
89+
} finally {
90+
await flow.returnToOriginalBranch?.();
91+
}
8892
});

0 commit comments

Comments
 (0)