mirror of
https://github.com/kiegroup/git-backporting.git
synced 2025-06-28 05:33:47 +00:00
refactor: move backport data generation to configs parser
This commit is contained in:
parent
e13d1fbf00
commit
10a46551ee
15 changed files with 238 additions and 310 deletions
|
@ -1,6 +1,6 @@
|
|||
|
||||
|
||||
import { GitPullRequest } from "@bp/service/git/git.types";
|
||||
import { BackportPullRequest, GitPullRequest } from "@bp/service/git/git.types";
|
||||
|
||||
export interface LocalGit {
|
||||
user: string, // local git user
|
||||
|
@ -19,6 +19,6 @@ export interface Configs {
|
|||
mergeStrategy?: string, // cherry-pick merge strategy
|
||||
mergeStrategyOption?: string, // cherry-pick merge strategy option
|
||||
originalPullRequest: GitPullRequest,
|
||||
backportPullRequest: GitPullRequest,
|
||||
backportPullRequest: BackportPullRequest,
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ import ConfigsParser from "@bp/service/configs/configs-parser";
|
|||
import { Configs } from "@bp/service/configs/configs.types";
|
||||
import GitClient from "@bp/service/git/git-client";
|
||||
import GitClientFactory from "@bp/service/git/git-client-factory";
|
||||
import { GitPullRequest } from "@bp/service/git/git.types";
|
||||
import { BackportPullRequest, GitPullRequest } from "@bp/service/git/git.types";
|
||||
|
||||
export default class PullRequestConfigsParser extends ConfigsParser {
|
||||
|
||||
|
@ -52,7 +52,7 @@ export default class PullRequestConfigsParser extends ConfigsParser {
|
|||
* @param targetBranch target branch where the backport should be applied
|
||||
* @returns {GitPullRequest}
|
||||
*/
|
||||
private getDefaultBackportPullRequest(originalPullRequest: GitPullRequest, args: Args): GitPullRequest {
|
||||
private getDefaultBackportPullRequest(originalPullRequest: GitPullRequest, args: Args): BackportPullRequest {
|
||||
const reviewers = args.reviewers ?? [];
|
||||
if (reviewers.length == 0 && args.inheritReviewers) {
|
||||
// inherit only if args.reviewers is empty and args.inheritReviewers set to true
|
||||
|
@ -70,16 +70,29 @@ export default class PullRequestConfigsParser extends ConfigsParser {
|
|||
labels.push(...originalPullRequest.labels);
|
||||
}
|
||||
|
||||
let backportBranch = args.bpBranchName;
|
||||
if (backportBranch === undefined || backportBranch.trim() === "") {
|
||||
// for each commit takes the first 7 chars that are enough to uniquely identify them in most of the projects
|
||||
const concatenatedCommits: string = originalPullRequest.commits!.map(c => c.slice(0, 7)).join("-");
|
||||
backportBranch = `bp-${args.targetBranch}-${concatenatedCommits}`;
|
||||
}
|
||||
|
||||
if (backportBranch.length > 250) {
|
||||
this.logger.warn(`Backport branch (length=${backportBranch.length}) exceeded the max length of 250 chars, branch name truncated!`);
|
||||
backportBranch = backportBranch.slice(0, 250);
|
||||
}
|
||||
|
||||
return {
|
||||
author: args.gitUser ?? this.gitClient.getDefaultGitUser(),
|
||||
owner: originalPullRequest.targetRepo.owner,
|
||||
repo: originalPullRequest.targetRepo.project,
|
||||
head: backportBranch,
|
||||
base: args.targetBranch,
|
||||
title: args.title ?? `[${args.targetBranch}] ${originalPullRequest.title}`,
|
||||
body: `${bodyPrefix}${body}`,
|
||||
reviewers: [...new Set(reviewers)],
|
||||
assignees: [...new Set(args.assignees)],
|
||||
labels: [...new Set(labels)],
|
||||
targetRepo: originalPullRequest.targetRepo,
|
||||
sourceRepo: originalPullRequest.targetRepo,
|
||||
branchName: args.bpBranchName,
|
||||
comments: [], // TODO fix comments
|
||||
};
|
||||
}
|
||||
}
|
|
@ -38,4 +38,5 @@ import { BackportPullRequest, GitPullRequest } from "@bp/service/git/git.types";
|
|||
* @returns {Promise<string>} the pull request url
|
||||
*/
|
||||
createPullRequest(backport: BackportPullRequest): Promise<string>;
|
||||
|
||||
}
|
|
@ -13,8 +13,8 @@ export interface GitPullRequest {
|
|||
labels: string[],
|
||||
targetRepo: GitRepository,
|
||||
sourceRepo: GitRepository,
|
||||
nCommits?: number, // number of commits in the pr
|
||||
commits?: string[], // merge commit or last one
|
||||
nCommits: number, // number of commits in the pr
|
||||
commits: string[], // merge commit or last one
|
||||
branchName?: string,
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,8 @@ export interface BackportPullRequest {
|
|||
reviewers: string[], // pr list of reviewers
|
||||
assignees: string[], // pr list of assignees
|
||||
labels: string[], // pr list of assigned labels
|
||||
branchName?: string,
|
||||
comments: string[], // pr list of additional comments
|
||||
// branchName?: string,
|
||||
}
|
||||
|
||||
export enum GitClientType {
|
||||
|
|
|
@ -141,7 +141,6 @@ export default class GitLabClient implements GitClient {
|
|||
return mr.web_url;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve a gitlab user given its username
|
||||
* @param username
|
||||
|
|
|
@ -63,7 +63,7 @@ export default class Runner {
|
|||
this.logger.debug("Parsing configs..");
|
||||
const configs: Configs = await new PullRequestConfigsParser().parseAndValidate(args);
|
||||
const originalPR: GitPullRequest = configs.originalPullRequest;
|
||||
const backportPR: GitPullRequest = configs.backportPullRequest;
|
||||
const backportPR: BackportPullRequest = configs.backportPullRequest;
|
||||
|
||||
// start local git operations
|
||||
const git: GitCLIService = new GitCLIService(configs.auth, configs.git);
|
||||
|
@ -74,19 +74,8 @@ export default class Runner {
|
|||
|
||||
// 5. create new branch from target one and checkout
|
||||
this.logger.debug("Creating local branch..");
|
||||
let backportBranch = backportPR.branchName;
|
||||
if (backportBranch === undefined || backportBranch.trim() === "") {
|
||||
// for each commit takes the first 7 chars that are enough to uniquely identify them in most of the projects
|
||||
const concatenatedCommits: string = originalPR.commits!.map(c => c.slice(0, 7)).join("-");
|
||||
backportBranch = `bp-${configs.targetBranch}-${concatenatedCommits}`;
|
||||
}
|
||||
|
||||
if (backportBranch.length > 250) {
|
||||
this.logger.warn(`Backport branch (length=${backportBranch.length}) exceeded the max length of 250 chars, branch name truncated!`);
|
||||
backportBranch = backportBranch.slice(0, 250);
|
||||
}
|
||||
|
||||
await git.createLocalBranch(configs.folder, backportBranch);
|
||||
await git.createLocalBranch(configs.folder, backportPR.head);
|
||||
|
||||
// 6. fetch pull request remote if source owner != target owner or pull request still open
|
||||
if (configs.originalPullRequest.sourceRepo.owner !== configs.originalPullRequest.targetRepo.owner ||
|
||||
|
@ -102,29 +91,17 @@ export default class Runner {
|
|||
await git.cherryPick(configs.folder, sha, configs.mergeStrategy, configs.mergeStrategyOption);
|
||||
}
|
||||
|
||||
const backport: BackportPullRequest = {
|
||||
owner: originalPR.targetRepo.owner,
|
||||
repo: originalPR.targetRepo.project,
|
||||
head: backportBranch,
|
||||
base: configs.targetBranch,
|
||||
title: backportPR.title,
|
||||
body: backportPR.body,
|
||||
reviewers: backportPR.reviewers,
|
||||
assignees: backportPR.assignees,
|
||||
labels: backportPR.labels,
|
||||
};
|
||||
|
||||
if (!configs.dryRun) {
|
||||
// 8. push the new branch to origin
|
||||
await git.push(configs.folder, backportBranch);
|
||||
await git.push(configs.folder, backportPR.head);
|
||||
|
||||
// 9. create pull request new branch -> target branch (using octokit)
|
||||
const prUrl = await gitApi.createPullRequest(backport);
|
||||
const prUrl = await gitApi.createPullRequest(backportPR);
|
||||
this.logger.info(`Pull request created: ${prUrl}`);
|
||||
|
||||
} else {
|
||||
this.logger.warn("Pull request creation and remote push skipped");
|
||||
this.logger.info(`${JSON.stringify(backport, null, 2)}`);
|
||||
this.logger.info(`${JSON.stringify(backportPR, null, 2)}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue