1

Does anyone tried to retrieve PolicyDefinition using Java SDK of TFS 2015?

I have used following code to retrieve the PolicyDefinition under intelliJ Plugin. Code execute fine but I am getting empty collection of PolicyDefinition. I am working on CustomPath Policy and I know on particular team project customPath Policy is applied.

Before allowing user checkin I want to validate that weather this policy is applied or not.

Sample Code:

Project myProject = policyContext.getProject(); // PolicyContext is received from intelliJ and I got Project
                                                // object opened in intelliJ

Workstation workstation = Workstation.getInstance(); // Workspace Info, it depeneds how many workspaces created
                                                        // to map TFS solution
Collection<WorkspaceInfo> workspaceInfos = workstation
        .findWorkspacesCached(TfsFileUtil.getFilePath(myProject.getBaseDir()), false); // This will return the
                                                                                        // workspace info based
                                                                                        // on project path.

WorkspaceInfo workspaceInfo = workspaceInfos.iterator().next();
Collection<String> serverFolderPaths = workspaceInfo
        .findServerPathsByLocalPath(VcsUtil.getFilePath(myProject.getBasePath()), false, myProject); // This
                                                                                                        // will
                                                                                                        // return$/TeamProjectCollection/TeamProject/ProjectFolder

ServerInfo serverInfo = workspaceInfo.getServer();
TfsSdkManager tfsSdkManager = TfsSdkManager.getInstance(); // intelliJ provide TfsSdkManager to get cached
                                                            // information about TFS, like credential
Credentials credentials = tfsSdkManager.getCredentials(serverInfo);
URI uri = new URI(serverInfo.getPresentableUri());

TFSTeamProjectCollection tpc = new TFSTeamProjectCollection(uri, credentials);
tpc.ensureAuthenticated();
VersionControlClient vcc;

vcc = forcePluginClassLoader(() -> tpc.getVersionControlClient()); // intelliJ has SOX issue if call is not
                                                                    // wrapped using forcePluginClassLoader()
                                                                    // method;
String teamProjectServerPath = "$/TeamProjectCollection"; // Tried to pass "$/TeamProjectCollection/TeamProject"
                                                            // as well but same result, getting emppty
//line: 15                                                          // collection of PlicyDefinition
PolicyDefinition[] results = vcc.getCheckinPoliciesForServerPaths(new String[] { teamProjectServerPath });

String serverProjectPath = serverFolderPaths.iterator().next();

// Another way to get policy
Item item = vcc.getItem("$/TeamProjectCollection");
TeamProject tp = new TeamProject(item, vcc);

String projectName = tp.getName();

//line: 20
PolicyDefinition[] ps = tp.getCheckinPolicies();
for (PolicyDefinition pd : ps) {
    int pi = pd.getPriority();

}

Line 15 & 20 both returns empty Policy definition.

Anyone faced this issue?

Community
  • 1
  • 1
Subodh
  • 13
  • 5
  • How do you config check in policy? Provide the detail steps here. – starian chen-MSFT Dec 18 '17 at 06:46
  • I config the policy in eclipse (Team explorer everywhere) and the code in intelliJ IDEA works fine. – starian chen-MSFT Dec 18 '17 at 08:14
  • My Configuration Manager has configured the check in policies using Visual Studio. We are applying custom path policies. I am not sure if Visual studio keep Visual Studio Policies and Team Explorer Everywhere policies separately. But when I debug the intelliJ plugin I hit debug points which gave me impression that hookups are working. But within the plugin I am trying to retrieve all policies as mentioned in code above. I am getting empty collection. Line 15 in my example above should return me collection. But it is not happening in my case. – Subodh Jan 29 '18 at 18:45
  • They are separately. What do you mean "hookups are working"? How is it working? – starian chen-MSFT Jan 30 '18 at 01:25
  • When I say hookups are working means intelliJ correctly searching for policy definition code and invoking the appropriate method. Inside those method I am creating TFS connection and trying to get the policy definition to confirm if it is applied in the folder where user is performing check in. Since Policy definition return empty I am not able to determine whether policy is define or not or if yest then which one. As you mentioned that they are separate. What do you mean by that? – Subodh Feb 01 '18 at 21:10
  • Adding checking policy through Visual Studio won't affect the policy in intellij or eclipse (Team explorer everywhere). – starian chen-MSFT Feb 02 '18 at 01:22
  • I have also notice that when I edit file from TFS web interface it does not care about chick-in policy applied on that path. But when I do the same using Visual Studio it works and through error if it does not satisfied. Do you have any example if I can do it using SOAP call? Not moving further with TFS Java SDK and also REST API. It would be great if you can point me some example which works using SOAP calls. – Subodh Feb 09 '18 at 20:41

0 Answers0