|
| 1 | +import { WebApi } from "azure-devops-node-api"; |
| 2 | +import { IGitApi } from "azure-devops-node-api/GitApi"; |
| 3 | +import { IRequestHandler } from "azure-devops-node-api/interfaces/common/VsoBaseInterfaces"; |
| 4 | +import { |
| 5 | + Comment, |
| 6 | + CommentThreadStatus, |
| 7 | + GitPullRequestCommentThread, |
| 8 | +} from "azure-devops-node-api/interfaces/GitInterfaces"; |
| 9 | +import AzureDevOpsApiWrapper from "../azureDevopsApiClient"; |
| 10 | +import { validateVariable } from "../envVariables/validator"; |
| 11 | +import CommentData from "../models/PullRequests/CommentData"; |
| 12 | +import FileCommentData from "../models/PullRequests/FileCommentData"; |
| 13 | +import PullRequestCommentData from "../models/PullRequests/PullRequestCommentData"; |
| 14 | + |
| 15 | +export class client { |
| 16 | + public azureDevOpsApiWrapper: AzureDevOpsApiWrapper = |
| 17 | + new AzureDevOpsApiWrapper(); |
| 18 | + |
| 19 | + public project: string = ""; |
| 20 | + public repositoryId: string = ""; |
| 21 | + public pullRequestId: number = 0; |
| 22 | + public gitApi: IGitApi | undefined; |
| 23 | + |
| 24 | + /** |
| 25 | + * Gets all the comments for the pull request. |
| 26 | + * @returns Promise<CommentData> |
| 27 | + */ |
| 28 | + public async getComments(): Promise<CommentData> { |
| 29 | + const gitApiPromise: IGitApi = await this.getGitApi(); |
| 30 | + |
| 31 | + const threads: GitPullRequestCommentThread[] = |
| 32 | + await gitApiPromise.getThreads( |
| 33 | + this.repositoryId, |
| 34 | + this.pullRequestId, |
| 35 | + this.project |
| 36 | + ); |
| 37 | + |
| 38 | + console.log(JSON.stringify(threads)); |
| 39 | + |
| 40 | + return client.convertPullRequestComments(threads); |
| 41 | + } |
| 42 | + |
| 43 | + public async createComment( |
| 44 | + content: string, |
| 45 | + status: CommentThreadStatus |
| 46 | + ): Promise<void> { |
| 47 | + console.debug("* AzdoRepoClient.createComment()"); |
| 48 | + |
| 49 | + const gitApi = await this.getGitApi(); |
| 50 | + |
| 51 | + const commentThread: GitPullRequestCommentThread = { |
| 52 | + comments: [{ content }], |
| 53 | + status, |
| 54 | + }; |
| 55 | + |
| 56 | + const result: GitPullRequestCommentThread = await gitApi.createThread( |
| 57 | + commentThread, |
| 58 | + this.repositoryId, |
| 59 | + this.pullRequestId, |
| 60 | + this.project |
| 61 | + ); |
| 62 | + |
| 63 | + console.debug(JSON.stringify(result)); |
| 64 | + } |
| 65 | + |
| 66 | + public async updateComment( |
| 67 | + commentThreadId: number, |
| 68 | + content: string | null, |
| 69 | + status: CommentThreadStatus | null |
| 70 | + ): Promise<void> { |
| 71 | + console.debug("* AzdoRepoClient.updateComment()"); |
| 72 | + |
| 73 | + if (content === null && status === null) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + const gitApi = await this.getGitApi(); |
| 78 | + if (content !== null) { |
| 79 | + const comment: Comment = { |
| 80 | + content, |
| 81 | + }; |
| 82 | + |
| 83 | + const commentResult: Comment = await gitApi.updateComment( |
| 84 | + comment, |
| 85 | + this.repositoryId, |
| 86 | + this.pullRequestId, |
| 87 | + commentThreadId, |
| 88 | + 1, |
| 89 | + this.project |
| 90 | + ); |
| 91 | + |
| 92 | + console.debug(JSON.stringify(commentResult)); |
| 93 | + } |
| 94 | + |
| 95 | + if (status !== null) { |
| 96 | + const commentThread: GitPullRequestCommentThread = { |
| 97 | + status, |
| 98 | + }; |
| 99 | + |
| 100 | + const threadResult: GitPullRequestCommentThread = |
| 101 | + await gitApi.updateThread( |
| 102 | + commentThread, |
| 103 | + this.repositoryId, |
| 104 | + this.pullRequestId, |
| 105 | + commentThreadId, |
| 106 | + this.project |
| 107 | + ); |
| 108 | + |
| 109 | + console.debug(JSON.stringify(threadResult)); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Translate API data to our model. |
| 115 | + * @param threads |
| 116 | + * @returns |
| 117 | + */ |
| 118 | + private static convertPullRequestComments( |
| 119 | + threads: GitPullRequestCommentThread[] |
| 120 | + ): CommentData { |
| 121 | + const result: CommentData = new CommentData(); |
| 122 | + |
| 123 | + threads.forEach((thread: GitPullRequestCommentThread): void => { |
| 124 | + const id: number = thread.id ?? 0; |
| 125 | + const comments: Comment[] | undefined = thread.comments; |
| 126 | + if (comments === undefined) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + const initialThreadComment: string | undefined = comments[0]?.content; |
| 131 | + if (initialThreadComment === undefined || initialThreadComment === "") { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + const threadStatus: CommentThreadStatus = |
| 136 | + thread.status ?? CommentThreadStatus.Unknown; |
| 137 | + |
| 138 | + if (thread.threadContext === null || thread.threadContext === undefined) { |
| 139 | + result.pullRequestComments.push( |
| 140 | + new PullRequestCommentData(id, initialThreadComment, threadStatus) |
| 141 | + ); |
| 142 | + } else { |
| 143 | + const fileName: string | undefined = thread.threadContext.filePath; |
| 144 | + if (fileName === undefined || fileName.length <= 1) { |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + result.fileComments.push( |
| 149 | + new FileCommentData( |
| 150 | + id, |
| 151 | + initialThreadComment, |
| 152 | + fileName.substring(1), |
| 153 | + threadStatus |
| 154 | + ) |
| 155 | + ); |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + return result; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Configures the client to use the Azure DevOps API. |
| 164 | + * @returns Promise<IGitApi> |
| 165 | + */ |
| 166 | + private async getGitApi(): Promise<IGitApi> { |
| 167 | + console.debug("* client.getGitApi()"); |
| 168 | + |
| 169 | + if (this.gitApi !== undefined) { |
| 170 | + return this.gitApi; |
| 171 | + } |
| 172 | + |
| 173 | + this.initPRVars(); |
| 174 | + |
| 175 | + const accessToken: string = validateVariable( |
| 176 | + "SYSTEM_ACCESS_TOKEN", |
| 177 | + "client.getGitApi()" |
| 178 | + ); |
| 179 | + const authHandler: IRequestHandler = |
| 180 | + this.azureDevOpsApiWrapper.getPersonalAccessTokenHandler(accessToken); |
| 181 | + |
| 182 | + const defaultUrl: string = validateVariable( |
| 183 | + "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI", |
| 184 | + "client.getGitApi()" |
| 185 | + ); |
| 186 | + const connection: WebApi = this.azureDevOpsApiWrapper.getWebApiInstance( |
| 187 | + defaultUrl, |
| 188 | + authHandler |
| 189 | + ); |
| 190 | + this.gitApi = await connection.getGitApi(); |
| 191 | + |
| 192 | + if (!this.gitApi) { |
| 193 | + throw new Error("Could not get GitApi"); |
| 194 | + } |
| 195 | + |
| 196 | + return this.gitApi; |
| 197 | + } |
| 198 | + |
| 199 | + public initPRVars() { |
| 200 | + this.project = validateVariable("SYSTEM_TEAMPROJECT", "client.getGitApi()"); |
| 201 | + |
| 202 | + this.repositoryId = validateVariable( |
| 203 | + "BUILD_REPOSITORY_ID", |
| 204 | + "client.getGitApi()" |
| 205 | + ); |
| 206 | + |
| 207 | + this.pullRequestId = parseInt( |
| 208 | + validateVariable( |
| 209 | + "SYSTEM_PULLREQUEST_PULLREQUESTID", |
| 210 | + "pullRequestIdForAzurePipelines" |
| 211 | + ), |
| 212 | + 10 |
| 213 | + ); |
| 214 | + } |
| 215 | +} |
0 commit comments