Fixing the script a bit
This commit is contained in:
67
app.ts
67
app.ts
@@ -1,13 +1,13 @@
|
||||
import * as core from '@actions/core';
|
||||
import * as core from "@actions/core";
|
||||
import { spawn } from "child_process";
|
||||
import fs from "fs/promises";
|
||||
import path from 'path';
|
||||
import path from "path";
|
||||
import {ChildProcess, SpawnOptions} from "node:child_process";
|
||||
|
||||
const myToken = core.getInput('branches');
|
||||
const myToken = core.getInput("branches");
|
||||
|
||||
// Ignored branches
|
||||
const IGNORED_BRANCHES = ['master', 'main', 'dev', 'release'];
|
||||
const IGNORED_BRANCHES = ["master", "main", "dev", "release"];
|
||||
|
||||
type BranchDependencies = Record<string, null | string>;
|
||||
|
||||
@@ -16,15 +16,15 @@ type BranchDependencies = Record<string, null | string>;
|
||||
*/
|
||||
const runGitCommand = (args: string[], options?: SpawnOptions): Promise<string> =>
|
||||
new Promise((resolve, reject) => {
|
||||
const git: ChildProcess = spawn('git', args, { stdio: "inherit", ...options });
|
||||
const git: ChildProcess = spawn("git", args, { stdio: "pipe", ...options });
|
||||
let stdout: string = "";
|
||||
let stderr: string = "";
|
||||
|
||||
git.stdout.on('data', (data) => stdout += data.toString());
|
||||
git.stderr.on('data', (data) => stderr += data.toString());
|
||||
git.stdout.on("data", (data) => stdout += data.toString());
|
||||
git.stderr.on("data", (data) => stderr += data.toString());
|
||||
|
||||
// Handle completion
|
||||
git.on('close', (code) => (code === 0) ?
|
||||
git.on("close", (code) => (code === 0) ?
|
||||
resolve(stdout) :
|
||||
reject(new Error(`Git command failed with code ${code}: ${stderr}`)));
|
||||
});
|
||||
@@ -33,20 +33,22 @@ const runGitCommand = (args: string[], options?: SpawnOptions): Promise<string>
|
||||
* Fetch all remote branches.
|
||||
*/
|
||||
const fetchBranches = async (): Promise<string[]> => {
|
||||
console.log('Fetching all branches...');
|
||||
await runGitCommand(['fetch', '--all']);
|
||||
const branches = await runGitCommand(['branch', '-r']);
|
||||
console.log("Fetching all branches...");
|
||||
await runGitCommand(["fetch", "--all"]);
|
||||
const branches: string = await runGitCommand(["branch", "-r"]);
|
||||
|
||||
console.log(branches)
|
||||
return branches
|
||||
.split('\n')
|
||||
.map((branch) => branch.trim().replace('origin/', ''))
|
||||
.filter((branch) => !IGNORED_BRANCHES.includes(branch) && branch !== '');
|
||||
.split("\n")
|
||||
.map((branch) => branch.trim().replace("origin/", ""))
|
||||
.filter((branch) => !IGNORED_BRANCHES.includes(branch) && branch !== "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect dependencies between branches.
|
||||
*/
|
||||
const detectDependencies = async (branches: string[]): Promise<BranchDependencies> => {
|
||||
console.log('Detecting dependencies...');
|
||||
console.log("Detecting dependencies...");
|
||||
const dependencies: BranchDependencies = {};
|
||||
|
||||
for (const branch of branches) {
|
||||
@@ -54,8 +56,8 @@ const detectDependencies = async (branches: string[]): Promise<BranchDependencie
|
||||
|
||||
for (const otherBranch of branches) {
|
||||
if (branch !== otherBranch) {
|
||||
const base = await runGitCommand(['merge-base', `origin/${branch}`, `origin/${otherBranch}`]);
|
||||
const isAncestor = await runGitCommand(['merge-base', '--is-ancestor', base.trim(), `origin/${branch}`]).catch(() => false);
|
||||
const base = await runGitCommand(["merge-base", `origin/${branch}`, `origin/${otherBranch}`]);
|
||||
const isAncestor = await runGitCommand(["merge-base", "--is-ancestor", base.trim(), `origin/${branch}`]).catch(() => false);
|
||||
if (isAncestor) {
|
||||
dependencies[branch] = otherBranch;
|
||||
break;
|
||||
@@ -70,7 +72,7 @@ const detectDependencies = async (branches: string[]): Promise<BranchDependencie
|
||||
* Perform a topological sort to determine the rebase order.
|
||||
*/
|
||||
const determineRebaseOrder = (dependencies: BranchDependencies) => {
|
||||
console.log('Determining rebase order...');
|
||||
console.log("Determining rebase order...");
|
||||
const visited = new Set();
|
||||
const order = [];
|
||||
|
||||
@@ -88,17 +90,17 @@ const determineRebaseOrder = (dependencies: BranchDependencies) => {
|
||||
/**
|
||||
* Rebase a branch onto its base branch.
|
||||
*/
|
||||
const rebaseBranch = async (branch: string, baseBranch = 'master'): Promise<void> => {
|
||||
const rebaseBranch = async (branch: string, baseBranch = "master"): Promise<void> => {
|
||||
console.log(`Rebasing ${branch} onto ${baseBranch}...`);
|
||||
try {
|
||||
await runGitCommand(['checkout', branch]);
|
||||
await runGitCommand(['fetch', 'origin', baseBranch]);
|
||||
await runGitCommand(['rebase', `origin/${baseBranch}`]);
|
||||
await runGitCommand(["checkout", branch]);
|
||||
await runGitCommand(["fetch", "origin", baseBranch]);
|
||||
await runGitCommand(["rebase", `origin/${baseBranch}`]);
|
||||
console.log(`Rebase successful for ${branch}. Pushing...`);
|
||||
await runGitCommand(['push', '--force-with-lease']);
|
||||
await runGitCommand(["push", "--force-with-lease"]);
|
||||
} catch (error) {
|
||||
console.error(`Rebase failed for ${branch}: ${error.message}`);
|
||||
await runGitCommand(['rebase', '--abort']);
|
||||
await runGitCommand(["rebase", "--abort"]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,26 +110,27 @@ const rebaseBranch = async (branch: string, baseBranch = 'master'): Promise<void
|
||||
const main = async (): Promise<void> => {
|
||||
try {
|
||||
// Step 1: Fetch branches
|
||||
const branches = await fetchBranches();
|
||||
await fs.writeFile(BRANCHES_FILE, branches.join('\n'), 'utf-8');
|
||||
console.log('Branches:', branches);
|
||||
const branches: string[] = await fetchBranches();
|
||||
console.log(branches)
|
||||
await fs.writeFile(BRANCHES_FILE, branches.join("\n"), "utf-8");
|
||||
console.log("Branches:", branches);
|
||||
|
||||
// Step 2: Detect dependencies
|
||||
const dependencies = await detectDependencies(branches);
|
||||
await fs.writeFile(DEPENDENCIES_FILE, JSON.stringify(dependencies, null, 2), 'utf-8');
|
||||
console.log('Dependencies:', dependencies);
|
||||
await fs.writeFile(DEPENDENCIES_FILE, JSON.stringify(dependencies, null, 2), "utf-8");
|
||||
console.log("Dependencies:", dependencies);
|
||||
|
||||
// Step 3: Determine rebase order
|
||||
const order = determineRebaseOrder(dependencies);
|
||||
console.log('Rebase order:', order);
|
||||
console.log("Rebase order:", order);
|
||||
|
||||
// Step 4: Rebase branches
|
||||
for (const branch of order) {
|
||||
const baseBranch = dependencies[branch] || 'master';
|
||||
const baseBranch = dependencies[branch] || "master";
|
||||
await rebaseBranch(branch, baseBranch);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error during workflow execution:', error.message);
|
||||
console.error("Error during workflow execution:", error.message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user