blob: 8e276071a3a63d88aa01e72e0e614b341e9eb10c [file] [log] [blame]
Dmitrii Filippovfc3e4f72020-10-22 16:57:57 +02001/**
2 * @license
3 * Copyright (C) 2020 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18import {
19 AccountId,
20 AccountInfo,
21 BranchName,
22 ChangeId,
23 ChangeInfo,
24 ChangeInfoId,
Dmitrii Filippov0695d402020-10-22 16:57:57 +020025 ChangeMessageId,
26 ChangeMessageInfo,
Dmitrii Filippovfc3e4f72020-10-22 16:57:57 +020027 CommentLinkInfo,
28 CommentLinks,
Dmitrii Filippov0695d402020-10-22 16:57:57 +020029 CommitInfo,
Dmitrii Filippovfc3e4f72020-10-22 16:57:57 +020030 ConfigInfo,
31 EmailAddress,
Dmitrii Filippov0695d402020-10-22 16:57:57 +020032 GitPersonInfo,
33 GitRef,
Dmitrii Filippovfc3e4f72020-10-22 16:57:57 +020034 InheritedBooleanInfo,
35 MaxObjectSizeLimitInfo,
36 NumericChangeId,
Dmitrii Filippov0695d402020-10-22 16:57:57 +020037 PatchSetNum,
Dmitrii Filippovfc3e4f72020-10-22 16:57:57 +020038 RepoName,
39 Reviewers,
Dmitrii Filippov0695d402020-10-22 16:57:57 +020040 RevisionInfo,
Dmitrii Filippovfc3e4f72020-10-22 16:57:57 +020041 SubmitTypeInfo,
42 Timestamp,
Dmitrii Filippov0695d402020-10-22 16:57:57 +020043 TimezoneOffset,
Dmitrii Filippovfc3e4f72020-10-22 16:57:57 +020044} from '../types/common';
45import {
46 ChangeStatus,
47 InheritedBooleanInfoConfiguredValue,
Dmitrii Filippov0695d402020-10-22 16:57:57 +020048 RevisionKind,
Dmitrii Filippovfc3e4f72020-10-22 16:57:57 +020049 SubmitType,
50} from '../constants/constants';
51import {formatDate} from '../utils/date-util';
52
53export function dateToTimestamp(date: Date): Timestamp {
54 const nanosecondSuffix = '.000000000';
55 return (formatDate(date, 'YYYY-MM-DD HH:mm:ss') +
56 nanosecondSuffix) as Timestamp;
57}
58
59export function createCommentLink(match = 'test'): CommentLinkInfo {
60 return {
61 match,
62 };
63}
64
65export function createInheritedBoolean(value = false): InheritedBooleanInfo {
66 return {
67 value,
68 configured_value: value
69 ? InheritedBooleanInfoConfiguredValue.TRUE
70 : InheritedBooleanInfoConfiguredValue.FALSE,
71 };
72}
73
74export function createMaxObjectSizeLimit(): MaxObjectSizeLimitInfo {
75 return {};
76}
77
78export function createSubmitType(
79 value: Exclude<SubmitType, SubmitType.INHERIT> = SubmitType.MERGE_IF_NECESSARY
80): SubmitTypeInfo {
81 return {
82 value,
83 configured_value: SubmitType.INHERIT,
84 inherited_value: value,
85 };
86}
87
88export function createCommentLinks(): CommentLinks {
89 return {};
90}
91
92export function createConfig(): ConfigInfo {
93 return {
94 private_by_default: createInheritedBoolean(),
95 work_in_progress_by_default: createInheritedBoolean(),
96 max_object_size_limit: createMaxObjectSizeLimit(),
97 default_submit_type: createSubmitType(),
98 submit_type: SubmitType.INHERIT,
99 commentlinks: createCommentLinks(),
100 };
101}
102
103export function createAccountWithId(id = 5): AccountInfo {
104 return {
105 _account_id: id as AccountId,
106 };
107}
108
109export function createAccountWithEmail(email = 'test@'): AccountInfo {
110 return {
111 email: email as EmailAddress,
112 };
113}
114
115export function createAccountWithIdNameAndEmail(id = 5): AccountInfo {
116 return {
117 _account_id: id as AccountId,
118 email: `user-${id}@` as EmailAddress,
119 name: `User-${id}`,
120 };
121}
122
123export function createReviewers(): Reviewers {
124 return {};
125}
126
127export const TEST_PROJECT_NAME: RepoName = 'test-project' as RepoName;
128export const TEST_BRANCH_ID: BranchName = 'test-branch' as BranchName;
129export const TEST_CHANGE_ID: ChangeId = 'TestChangeId' as ChangeId;
130export const TEST_CHANGE_INFO_ID: ChangeInfoId = `${TEST_PROJECT_NAME}~${TEST_BRANCH_ID}~${TEST_CHANGE_ID}` as ChangeInfoId;
131export const TEST_SUBJECT = 'Test subject';
132export const TEST_NUMERIC_CHANGE_ID = 42 as NumericChangeId;
133
134export const TEST_CHANGE_CREATED = new Date(2020, 1, 1, 1, 2, 3);
135export const TEST_CHANGE_UPDATED = new Date(2020, 10, 6, 5, 12, 34);
136
Dmitrii Filippov0695d402020-10-22 16:57:57 +0200137export function createGitPerson(): GitPersonInfo {
138 return {
139 name: 'Test person',
140 email: 'email@google.com',
141 date: dateToTimestamp(new Date(2019, 11, 6, 14, 5, 8)),
142 tz: 0 as TimezoneOffset,
143 };
144}
145
146export function createCommit(): CommitInfo {
147 return {
148 parents: [],
149 author: createGitPerson(),
150 committer: createGitPerson(),
151 subject: 'Test commit subject',
152 message: 'Test commit message',
153 };
154}
155
156export function createRevision(): RevisionInfo {
157 return {
158 _number: 1 as PatchSetNum,
159 commit: createCommit(),
160 created: dateToTimestamp(TEST_CHANGE_CREATED),
161 kind: RevisionKind.REWORK,
162 ref: 'refs/changes/5/6/1' as GitRef,
163 uploader: createAccountWithId(),
164 };
165}
166
167export function createChangeMessage(): ChangeMessageInfo {
168 return {
169 id: '1000' as ChangeMessageId,
170 date: dateToTimestamp(TEST_CHANGE_CREATED),
171 message: 'This is a message',
172 };
173}
174
175export function createRevisions(
176 count: number
177): {[revisionId: string]: RevisionInfo} {
178 const revisions: {[revisionId: string]: RevisionInfo} = {};
179 const revisionDate = TEST_CHANGE_CREATED;
180 const revisionIdStart = 1;
181 for (let i = 0; i < count; i++) {
182 const revisionId = (i + revisionIdStart).toString(16);
183 const revision: RevisionInfo = {
184 ...createRevision(),
185 _number: (i + 1) as PatchSetNum,
186 created: dateToTimestamp(revisionDate),
187 ref: `refs/changes/5/6/${i + 1}` as GitRef,
188 };
189 revisions[revisionId] = revision;
190 // advance 1 day
191 revisionDate.setDate(revisionDate.getDate() + 1);
192 }
193 return revisions;
194}
195
196export function createChangeMessages(count: number): ChangeMessageInfo[] {
197 const messageIdStart = 1000;
198 const messages: ChangeMessageInfo[] = [];
199 const messageDate = TEST_CHANGE_CREATED;
200 for (let i = 0; i < count; i++) {
201 messages.push({
202 ...createChangeMessage(),
203 id: (i + messageIdStart).toString(16) as ChangeMessageId,
204 date: dateToTimestamp(messageDate),
205 message: `This is a message N${i + 1}`,
206 });
207 messageDate.setDate(messageDate.getDate() + 1);
208 }
209 return messages;
210}
211
Dmitrii Filippovfc3e4f72020-10-22 16:57:57 +0200212export function createChange(): ChangeInfo {
213 return {
214 id: TEST_CHANGE_INFO_ID,
215 project: TEST_PROJECT_NAME,
216 branch: TEST_BRANCH_ID,
217 change_id: TEST_CHANGE_ID,
218 subject: TEST_SUBJECT,
219 status: ChangeStatus.NEW,
220 created: dateToTimestamp(TEST_CHANGE_CREATED),
221 updated: dateToTimestamp(TEST_CHANGE_UPDATED),
222 insertions: 0,
223 deletions: 0,
224 _number: TEST_NUMERIC_CHANGE_ID,
225 owner: createAccountWithId(),
226 // This is documented as optional, but actually always set.
227 reviewers: createReviewers(),
228 };
229}