A component naming convention advisor


Organizations sometimes would like to implement naming conventions for components based on the architecture for example. This post shows a simple example advisor that checks for a naming convention.

License

The post contains published code, so our lawyers reminded me to state that the code in this post is derived from examples from Jazz.net as well as the RTC SDK. The usage of code from that example source code is governed by this license. Therefore this code is governed by this license. I found a section relevant to source code at the and of the license. Please also remember, as stated in the disclaimer, that this code comes with the usual lack of promise or guarantee. Enjoy!

Just Starting With Extending RTC?

If you just get started with extending Rational Team Concert, or create API based automation, start with the post Learning To Fly: Getting Started with the RTC Java API’s and follow the linked resources.

You should be able to use the following code in this environment and get your own automation or extension working.

Compatibility

This code has been used with RTC 6.0 and is prepared to be used with RTC 6.0.x with no changes and it is pretty safe to assume, that the code will work with newer versions of RTC. It should run with any version that provides the operation ID.

The code in this post uses common and server libraries/services that are available in the RTC Server SDK.

Download

The code is included in the download in the post DIY stream naming convention advisors.

Solution

In the last few versions of RTC several operations have been made available for operational behavior. At least since RTC 5.0.2 the operation to modify a component is available with the operation ID com.ibm.team.scm.server.component. This allows to create advisors/preconditions as well as participants/follow up actions that operate on such events. An example shipped with the product is implemented in the class com.ibm.team.scm.service.UniqueComponentNameAdvisor.

There are examples shipped with the product in the SDK that you can look at for more sample code. For example: com.ibm.team.scm.service.internal.process.advisors.UniqueComponentNameAdvisor

The code below shows a very basic example how to test the component name for some simple naming schema.

The important information to take away is that the information about the save operation is provided in a special interface IComponentModificationData which allows access to the type of the operation, to old and new properties and to the component directly.

componentmodification

So it is possible to find out what operation is done on the component and based on that look at the properties that the component has.

The code below does exactly that. It checks what operation is going on and then takes the new name of the component and checks it.

/*******************************************************************************
 * Licensed Materials - Property of IBM
 * (c) Copyright IBM Corporation 2017. All Rights Reserved. 
 *
 * Note to U.S. Government Users Restricted Rights:  Use, duplication or 
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *******************************************************************************/
package com.ibm.js.scm.naming.advisor.service;

import org.eclipse.core.runtime.IProgressMonitor;

import com.ibm.team.process.common.IProcessConfigurationElement;
import com.ibm.team.process.common.advice.AdvisableOperation;
import com.ibm.team.process.common.advice.IAdvisorInfo;
import com.ibm.team.process.common.advice.IAdvisorInfoCollector;
import com.ibm.team.process.common.advice.runtime.IOperationAdvisor;
import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.scm.service.internal.AbstractScmService;
import com.ibm.team.scm.service.internal.process.IComponentModificationData;
import com.ibm.team.scm.service.internal.process.IComponentModificationData.OpType;

/**
 * An example advisor that checks the name of a component for some naming convention
 *
 * Also @see com.ibm.team.scm.service.UniqueComponentNameAdvisor for product example code
 */
@SuppressWarnings("restriction")
public class ComponentNamingAdvisor extends AbstractScmService implements
		IOperationAdvisor {
	public static final String REQUIRED_PREFIX = "TEST_";
	public static final String COMPONENT_NAMING_ADVISOR = "com.ibm.js.scm.naming.advisor.service.componentNaming";

	@Override
	public void run(AdvisableOperation operation,
			IProcessConfigurationElement advisorConfiguration,
			IAdvisorInfoCollector collector, IProgressMonitor monitor)
			throws TeamRepositoryException {

		IComponentModificationData data = (IComponentModificationData) operation
				.getOperationData();
		if (data == null) {
			throw new TeamRepositoryException("Missing component data"); //$NON-NLS-1$
		}

		String compName;

		if (data.getOpType() == OpType.CREATE) {
			compName = data.getNewName();
		} else if (data.getOpType() == OpType.RENAME) {
			compName = data.getNewName();
		} else {
			return;
		}
		if (validateName(compName)) {
			// Nothing to do
			return;
		}
		String description = "The component name violates the naming conventions component name must have prefix '"
				+ REQUIRED_PREFIX + "'!";
		String summary = description;
		IAdvisorInfo info = collector.createProblemInfo(summary, description,
				COMPONENT_NAMING_ADVISOR);//$NON-NLS-1$
		collector.addInfo(info);
	}

	/**
	 * Validate the name against the convention
	 * 
	 * @param compName
	 * @return
	 */
	private boolean validateName(String compName) {
		// Implement your own naming convention here
		if (compName.startsWith(REQUIRED_PREFIX)) {
			return true;
		}
		return false;
	}
}

Summary

This is as usual a very basic example with no or very limited testing and error handling. See the links in the getting started section for more examples. As always I hope this helps someone out there with running RTC.

Advertisements

About rsjazz

Hi, my name is Ralph. I work for IBM and help colleagues and customers with adopting the Jazz technologies.
This entry was posted in Jazz, RTC, RTC Automation, RTC Extensibility, RTC Process Customization and tagged , , , , , . Bookmark the permalink.

12 Responses to A component naming convention advisor

  1. Dexter, Michael says:

    Ralph,

    Do you have similar code to test Stream name?

    Regards,

    Michael

  2. Krasimir Malchev says:

    Hello, Ralf.
    If I change the name of a component, which owner is my team / project area (where is this precondition enabled), then the advisor is enabled.
    However if the component owner is a user (contributor), then the advisor is not executed.
    Is there a way to enable the advisor for components which owner is a contributor, but not a process area?

    • rsjazz says:

      Hi Krasimir,

      the owner of the component/the context where the advisor would work should be the “Owned Buy” attribute you see when you open the component in the Eclipse client.

      For privately owned components, I don’t think you will be able to run the advisor. You can however prevent a user from changing the ownership to a project area while violating the rule if you check the name under that condition (owner change), I suppose.

      • Krasimir Malchev says:

        Hello, Ralf.
        Thanks for your fast reply!

        I made some more tests. In fact the advisor is not called if the RTC user changes the owner of the component to a contributor.
        That is, I am not able to further verify either the owner (incl. component visibility) or the component name.

      • rsjazz says:

        But no one except the owner would see the component then, would they? Otherwise you could check the components on a stream when the stream is saved as well.

        The problem with privately owned items such as repo workspaces and components is that the context they live in is really the user and not a project area. You can only protect operations in the project/team area content.
        So you would have to check in that context e.g. if a component is now saved as owned by the team area or other similar conditions.

      • Krasimir Malchev says:

        Yes, you are right.
        In fact the context of the problem is to prevent privately owned components to be delivered to streams (common space for the development streams).
        If that happens then I have a requirement to automatically change the component ownership to a common team area for all teams. This turns to be challenge in the described use case.

      • rsjazz says:

        Hi Krasimir,

        I have not looked at your use case. The key will be to find the governing operation e.g. Save Stream or delivery etc., I think The owner of the stream will be a project area, so the context is then governed by the process. It is just the stuff that is privately owned that you can’t control.

      • Krasimir Malchev says:

        Hello, Ralf.
        The use case is that if a component is part of (added to) any stream, its ownership has to be anything but not a contributor (i.e. privately owned). What is more, users should not be able to change the component ownership to a contributor while it resides in a stream. Though if the component is removed from the context of a stream then its owner can be changed.

        I have covered the following operations:
        – Save stream (server) with corresponding operation id com.ibm.team.scm.server.modifyStream
        Adding components with privately owned components is controlled
        – Deliver (server) with corresponding operation id com.ibm.team.scm.server.deliver
        Delivering code of privately owned components is controlled
        – Modify Component (server) with corresponding operation id com.ibm.team.scm.server.component
        Changing the component from process area owner is controlled.
        The only thing I am not able to process is changing the owner from a contributor to anything as the corresponding advisor/participant is not called.
        After our discussion I see that RTC does not allow it.

        Thank you for your help!

      • rsjazz says:

        I am wondering… If the owner is changed to anything else than a contributor, I would assume the change to surface in calling advisors in that context.

        However, I don’t have the deep involvement you have at this point.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s