Sustainable Kibana Architecture: Relocate script v2 (#203887)

## Summary

Commits titles are self-explanatory
This commit is contained in:
Gerard Soldevila 2024-12-12 11:10:50 +01:00 committed by GitHub
parent 0203bba44f
commit 748193d0a7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 6 deletions

View file

@ -18,6 +18,8 @@ gh repo set-default elastic/kibana
You must have `elastic/kibana` remote configured under the name `upstream`.
You must have a remote named `origin` pointing to your fork of the Kibana repo.
## Usage
First of all, you need to decide whether you want to contribute to an existing PR or to create a new one. Use the `--pr` flag to specify the PR you are trying to update:

View file

@ -164,7 +164,7 @@ export const findAndRelocateModules = async (params: RelocateModulesParams) => {
await safeExec(`git restore --staged .`);
await safeExec(`git restore .`);
await safeExec(`git clean -f -d`);
await safeExec(`git checkout ${baseBranch} && git pull upstream ${baseBranch} && git push`);
await safeExec(`git checkout ${baseBranch} && git pull upstream ${baseBranch}`);
if (prNumber) {
// checkout existing PR, reset all commits, rebase from baseBranch

View file

@ -21,4 +21,5 @@ export interface Commit {
export interface PullRequest {
number: string;
commits: Commit[];
headRefName: string;
}

View file

@ -12,10 +12,8 @@ import type { Commit, PullRequest } from './types';
import { safeExec } from './utils.exec';
export const findPr = async (number: string): Promise<PullRequest> => {
const commits = JSON.parse(
(await safeExec(`gh pr view ${number} --json commits`)).stdout
).commits;
return { number, commits };
const res = await safeExec(`gh pr view ${number} --json commits,headRefName`);
return { ...JSON.parse(res.stdout), number };
};
export function hasManualCommits(commits: Commit[]) {
@ -55,6 +53,20 @@ export async function localBranchExists(branchName: string): Promise<boolean> {
return branches.includes(branchName);
}
async function deleteBranches(...branchNames: string[]) {
const res = await safeExec('git branch -l');
const branches = res.stdout
.split('\n')
.filter(Boolean)
.map((branchName) => branchName.trim());
await Promise.all(
branchNames
.filter((toDelete) => branches.includes(toDelete))
.map((toDelete) => safeExec(`git branch -D ${toDelete}`).catch(() => {}))
);
}
export const checkoutResetPr = async (baseBranch: string, prNumber: string): Promise<boolean> => {
const pr = await findPr(prNumber);
@ -69,11 +81,14 @@ export const checkoutResetPr = async (baseBranch: string, prNumber: string): Pro
}
}
// previous cleanup TODO REMOVE
// previous cleanup on current branch
await safeExec(`git restore --staged .`);
await safeExec(`git restore .`);
await safeExec(`git clean -f -d`);
// delete existing branch
await deleteBranches(pr.headRefName);
// checkout the PR branch
await safeExec(`gh pr checkout ${prNumber}`);
await resetAllCommits(pr.commits.length);