comment.ts•1.79 kB
import axios from "axios";
import * as dotenv from "dotenv";
// Load environment variables
dotenv.config();
// Configurations
const AZURE_PAT = process.env.AZURE_PAT;
/*
Creates a comment in a specific line of the PR
@param pr_info: { organization: string, project: string, repositoryId: string, pullRequestNuber: string } - PR information
@param file_path: string - File path
@param line_number: number - Line number
@param comment_content: string - Comment content
*/
export async function createThreadComment(
prInfo: {
organization: string;
project: string;
repositoryId: string;
pullRequestNumber: string;
},
filePath: string,
lineNumber: number,
commentContent: string
) {
if (!AZURE_PAT) {
throw new Error("AZURE_PAT is not set");
}
const { organization, project, repositoryId, pullRequestNumber } = prInfo;
const url = `https://dev.azure.com/${organization}/${project}/_apis/git/repositories/${repositoryId}/pullRequests/${pullRequestNumber}/threads?api-version=7.1-preview.1`;
const payload = {
comments: [
{
content: commentContent,
},
],
status: "active",
threadContext: {
filePath: "/" + filePath,
rightFileStart: {
line: lineNumber,
offset: 1,
},
rightFileEnd: {
line: lineNumber,
offset: 1,
},
},
};
const response = await axios.post(url, payload, {
auth: {
username: "",
password: AZURE_PAT,
},
});
if (response.status === 200 || response.status === 201) {
console.log(`Comment created successfully for ${filePath}:${lineNumber}`);
return true;
} else {
console.log(
`Error creating comment: ${response.status} - ${response.data}`
);
return false;
}
}