技术开发 频道

ClearQuest的Hook机制

   7) "LogBase"的Notification Hook:

    为实现在日志文件中记录所有提交执行的操作的功能,我们定义了BASE类型操作 "LogBase",并为其定义了Notification Hook。这个Hook调用了Global Hook"BuildLogEntry"。

    点击LogBase 操作的Commit hook,选择SCRIPTS -> Perl,编写如下的脚本:

    sub defect_Notification {

    # Post-commit notifications about actions may be handled here.

    my($session, $logFile, $login, $logEntry);

    $session = $entity->GetSession();

    # 操作执行的信息记录在f:\\tmp\\actions.log中

    open($logFile, "+>>f:\\tmp\\actions.log") ||

    $session->OutputDebugString("open actions.log failed");

    $login = $session->GetUserLoginName();

    $logEntry = BuildLogEntry($actionname, $login);

    print $logFile $logEntry;

    close($logFile);

    }

    8) Delete权限控制Hook:

    限制只有特定组"defectcoordinators"的用户才能删除defects。

    这个功能也可以对Delete操作的权限控制直接定义用户组来实现。这里是为了演示Hook的功能。使用Hook可以实现更复杂的逻辑。

    点击Delete操作的Access Control hook,选择SCRIPTS -> Perl,编写如下的脚本:

    sub defect_AccessControl {

    my $session = $entity->GetSession();

    my @groups;

    $groups = $session->GetUserGroups();

    $session->OutputDebugString("\nThe first groups is ".@$groups[0]);

    $result = 0;

    my $group;

    for $group (@$groups){

    if ($group eq "defectcoordinators"){

    $result = 1;

    break;

    }

    }

    }

    9) 记录类型hook

    最后,为了演示记录类型hook的使用方法,我们再定义一个类型为RECORD_SCRIPT_ALIAS的操作"ExportDefect"。它所关联的record scripts是我们定义的记录类型hook "defect_ExportDefectToTxt"。defect_ExportDefectToTxt将当前defect基本信息写到一个文本文件中:

    首先先定义一个记录类型hook"defect_ExportDefectToTxt"。在模板中,右击Record Types ->defect ->Record Scripts -> Perl,选择Add添加record script,并编写如下脚本:

    sub defect_ ExportDefectToTxt {

    my($result);

    if (ref ($param) eq "CQEventObject") {

    # add your CQEventObject parameter handling code here

    my($MYFILE, $session, $DefectId, $description);

    $session = $entity->GetSession();

    $DefectId = $entity->GetFieldValue("defectid")->GetValue();

    open($MYFILE, "+>f:\\tmp\\Exported_".$DefectId.".defect") ||

    $session->OutputDebugString("\nExport File Open failed!");

    $DefectId = $entity->GetFieldValue("DefectId")->GetValue();

    $description = $entity->GetFieldValue("Description")->GetValue();

    print $MYFILE ("\nDefectID: " . $DefectId . "\nDefectDescription: " . $description);

    close($MYFILE);

    } elsif (ref (\$param) eq "SCALAR") {

    # add your scalar parameter handling code here

    } else {

    # add your handling code for other type parameters here, for example:

    # die("Unknown parameter type");

    }

    }

    然后,回到actions定义窗口,点击操作类型为RECORD_SCRIPT_ALIAS的 "ExportDefect"的Record Scripts,选择刚才定义的record script。这样,在ClearQuest客户端选择操作"ExportDefect"就会触发defect_ ExportDefectToTxt Hook脚本。

    最后,为defect记录类型定义一个表单。如下图所示:

    ClearQuest客户端的实际操作

    现在,我们来实际演示一下在实际操作中这些Hooks是如何发生作用的。

    首先创建用户组"defectcoordinators"和用户"Mary""Rose",其中"Mary"是"defectcoordinators"的组员。根据刚才创建的模板生成一个用户数据库,将用户组"defectcoordinators"和用户"Rose"加入此数据库。然后打开ClearQuest客户端,以用户Mary的身份连接到刚才创建的用户数据库,做如下操作:

    1)创建一个defect纪录。DefectId值是系统调用Defect的Default Value Hook自动生成的。如下图所示:

    2)从下拉列表中选择OsType的值,由于触发了OsType的Value Changed Hook,OsVersion的值会自动设上:

    3)我们创建4个defects用于测试系统duplicate的功能。通过Duplicate操作将defect2、defect4设为defect1的duplicate,defect4又设为defect2的duplicate。

    4)试图将defect2设为验证通过,打开defect2,点击其Action列表中的Accept。由于Accept的Access Control Hook不允许针对duplicate状态下的defect操作,所以得到错误信息,操作不能执行:

    6) 对defect1进行Fix和Accept操作,当Apply Accept操作(也就是提交Accept操作)时,defect1的Accept 操作的Commit hook被调用,所有直接和间接的duplicate defects都同时被验证通过:(需要刷新才能看到所有defect的最新状态)

    7)察看系统的actions.log日志文件记录了所有提交执行的操作。这是目前actions.log的最后部分:

    *** Action committed ***

    Action Taken: Dup

    User: Mary

    *** Action committed ***

    Action Taken: Dup

    User: Mary

    *** Action committed ***

    Action Taken: Dup

    User: Mary

    *** Action committed ***

    Action Taken: Fix

    User: Mary

    *** Action committed ***

    Action Taken: Accept

    User: Mary

    8) 在defect 记录的表单中,有我们自定义的操作Exportdefect选项:

    对defect4点击Exportdefect进行操作,触发调用了我们定义的记录类型hook "defect_ExportToTxt",当前的defect信息会写入对应的文件Exported_4.defect中,其内容为:

    DefectID: 4

    DefectDescription: duplicate to 1

    9) 以另外一个用户Rose的身份登陆,尝试删除某个defect。Delete的Access Control Hook检测到Rose不是defectcoordinators组的成员,操作不能执行:

    在刚才的一系列操作中,我们触发调用了本模板定义的所有Hooks,均能正常工作。

    总结

    Hooks是ClearQuest提供的一个很重要、很有用的功能。利用它就可以非常灵活的定义变更管理模板,扩展ClearQuest的功能。同时,使用编写Hooks也是非常的方便。
 

0
相关文章