Dmitrii Filippov | fc3e4f7 | 2020-10-22 16:57:57 +0200 | [diff] [blame] | 1 | /** |
| 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 | |
| 18 | import { |
| 19 | AccountId, |
| 20 | AccountInfo, |
| 21 | BranchName, |
| 22 | ChangeId, |
| 23 | ChangeInfo, |
| 24 | ChangeInfoId, |
Dmitrii Filippov | 0695d40 | 2020-10-22 16:57:57 +0200 | [diff] [blame^] | 25 | ChangeMessageId, |
| 26 | ChangeMessageInfo, |
Dmitrii Filippov | fc3e4f7 | 2020-10-22 16:57:57 +0200 | [diff] [blame] | 27 | CommentLinkInfo, |
| 28 | CommentLinks, |
Dmitrii Filippov | 0695d40 | 2020-10-22 16:57:57 +0200 | [diff] [blame^] | 29 | CommitInfo, |
Dmitrii Filippov | fc3e4f7 | 2020-10-22 16:57:57 +0200 | [diff] [blame] | 30 | ConfigInfo, |
| 31 | EmailAddress, |
Dmitrii Filippov | 0695d40 | 2020-10-22 16:57:57 +0200 | [diff] [blame^] | 32 | GitPersonInfo, |
| 33 | GitRef, |
Dmitrii Filippov | fc3e4f7 | 2020-10-22 16:57:57 +0200 | [diff] [blame] | 34 | InheritedBooleanInfo, |
| 35 | MaxObjectSizeLimitInfo, |
| 36 | NumericChangeId, |
Dmitrii Filippov | 0695d40 | 2020-10-22 16:57:57 +0200 | [diff] [blame^] | 37 | PatchSetNum, |
Dmitrii Filippov | fc3e4f7 | 2020-10-22 16:57:57 +0200 | [diff] [blame] | 38 | RepoName, |
| 39 | Reviewers, |
Dmitrii Filippov | 0695d40 | 2020-10-22 16:57:57 +0200 | [diff] [blame^] | 40 | RevisionInfo, |
Dmitrii Filippov | fc3e4f7 | 2020-10-22 16:57:57 +0200 | [diff] [blame] | 41 | SubmitTypeInfo, |
| 42 | Timestamp, |
Dmitrii Filippov | 0695d40 | 2020-10-22 16:57:57 +0200 | [diff] [blame^] | 43 | TimezoneOffset, |
Dmitrii Filippov | fc3e4f7 | 2020-10-22 16:57:57 +0200 | [diff] [blame] | 44 | } from '../types/common'; |
| 45 | import { |
| 46 | ChangeStatus, |
| 47 | InheritedBooleanInfoConfiguredValue, |
Dmitrii Filippov | 0695d40 | 2020-10-22 16:57:57 +0200 | [diff] [blame^] | 48 | RevisionKind, |
Dmitrii Filippov | fc3e4f7 | 2020-10-22 16:57:57 +0200 | [diff] [blame] | 49 | SubmitType, |
| 50 | } from '../constants/constants'; |
| 51 | import {formatDate} from '../utils/date-util'; |
| 52 | |
| 53 | export 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 | |
| 59 | export function createCommentLink(match = 'test'): CommentLinkInfo { |
| 60 | return { |
| 61 | match, |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | export function createInheritedBoolean(value = false): InheritedBooleanInfo { |
| 66 | return { |
| 67 | value, |
| 68 | configured_value: value |
| 69 | ? InheritedBooleanInfoConfiguredValue.TRUE |
| 70 | : InheritedBooleanInfoConfiguredValue.FALSE, |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | export function createMaxObjectSizeLimit(): MaxObjectSizeLimitInfo { |
| 75 | return {}; |
| 76 | } |
| 77 | |
| 78 | export 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 | |
| 88 | export function createCommentLinks(): CommentLinks { |
| 89 | return {}; |
| 90 | } |
| 91 | |
| 92 | export 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 | |
| 103 | export function createAccountWithId(id = 5): AccountInfo { |
| 104 | return { |
| 105 | _account_id: id as AccountId, |
| 106 | }; |
| 107 | } |
| 108 | |
| 109 | export function createAccountWithEmail(email = 'test@'): AccountInfo { |
| 110 | return { |
| 111 | email: email as EmailAddress, |
| 112 | }; |
| 113 | } |
| 114 | |
| 115 | export 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 | |
| 123 | export function createReviewers(): Reviewers { |
| 124 | return {}; |
| 125 | } |
| 126 | |
| 127 | export const TEST_PROJECT_NAME: RepoName = 'test-project' as RepoName; |
| 128 | export const TEST_BRANCH_ID: BranchName = 'test-branch' as BranchName; |
| 129 | export const TEST_CHANGE_ID: ChangeId = 'TestChangeId' as ChangeId; |
| 130 | export const TEST_CHANGE_INFO_ID: ChangeInfoId = `${TEST_PROJECT_NAME}~${TEST_BRANCH_ID}~${TEST_CHANGE_ID}` as ChangeInfoId; |
| 131 | export const TEST_SUBJECT = 'Test subject'; |
| 132 | export const TEST_NUMERIC_CHANGE_ID = 42 as NumericChangeId; |
| 133 | |
| 134 | export const TEST_CHANGE_CREATED = new Date(2020, 1, 1, 1, 2, 3); |
| 135 | export const TEST_CHANGE_UPDATED = new Date(2020, 10, 6, 5, 12, 34); |
| 136 | |
Dmitrii Filippov | 0695d40 | 2020-10-22 16:57:57 +0200 | [diff] [blame^] | 137 | export 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 | |
| 146 | export 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 | |
| 156 | export 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 | |
| 167 | export 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 | |
| 175 | export 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 | |
| 196 | export 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 Filippov | fc3e4f7 | 2020-10-22 16:57:57 +0200 | [diff] [blame] | 212 | export 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 | } |