refactor: changed method name and removed useless data

This commit is contained in:
Andrea Lamparelli 2023-06-22 17:57:49 +02:00
parent 99fbcc39cc
commit a30b6d6290
8 changed files with 19 additions and 34 deletions

10
dist/cli/index.js vendored
View file

@ -181,8 +181,8 @@ class PullRequestConfigsParser extends configs_parser_1.default {
targetRepo: originalPullRequest.targetRepo, targetRepo: originalPullRequest.targetRepo,
sourceRepo: originalPullRequest.targetRepo, sourceRepo: originalPullRequest.targetRepo,
branchName: args.bpBranchName, branchName: args.bpBranchName,
nCommits: 0, // nCommits: 0, // not needed, but required by the
commits: [] // TODO needed? // commits: [] // not needed
}; };
} }
} }
@ -342,8 +342,7 @@ class GitServiceFactory {
* @param type git management service type * @param type git management service type
* @param auth authentication, like github token * @param auth authentication, like github token
*/ */
// TODO rename to getOrCreate static getOrCreate(type, auth) {
static init(type, auth) {
if (GitServiceFactory.instance) { if (GitServiceFactory.instance) {
GitServiceFactory.logger.warn("Git service already initialized!"); GitServiceFactory.logger.warn("Git service already initialized!");
return; return;
@ -664,7 +663,6 @@ class Runner {
* Entry point invoked by the command line or gha * Entry point invoked by the command line or gha
*/ */
async run() { async run() {
this.logger.info("Starting process.");
try { try {
await this.execute(); await this.execute();
this.logger.info("Process succeeded!"); this.logger.info("Process succeeded!");
@ -686,7 +684,7 @@ class Runner {
this.logger.warn("Dry run enabled!"); this.logger.warn("Dry run enabled!");
} }
// 2. init git service // 2. init git service
git_service_factory_1.default.init(this.inferRemoteGitService(args.pullRequest), args.auth); git_service_factory_1.default.getOrCreate(this.inferRemoteGitService(args.pullRequest), args.auth);
const gitApi = git_service_factory_1.default.getService(); const gitApi = git_service_factory_1.default.getService();
// 3. parse configs // 3. parse configs
const configs = await new pr_configs_parser_1.default().parseAndValidate(args); const configs = await new pr_configs_parser_1.default().parseAndValidate(args);

10
dist/gha/index.js vendored
View file

@ -175,8 +175,8 @@ class PullRequestConfigsParser extends configs_parser_1.default {
targetRepo: originalPullRequest.targetRepo, targetRepo: originalPullRequest.targetRepo,
sourceRepo: originalPullRequest.targetRepo, sourceRepo: originalPullRequest.targetRepo,
branchName: args.bpBranchName, branchName: args.bpBranchName,
nCommits: 0, // nCommits: 0, // not needed, but required by the
commits: [] // TODO needed? // commits: [] // not needed
}; };
} }
} }
@ -336,8 +336,7 @@ class GitServiceFactory {
* @param type git management service type * @param type git management service type
* @param auth authentication, like github token * @param auth authentication, like github token
*/ */
// TODO rename to getOrCreate static getOrCreate(type, auth) {
static init(type, auth) {
if (GitServiceFactory.instance) { if (GitServiceFactory.instance) {
GitServiceFactory.logger.warn("Git service already initialized!"); GitServiceFactory.logger.warn("Git service already initialized!");
return; return;
@ -658,7 +657,6 @@ class Runner {
* Entry point invoked by the command line or gha * Entry point invoked by the command line or gha
*/ */
async run() { async run() {
this.logger.info("Starting process.");
try { try {
await this.execute(); await this.execute();
this.logger.info("Process succeeded!"); this.logger.info("Process succeeded!");
@ -680,7 +678,7 @@ class Runner {
this.logger.warn("Dry run enabled!"); this.logger.warn("Dry run enabled!");
} }
// 2. init git service // 2. init git service
git_service_factory_1.default.init(this.inferRemoteGitService(args.pullRequest), args.auth); git_service_factory_1.default.getOrCreate(this.inferRemoteGitService(args.pullRequest), args.auth);
const gitApi = git_service_factory_1.default.getService(); const gitApi = git_service_factory_1.default.getService();
// 3. parse configs // 3. parse configs
const configs = await new pr_configs_parser_1.default().parseAndValidate(args); const configs = await new pr_configs_parser_1.default().parseAndValidate(args);

View file

@ -65,8 +65,8 @@ export default class PullRequestConfigsParser extends ConfigsParser {
targetRepo: originalPullRequest.targetRepo, targetRepo: originalPullRequest.targetRepo,
sourceRepo: originalPullRequest.targetRepo, sourceRepo: originalPullRequest.targetRepo,
branchName: args.bpBranchName, branchName: args.bpBranchName,
nCommits: 0, // TODO: needed? // nCommits: 0, // not needed, but required by the
commits: [] // TODO needed? // commits: [] // not needed
}; };
} }
} }

View file

@ -25,8 +25,7 @@ export default class GitServiceFactory {
* @param type git management service type * @param type git management service type
* @param auth authentication, like github token * @param auth authentication, like github token
*/ */
// TODO rename to getOrCreate public static getOrCreate(type: GitServiceType, auth: string): void {
public static init(type: GitServiceType, auth: string): void {
if (GitServiceFactory.instance) { if (GitServiceFactory.instance) {
GitServiceFactory.logger.warn("Git service already initialized!"); GitServiceFactory.logger.warn("Git service already initialized!");

View file

@ -12,8 +12,8 @@ export interface GitPullRequest {
assignees: string[], assignees: string[],
targetRepo: GitRepository, targetRepo: GitRepository,
sourceRepo: GitRepository, sourceRepo: GitRepository,
nCommits: number, // number of commits in the pr nCommits?: number, // number of commits in the pr
commits: string[], // merge commit or last one commits?: string[], // merge commit or last one
branchName?: string, branchName?: string,
} }

View file

@ -42,8 +42,6 @@ export default class Runner {
* Entry point invoked by the command line or gha * Entry point invoked by the command line or gha
*/ */
async run(): Promise<void> { async run(): Promise<void> {
this.logger.info("Starting process.");
try { try {
await this.execute(); await this.execute();
@ -71,7 +69,7 @@ export default class Runner {
} }
// 2. init git service // 2. init git service
GitServiceFactory.init(this.inferRemoteGitService(args.pullRequest), args.auth); GitServiceFactory.getOrCreate(this.inferRemoteGitService(args.pullRequest), args.auth);
const gitApi: GitService = GitServiceFactory.getService(); const gitApi: GitService = GitServiceFactory.getService();
// 3. parse configs // 3. parse configs
@ -86,7 +84,7 @@ export default class Runner {
await git.clone(configs.originalPullRequest.targetRepo.cloneUrl, configs.folder, configs.targetBranch); await git.clone(configs.originalPullRequest.targetRepo.cloneUrl, configs.folder, configs.targetBranch);
// 5. create new branch from target one and checkout // 5. create new branch from target one and checkout
const backportBranch = backportPR.branchName ?? `bp-${configs.targetBranch}-${originalPR.commits.join("-")}`; const backportBranch = backportPR.branchName ?? `bp-${configs.targetBranch}-${originalPR.commits!.join("-")}`;
await git.createLocalBranch(configs.folder, backportBranch); await git.createLocalBranch(configs.folder, backportBranch);
// 6. fetch pull request remote if source owner != target owner or pull request still open // 6. fetch pull request remote if source owner != target owner or pull request still open
@ -96,7 +94,7 @@ export default class Runner {
} }
// 7. apply all changes to the new branch // 7. apply all changes to the new branch
for (const sha of originalPR.commits) { for (const sha of originalPR.commits!) {
await git.cherryPick(configs.folder, sha); await git.cherryPick(configs.folder, sha);
} }

View file

@ -15,7 +15,7 @@ describe("pull request config parser", () => {
let parser: PullRequestConfigsParser; let parser: PullRequestConfigsParser;
beforeAll(() => { beforeAll(() => {
GitServiceFactory.init(GitServiceType.GITHUB, "whatever"); GitServiceFactory.getOrCreate(GitServiceType.GITHUB, "whatever");
}); });
beforeEach(() => { beforeEach(() => {
@ -95,8 +95,6 @@ describe("pull request config parser", () => {
cloneUrl: "https://github.com/owner/reponame.git" cloneUrl: "https://github.com/owner/reponame.git"
}, },
bpBranchName: undefined, bpBranchName: undefined,
nCommits: 0,
commits: []
}); });
}); });
@ -289,8 +287,6 @@ describe("pull request config parser", () => {
cloneUrl: "https://github.com/owner/reponame.git" cloneUrl: "https://github.com/owner/reponame.git"
}, },
bpBranchName: undefined, bpBranchName: undefined,
nCommits: 0,
commits: []
}); });
}); });
@ -365,8 +361,6 @@ describe("pull request config parser", () => {
cloneUrl: "https://github.com/owner/reponame.git" cloneUrl: "https://github.com/owner/reponame.git"
}, },
bpBranchName: undefined, bpBranchName: undefined,
nCommits: 0,
commits: []
}); });
}); });
@ -441,8 +435,6 @@ describe("pull request config parser", () => {
cloneUrl: "https://github.com/owner/reponame.git" cloneUrl: "https://github.com/owner/reponame.git"
}, },
bpBranchName: undefined, bpBranchName: undefined,
nCommits: 0,
commits: []
}); });
}); });
}); });

View file

@ -10,7 +10,7 @@ describe("github service", () => {
beforeAll(() => { beforeAll(() => {
// init git service // init git service
GitServiceFactory.init(GitServiceType.GITHUB, "whatever"); GitServiceFactory.getOrCreate(GitServiceType.GITHUB, "whatever");
}); });
beforeEach(() => { beforeEach(() => {
@ -33,7 +33,7 @@ describe("github service", () => {
cloneUrl: "https://github.com/owner/reponame.git" cloneUrl: "https://github.com/owner/reponame.git"
}); });
expect(res.title).toBe("PR Title"); expect(res.title).toBe("PR Title");
expect(res.commits.length).toBe(1); expect(res.commits!.length).toBe(1);
expect(res.commits).toEqual(["28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc"]); expect(res.commits).toEqual(["28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc"]);
}); });