TDConnection の例
このセクションでは、TDConnection オブジェクトの使用方法を理解するのに役立つプロジェクト・スクリプトの例をいくつか示します。
このトピックの内容:
- 例:不具合を却下し、要件に変換する
- 例:不具合をメールで送信する
- 例:レコードのフィルタリング
- 例:添付オブジェクトからファイルをダウンロードする
- 例:不具合プロパティの取得と設定
- 例:不具合間のリンクを作成する
- 例:ターゲット・リリース・サイクルを子に入力する
- 例:要件を移動する
- 例:ソートされた要件を取得する
- 例:実行されていないカバレッジテストを取得する
例:不具合を却下し、要件に変換する
この例では、次のユースケースを示します。
不具合が「BLI に変換」という理由で却下されると、要件が自動的に作成され、不具合にリンクされます。
var BUG_STATUS = "BG_STATUS";
var BUG_REJECT_REASON = "BG_USER_01";
var BUG_CONVERTED_BLI_FOLDER_ID = 1;
async function Bug_FieldChange(fieldName) {
if (fieldName === BUG_STATUS) {
var status = Bug_Fields(BUG_STATUS).Value;
if (status === "Rejected") {
Bug_Fields(BUG_REJECT_REASON).IsRequired = true;
} else {
Bug_Fields(BUG_REJECT_REASON).IsRequired = false;
}
return;
}
if (fieldName === BUG_REJECT_REASON) {
var rejectReason = Bug_Fields(BUG_REJECT_REASON).Value;
if (rejectReason !== "Converted to BLI") return;
var isCreateBli = await MsgBox("Do you want to create a bli which links to this defect?", 1);
if (!isCreateBli) return;
try {
// Create requirement
var reqFactory = TDConnection.ReqFactory;
var reqItem = reqFactory.AddItem(null);
reqItem.Name = "Converted BLI from defect " + Bug_Fields("BG_BUG_ID").Value + ": " + Bug_Fields("BG_SUMMARY").Value;
reqItem.ParentId = BUG_CONVERTED_BLI_FOLDER_ID;
reqItem.Post();
// Create linkage
var linkFactory = reqItem.BugLinkFactory;
linkFactory.AddItem(Bug_Fields("BG_BUG_ID").Value);
var comments = Bug_Fields("BG_DEV_COMMENTS").Value;
if (comments) {
comments += "\n\n";
} else {
comments = "";
}
comments += (User.UserName + ", " + (new Date).toLocaleDateString() + ": Converted to bli " + reqItem.ID);
Bug_Fields("BG_DEV_COMMENTS").Value = comments;
} catch (e) {
debugger;
}
}
}
例:不具合をメールで送信する
次のスクリプトは、ID 1 の不具合をメールで送信します。不具合の添付と履歴もメールに含められます。このメールは「重要度 - 高」で送信されます。
var bug = TDConnection.BugFactory.Item(1)
bug.Mail(
"abc@vip.com,123@vip.com,xyz@vip.com",
"ccrecipient@vip.com",
"bccrecipient@vip.com",
1 | 2 , // Send the entity's attachments and include the entity's history in this mail
"This is the email subject line.",
1, // Set the priority as high
"This is the email comment"
)
例:レコードのフィルタリング
不具合のフィルタリング
function bugFilter() {
try {
const bugFactory = TDConnection.BugFactory;
const bugFilter = bugFactory.Filter;
// Set the filter values
bugFilter.Filter["BG_STATUS"] = "Closed";
bugFilter.Order["BG_PRIORITY"] = 1;
console.log(bugFilter.Text); // Output the filter text
// Create a list of defects from the filter and show a few of them
const bugList = bugFilter.NewList();
let msg = `Number of defects = ${bugList.length}\n`;
let count = 0;
for (const theBug of bugList) {
msg += `${theBug.ID}, ${theBug.Summary}, ${theBug.Status}, ${theBug.Priority}\n`;
count++;
if (count > 10) break; // Limiting to the first 10 bugs
}
// Display the message in a dialog box
MsgBox(msg);
} catch (error) {
console.error("Error in bugFilter:", error);
}
}
タイプによる要件のフィルタリング
function filterReqsByType() {
try {
const reqFactory = TDConnection.ReqFactory;
const filter = reqFactory.Filter;
// Use requirement type ID to fetch requirements of a specific type.
filter.Filter["RQ_TYPE_ID"] = 101;
const reqList = filter.NewList();
// Print out each requirement's details
reqList.forEach((req) => {
console.log(`${req.Name} : ${req.TypeId} : ${req.RequirementType.Name}`);
});
} catch (error) {
console.error("Error in filterReqsByType:", error);
}
}
例:添付オブジェクトからファイルをダウンロードする
function attachmentDownload() {
try {
// Get the BugFactory from the global TDConnection object
let bugFact = TDConnection.BugFactory;
// Retrieve a specific bug object by ID (in this case, ID 1)
let bugObj = bugFact.Item(1);
console.log(bugObj.Summary); // Log the bug summary for reference
// Access the attachments associated with the bug object
let attachFact = bugObj.Attachments;
// Create a new list of attachments
let attachList = attachFact.NewList("");
if (attachList.length === 0) return;
// Get the first attachment from the attachment list
let attachObj = attachList[0];
console.log(attachObj.FileName); // Log the file name of the attachment
// Load the attachment
attachObj.Load();
console.log(`Attachment downloaded);
// Return success status
return "SUCCESS";
} catch (err) {
return "FAILURE"; // Return failure status
}
}
例:不具合プロパティの取得と設定
function bugProperties(theBug) {
try {
// Print the properties.
console.log("Initial Bug properties");
console.log(theBug.Priority);
console.log(theBug.AssignedTo);
console.log(theBug.DetectedBy);
console.log(theBug.Status);
console.log(theBug.Summary);
// Change the values.
theBug.Priority = "4-Very High";
theBug.AssignedTo = "steves";
theBug.DetectedBy = "alex_alm";
theBug.Status = "Fixed";
theBug.Summary = "Amended: The list of flights is given even when past date set as Departing date";
// Commit to database.
theBug.Post();
// Check the new values.
console.log("Changed Bug properties");
console.log(theBug.Priority);
console.log(theBug.AssignedTo);
console.log(theBug.DetectedBy);
console.log(theBug.Status);
console.log(theBug.Summary);
return "SUCCESS";
} catch (err) {
return "FAILURE";
}
}
例:不具合間のリンクを作成する
async function linkDefects() {
try {
const bugF = TDConnection.BugFactory;
// Create the first bug
const bug1 = bugF.AddItem(null);
bug1.Summary = "Lydia Bennet is 15 years old.";
bug1.Status = "New";
bug1.Priority = "3-High";
bug1.Field["BG_SEVERITY"] = "3-High";
bug1.DetectedBy = User.UserName;
bug1.Field["BG_DETECTION_DATE"] = new Date();
bug1.Field["BG_RESPONSIBLE"] = User.UserName;
bug1.Post();
// Create the second bug
const bug2 = await bugF.AddItem(null);
bug2.Summary = "Mr. Bennet hides in library.";
bug2.Status = "New";
bug2.Priority = "3-High";
bug2.Field["BG_SEVERITY"] = "3-High";
bug2.DetectedBy = User.UserName;
bug2.Field["BG_DETECTION_DATE"] = new Date();
bug2.Field["BG_RESPONSIBLE"] = User.UserName;
await bug2.Post();
// Link the new defects
const bgLinkable = bug1; // Casting Bug1 to ILinkable
const bugLinkF = bgLinkable.BugLinkFactory;
// Create a link between Bug1 (source) and Bug2 (target)
const bugLink = bugLinkF.AddItem(bug2);
bugLink.LinkType = "Related";
bugLink.Post();
// Show the link definition
let anObj = bugLink.SourceEntity;
console.log(anObj.Summary); // Lydia Bennet is 15 years old. (Bug1)
anObj = bugLink.TargetEntity;
console.log(anObj.Summary); // Mr. Bennet hides in library. (Bug2)
} catch (err) {
console.error("Error in linkDefects:", err);
}
}
例:ターゲット・リリース・サイクルを子に入力する
function populateCycle() {
try {
const rFactory = TDConnection.ReqFactory;
// Fetch the requirement item by ID
const requirement = rFactory.Item(1);
// Apply the requirement's target cycle to all descendants
requirement.PopulateTargetCycleToChildren();
} catch (err) {
console.error("Error in populateCycle:", err);
}
}
例:要件を移動する
function moveReqs() {
const rqFactory = TDConnection.ReqFactory;
// Move requirements to a specific position
rqFactory.MoveRequirements("5, 4, 3", "1, 2, 3", 2);
console.assert(false, "After first move:");
// ------------------------------------------------
// Append the Reqs to the end of the existing children
const last = tagTDAPI_POS_ORDER.TDPOSITION_LAST;
const order = `${last},${last},${last}`;
rqFactory.MoveRequirements("3, 5, 4", order, 1);
console.assert(false, "After second move:");
// ------------------------------------------------
// Add the Reqs to the beginning of the existing children
rqFactory.MoveRequirements("3, 4, 5", "1, 1, 1", 2);
console.assert(false, "After third move:");
// ------------------------------------------------
// Place Reqs as specified
rqFactory.MoveRequirements("3, 4, 5", "1, 2, 2", 1);
console.assert(false, "After fourth move:");
}
例:ソートされた要件を取得する
function retrieveOrderedRecords(sinceDate, currentSyncDate) {
try {
const reqFactory = TDConnection.ReqFactory;
if (!reqFactory) {
throw new Error("Request Factory is not initialized.");
}
const filter = reqFactory.Filter;
if (!filter) {
throw new Error("Filter is not initialized.");
}
// Query criteria
filter.Filter["RQ_VTS"] = `> "${sinceDate.toISOString().slice(0, 19).replace('T', ' ')}" AND < "${currentSyncDate.toISOString().slice(0, 19).replace('T', ' ')}"`;
// Sort by path to get parent requirements in list before children
filter.Order["RQ_REQ_PATH"] = 1;
filter.OrderDirection["RQ_REQ_PATH"] = TDOLE_ASCENDING;
// Sort by version timestamp so earlier records output first
filter.Order["RQ_VTS"] = 2;
filter.OrderDirection["RQ_VTS"] = TDOLE_ASCENDING;
// Return the list
const orderedRecords = filter.NewList();
return orderedRecords;
} catch (error) {
console.error("Error in retrieveOrderedRecords:", error);
// Handle error appropriately
}
}
例:実行されていないカバレッジテストを取得する
function bugProperties(theBug) {
try {
// Print the properties.
console.log("Initial Bug properties");
console.log(theBug.Priority);
console.log(theBug.AssignedTo);
console.log(theBug.DetectedBy);
console.log(theBug.Status);
console.log(theBug.Summary);
// Change the values.
theBug.Priority = "4-Very High";
theBug.AssignedTo = "steves";
theBug.DetectedBy = "alex_alm";
theBug.Status = "Fixed";
theBug.Summary = "Amended: The list of flights is given even when past date set as Departing date";
// Commit to database.
theBug.Post();
// Check the new values.
console.log("Changed Bug properties");
console.log(theBug.Priority);
console.log(theBug.AssignedTo);
console.log(theBug.DetectedBy);
console.log(theBug.Status);
console.log(theBug.Summary);
return "SUCCESS";
} catch (err) {
return "FAILURE";
}
}