1
0
Fork 0
mirror of https://code.forgejo.org/actions/git-backporting synced 2025-07-01 23:06:00 +02:00

feat(issue-54): backport pr commits without squash (#55)

* feat(issue-54): backport pr commits without squash

fix https://github.com/kiegroup/git-backporting/issues/54

* feat(issue-54): fixed readme
This commit is contained in:
Andrea Lamparelli 2023-07-11 11:22:01 +02:00 committed by GitHub
parent a737aa7c4c
commit c4dbb26c1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 990 additions and 145 deletions

View file

@ -2,7 +2,7 @@ import LoggerService from "@bp/service/logger/logger-service";
import GitClient from "@bp/service/git/git-client";
import { GitPullRequest, BackportPullRequest } from "@bp/service/git/git.types";
import LoggerServiceFactory from "@bp/service/logger/logger-service-factory";
import { MergeRequestSchema, UserSchema } from "@gitbeaker/rest";
import { CommitSchema, MergeRequestSchema, UserSchema } from "@gitbeaker/rest";
import GitLabMapper from "@bp/service/git/gitlab/gitlab-mapper";
import axios, { Axios } from "axios";
import https from "https";
@ -41,16 +41,29 @@ export default class GitLabClient implements GitClient {
// READ
// example: <host>/api/v4/projects/<namespace>%2Fbackporting-example/merge_requests/1
async getPullRequest(namespace: string, repo: string, mrNumber: number): Promise<GitPullRequest> {
async getPullRequest(namespace: string, repo: string, mrNumber: number, squash = true): Promise<GitPullRequest> {
const projectId = this.getProjectId(namespace, repo);
const { data } = await this.client.get(`/projects/${projectId}/merge_requests/${mrNumber}`);
return this.mapper.mapPullRequest(data as MergeRequestSchema);
const commits: string[] = [];
if (!squash) {
// fetch all commits
try {
const { data } = await this.client.get(`/projects/${projectId}/merge_requests/${mrNumber}/commits`);
// gitlab returns them in reverse order
commits.push(...(data as CommitSchema[]).map(c => c.id).reverse());
} catch(error) {
throw new Error(`Failed to retrieve commits for merge request n. ${mrNumber}`);
}
}
return this.mapper.mapPullRequest(data as MergeRequestSchema, commits);
}
getPullRequestFromUrl(mrUrl: string): Promise<GitPullRequest> {
getPullRequestFromUrl(mrUrl: string, squash = true): Promise<GitPullRequest> {
const { namespace, project, id } = this.extractMergeRequestData(mrUrl);
return this.getPullRequest(namespace, project, id);
return this.getPullRequest(namespace, project, id, squash);
}
// WRITE