Skip to content

Commit

Permalink
feat: add GitHub App token generation and improve bot credential hand…
Browse files Browse the repository at this point in the history
…ling in sync-configs
  • Loading branch information
0x4007 committed Oct 18, 2024
1 parent 5fc4636 commit 29a7c5f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/start-program.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,18 @@ jobs:
- name: Install dependencies
run: bun install

- name: Generate GitHub App token
id: generate_token
uses: tibdex/github-app-token@v2
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.APP_PRIVATE_KEY }}

- name: Run start script
run: bun run start ${{ github.event.inputs.target_repo }}
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
EDITOR_INSTRUCTION: ${{ github.event.inputs.editor_instruction }}
INTERACTIVE: "false"
GITHUB_ACTOR: ${{ github.actor }}
GITHUB_APP_TOKEN: ${{ steps.generate_token.outputs.token }}
37 changes: 34 additions & 3 deletions src/sync-configs/apply-changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ export async function applyChanges({
stderr.pipe(process.stderr);
});

// Check for global Git config
const globalUserName = await git.getConfig("user.name", "global");
const globalUserEmail = await git.getConfig("user.email", "global");

let isBot = false;
if (!globalUserName.value || !globalUserEmail.value) {
// If global config is not set, use the bot credentials
const userName = "UbiquityOS Configurations Agent[bot]";
const userEmail = "ubiquity-os[bot]@users.noreply.github.com";

await git.addConfig("user.name", userName, false, "local");
await git.addConfig("user.email", userEmail, false, "local");
console.log("Using bot credentials for Git operations.");
isBot = true;
} else {
console.log("Using global Git config for operations.");
}

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

await git.checkout(defaultBranch);
Expand All @@ -42,16 +60,29 @@ export async function applyChanges({

await git.add(target.filePath);

await git.commit(["chore: update using UbiquityOS Configurations Agent", instruction].join("\n\n"));
let commitMessage: string;
if (isBot && process.env.GITHUB_ACTOR) {
commitMessage = ["chore: update", instruction, `Triggered by @${process.env.GITHUB_ACTOR}`].join("\n\n");
} else {
commitMessage = ["chore: update using UbiquityOS Configurations Agent", instruction].join("\n\n");
}

await git.commit(commitMessage);

try {
if (isInteractive) {
await git.push("origin", defaultBranch);
await git.push("origin", defaultBranch, {
"--force-with-lease": null,
...(process.env.GITHUB_APP_TOKEN ? { "-o": `oauth_token=${process.env.GITHUB_APP_TOKEN}` } : {}),
});
console.log(`Changes pushed to ${target.url} in branch ${defaultBranch}`);
} else {
const branchName = `sync-configs-${Date.now()}`;
await git.checkoutLocalBranch(branchName);
await git.push("origin", branchName, ["--set-upstream"]);
await git.push("origin", branchName, {
"-u": null,
...(process.env.GITHUB_APP_TOKEN ? { "-o": `oauth_token=${process.env.GITHUB_APP_TOKEN}` } : {}),
});
await createPullRequest({ target, branchName, defaultBranch, instruction });
console.log(`Pull request created for ${target.url} from branch ${branchName} to ${defaultBranch}`);
}
Expand Down

0 comments on commit 29a7c5f

Please sign in to comment.