Skip to content

Commit

Permalink
feat: retain last run instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Oct 4, 2024
1 parent 856763a commit a0b3691
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
15 changes: 3 additions & 12 deletions src/sync-configs/apply-changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,22 @@ export async function applyChanges({

const defaultBranch = forceBranch || (await getDefaultBranch(repo.url));

// Checkout and pull the default branch
await git.checkout(defaultBranch);
await git.pull("origin", defaultBranch);

// Create and checkout a new branch
const branchName = `sync-configs-${Date.now()}`;
await git.checkoutLocalBranch(branchName);

// Now apply the changes
fs.writeFileSync(filePath, modifiedContent, "utf8");

await git.add(repo.filePath);

await git.commit(
`chore: update using UbiquityOS Configurations Agent
${instruction}
`
);
await git.commit(["chore: update using UbiquityOS Configurations Agent", instruction].join("\n\n"));

try {
if (isInteractive) {
await git.push("origin", defaultBranch);
console.log(`Changes pushed to ${repo.url} in branch ${defaultBranch}`);
} else {
const branchName = `sync-configs-${Date.now()}`;
await git.checkoutLocalBranch(branchName);
await git.push("origin", branchName, ["--set-upstream"]);
await createPullRequest({ repo, branchName, defaultBranch, instruction });
console.log(`Pull request created for ${repo.url} from branch ${branchName} to ${defaultBranch}`);
Expand Down
5 changes: 5 additions & 0 deletions src/sync-configs/process-repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { repositories } from "./repositories";
import { REPOS_DIR } from "./sync-configs";
import * as fs from "fs";

export const LAST_RUN_INSTRUCTION = "last-run-instruction.txt";

export async function processRepositories(instruction: string, isInteractive: boolean) {
const instructionFilePath = path.join(__dirname, REPOS_DIR, LAST_RUN_INSTRUCTION);
fs.writeFileSync(instructionFilePath, instruction, "utf8");

const parserRepoIndex = repositories.findIndex((repo) => repo.type === "parser");
if (parserRepoIndex === -1) {
console.error("Parser repository not found. Unable to proceed.");
Expand Down
3 changes: 2 additions & 1 deletion src/sync-configs/push-modified-contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from "fs";
import path from "path";
import { applyChanges } from "./apply-changes";
import { getDefaultBranch } from "./get-default-branch";
import { LAST_RUN_INSTRUCTION } from "./process-repositories";
import { repositories } from "./repositories";
import { REPOS_DIR } from "./sync-configs";

Expand All @@ -20,7 +21,7 @@ export async function pushModifiedContents() {
repo,
filePath,
modifiedContent,
instruction: "Rerunning using `--push` flag. Original prompt has not been retained.",
instruction: fs.readFileSync(path.join(__dirname, REPOS_DIR, [`Rerunning using \`--push\` flag.`, LAST_RUN_INSTRUCTION].join("\n\n")), "utf8"),
isInteractive: false,
forceBranch: defaultBranch,
});
Expand Down

0 comments on commit a0b3691

Please sign in to comment.