提交 b4754dc2 编写于 作者: T Tomas Vik

refactor: extract methods for parsing GqlPosition

上级 ca94f7cd
......@@ -5,6 +5,7 @@ import { GitLabComment } from './gitlab_comment';
import { toReviewUri } from './review_uri';
import { GqlTextDiffDiscussion } from '../gitlab/graphql/get_discussions';
import { GqlNote, GqlTextDiffNote, GqlTextPosition } from '../gitlab/graphql/shared';
import { commitFromPosition, pathFromPosition } from './gql_position_parser';
const firstNoteFrom = (discussion: GqlTextDiffDiscussion): GqlTextDiffNote => {
const note = discussion.notes.nodes[0];
......@@ -22,13 +23,6 @@ const commentRangeFromPosition = (position: GqlTextPosition): vscode.Range => {
return new vscode.Range(vsPosition, vsPosition);
};
const pathAndCommitFromPosition = (position: GqlTextPosition) => {
const onOldVersion = position.oldLine !== null;
const path = onOldVersion ? position.oldPath : position.newPath;
const commit = onOldVersion ? position.diffRefs.baseSha : position.diffRefs.headSha;
return { path, commit };
};
interface CreateThreadOptions {
commentController: vscode.CommentController;
repositoryRoot: string;
......@@ -141,7 +135,8 @@ export class GitLabCommentThread {
const { position } = firstNoteFrom(discussion);
const vsThread = commentController.createCommentThread(
toReviewUri({
...pathAndCommitFromPosition(position),
path: pathFromPosition(position),
commit: commitFromPosition(position),
repositoryRoot,
projectId: mr.project_id,
mrId: mr.id,
......
import { commitFromPosition, pathFromPosition } from './gql_position_parser';
import { noteOnDiff } from '../../test/integration/fixtures/graphql/discussions.js';
import { GqlTextPosition } from '../gitlab/graphql/shared';
const { position } = noteOnDiff;
const oldPosition = {
...position,
oldLine: 1,
newLine: null,
oldPath: 'oldPath.js',
diffRefs: {
...position.diffRefs,
baseSha: 'abcd',
},
} as GqlTextPosition;
const newPosition = {
...position,
oldLine: null,
newLine: 1,
newPath: 'newPath.js',
diffRefs: {
...position.diffRefs,
headSha: '1234',
},
} as GqlTextPosition;
describe('pathFromPosition', () => {
it('returns old path for old position', () => {
expect(pathFromPosition(oldPosition)).toBe('oldPath.js');
});
it('returns new path for new position', () => {
expect(pathFromPosition(newPosition)).toBe('newPath.js');
});
});
describe('commitFromPosition', () => {
it('returns baseSha for old position', () => {
expect(commitFromPosition(oldPosition)).toBe('abcd');
});
it('returns headSha for new position', () => {
expect(commitFromPosition(newPosition)).toBe('1234');
});
});
import { GqlTextPosition } from '../gitlab/graphql/shared';
const isOld = (position: GqlTextPosition) => position.oldLine !== null;
export const pathFromPosition = (position: GqlTextPosition): string => {
return isOld(position) ? position.oldPath : position.newPath;
};
export const commitFromPosition = (position: GqlTextPosition): string => {
return isOld(position) ? position.diffRefs.baseSha : position.diffRefs.headSha;
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册