mirror of
https://github.com/kiegroup/git-backporting.git
synced 2025-06-28 21:53:47 +00:00
feat: integrate tool with gitlab service (#39)
* feat: integrate tool with gitlab service Fix https://github.com/lampajr/backporting/issues/30
This commit is contained in:
parent
8a007941d1
commit
107f5e52d6
35 changed files with 17821 additions and 1553 deletions
83
src/service/git/gitlab/gitlab-mapper.ts
Normal file
83
src/service/git/gitlab/gitlab-mapper.ts
Normal file
|
@ -0,0 +1,83 @@
|
|||
import { GitPullRequest, GitRepoState, GitRepository } from "@bp/service/git/git.types";
|
||||
import GitResponseMapper from "@bp/service/git/git-mapper";
|
||||
import { MergeRequestSchema, ProjectSchema } from "@gitbeaker/rest";
|
||||
import { Axios } from "axios";
|
||||
|
||||
export default class GitLabMapper implements GitResponseMapper<MergeRequestSchema, string> {
|
||||
|
||||
private readonly client;
|
||||
// needs client to perform additional requests
|
||||
constructor(client: Axios) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
mapGitState(state: string): GitRepoState {
|
||||
switch (state) {
|
||||
case "opened":
|
||||
return GitRepoState.OPEN;
|
||||
case "closed":
|
||||
return GitRepoState.CLOSED;
|
||||
case "merged":
|
||||
return GitRepoState.MERGED;
|
||||
default:
|
||||
return GitRepoState.LOCKED;
|
||||
}
|
||||
}
|
||||
|
||||
async mapPullRequest(mr: MergeRequestSchema): Promise<GitPullRequest> {
|
||||
// throw new Error("Method not implemented.");
|
||||
return {
|
||||
number: mr.iid,
|
||||
author: mr.author.username,
|
||||
url: mr.web_url,
|
||||
htmlUrl: mr.web_url,
|
||||
title: mr.title,
|
||||
body: mr.description,
|
||||
state: this.mapGitState(mr.state),
|
||||
merged: this.isMerged(mr),
|
||||
mergedBy: mr.merged_by?.username,
|
||||
reviewers: mr.reviewers?.map((r => r.username)) ?? [],
|
||||
assignees: mr.assignees?.map((r => r.username)) ?? [],
|
||||
sourceRepo: await this.mapSourceRepo(mr),
|
||||
targetRepo: await this.mapTargetRepo(mr),
|
||||
nCommits: 1, // info not present on mr
|
||||
// if mr is merged, use merge_commit_sha otherwise use sha
|
||||
// what is the difference between sha and diff_refs.head_sha?
|
||||
commits: this.isMerged(mr) ? [mr.squash_commit_sha ? mr.squash_commit_sha : mr.merge_commit_sha as string] : [mr.sha]
|
||||
};
|
||||
}
|
||||
|
||||
async mapSourceRepo(mr: MergeRequestSchema): Promise<GitRepository> {
|
||||
const project: ProjectSchema = await this.getProject(mr.source_project_id);
|
||||
|
||||
return {
|
||||
owner: project.namespace.full_path, // or just proj.path?
|
||||
project: project.path,
|
||||
cloneUrl: project.http_url_to_repo,
|
||||
};
|
||||
}
|
||||
|
||||
async mapTargetRepo(mr: MergeRequestSchema): Promise<GitRepository> {
|
||||
const project: ProjectSchema = await this.getProject(mr.target_project_id);
|
||||
|
||||
return {
|
||||
owner: project.namespace.full_path, // or just proj.path?
|
||||
project: project.path,
|
||||
cloneUrl: project.http_url_to_repo,
|
||||
};
|
||||
}
|
||||
|
||||
private isMerged(mr: MergeRequestSchema) {
|
||||
return this.mapGitState(mr.state) === GitRepoState.MERGED;
|
||||
}
|
||||
|
||||
private async getProject(projectId: number): Promise<ProjectSchema> {
|
||||
const { data } = await this.client.get(`/projects/${projectId}`);
|
||||
|
||||
if (!data) {
|
||||
throw new Error(`Project ${projectId} not found`);
|
||||
}
|
||||
|
||||
return data as ProjectSchema;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue