VCS user in notifications

Hi,

when writing a notification plugin, is it possible to get hold of the last user who committed changes to the current build?

I have tried the following:

public void notifyBuildStarted(SRunningBuild sRunningBuild, Set<SUser> sUsers) {

   String user = ((User)sRunningBuild.getCommitters(SelectPrevBuildPolicy.SINCE_LAST_BUILD).getUsers().toArray()[0]).getName();
   ...


But it doesn't seem to work.
I have also tried:

sRunningBuild.getTriggeredBy().getUser().getName();

but this returns a teamcity user only if a build is forced.

We use teamcity 4.5 and subversion

Kind regards,

Rob

P.S.

I assume the sRunningBuild.getBuildComment() returns a teamcity comment rather than a VCS comment. are VCS comments possible to get hold of?

2 comments
Comment actions Permalink

You can do the following:
Obtain changes contained in this build:
SRunningBuild::getContainingChanges()

Note that in this list the last change detected by TeamCity goes first (this is default sorting for collections with changes).

Then take first element (SVcsModification) and with help of VcsManager obtain TeamCity users related to this change: VcsManager::getModificationUsers(SVcsModification)
Note that ther can be more than one person, because matching is performed by VCS usernames provided by users themselves and if two users have same VCS usernames then both will be returned.

Or if you do not need TeamCity users and committer username is just enough you can simply take it from SVcsModification: SVcsModification::getUserName()

0
Comment actions Permalink

Many thanks for that.

Your assistance helped me to write a function to get the user or to fall back to the VCS username if not found.

I share it here, please forgive the excessive dirty try...catch blocks

private String getUser(Build build) {
        String user = "";
        
        try {
            if (build instanceof SBuild) {
                SBuild sBuild = (SBuild)build;
                if (sBuild.getTriggeredBy().isTriggeredByUser()) {
                    user = sBuild.getTriggeredBy().getUser().getName();
                }
            }
        }
        catch(Exception e) {
        }

        if (user == null || user.equals("")) {
            try {
                user = build.getCommitters(SelectPrevBuildPolicy.SINCE_LAST_BUILD).getUsers().iterator().next().getName();
            }
            catch(Exception e) {
            }
        }

        if (user == null || user.equals("")) {
            try {
                user = build.getContainingChanges().get(0).getUserName();
            }
            catch(Exception e) {
            }
        }

        return user;
    }

0

Please sign in to leave a comment.