Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to get the git URL from within a bamboo plugin?

Ken Wood
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 5, 2013

I have a plugin with the code:

for (RepositoryDefinition repoDef : taskContext.getBuildContext().getRepositoryDefinitions())
{
Repository repo = repoDef.getRepository();
if (repo instanceof SvnRepository)
{
SvnRepository svn = (SvnRepository)repo;
return svn.getLocationIdentifier();
}

We now are also using 'git', so I want to add to that code the equivalent code to fetch the git URL, but can't seem to find the right documentation and recipe.

I tried looking at the source code for the bamboo-git plugin, and found this class defined, so I added an import:

import com.atlassian.bamboo.plugins.git;

And, I added to my pom.xml:

<dependency>
<groupId>com.atlassian.bamboo.plugins</groupId>
<artifactId>atlassian-bamboo-plugin-git</artifactId>
<version>3.0.15</version>
<scope>provided</scope>

But, I get this message:

[WARNING] POM for 'com.atlassian.bamboo.plugins:atlassian-bamboo-plugin-git:pom:3.0.15:compile' is invalid.

Its dependencies (if any) will NOT be available to the current build.

And the code won't compile:

[INFO] Compilation failure

/kwork/wkcopy/e2open_tasks/src/main/java/com/prcpo/bamboo/plugins/GetConfigTask.java:[11,35] cannot find symbol
symbol : class git
location: package com.atlassian.bamboo.plugins

Suggesions on information that will help me write the equivalent code for fetching the git URL is appreciated.

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 11, 2013

EDIT: I found a better solution and have updated my answer.

Aseem's answer is correct... or, it would be correct if the Bamboo Git Plugin implemented the Repository interface correctly.

Unfortunately, it doesn't. The text "Repository location information not supplied by plugin" is displayed when the repository implementation does not provide its location information to the Repository API.

I had a look at the implementation of the GitRepository class to see if there was another way to get at the location (since it must know this location in order to be able to access the source!). For reference, the bamboo-git-plugin is open source and available for viewing on Bitbucket: https://bitbucket.org/atlassian/bamboo-git-plugin/overview

To get at the repository URL using this workaround, you'll need this entry in your pom.xml (in the dependencies section):

&lt;dependency&gt;
	&lt;groupId&gt;com.atlassian.bamboo.plugins&lt;/groupId&gt;
	&lt;artifactId&gt;atlassian-bamboo-plugin-git&lt;/artifactId&gt;
	&lt;version&gt;3.0.25&lt;/version&gt;
	&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;

Then, in your task, you need to call the getAccessData() method on the repository, like this:

package com.atlassian.bamboo.test;

import com.atlassian.bamboo.build.logger.BuildLogger;
import com.atlassian.bamboo.plugins.git.GitRepository;
import com.atlassian.bamboo.repository.Repository;
import com.atlassian.bamboo.repository.RepositoryDefinition;
import com.atlassian.bamboo.task.*;
import org.jetbrains.annotations.NotNull;

/**
 * Tests using the Bamboo Git plugin API
 */
public class GitRepoTask implements TaskType
{
    @NotNull
    @Override
    public TaskResult execute(@NotNull TaskContext taskContext) throws TaskException
    {
        BuildLogger buildLogger = taskContext.getBuildLogger();

        for (RepositoryDefinition repoDef : taskContext.getBuildContext().getRepositoryDefinitions())
        {
            Repository repo = repoDef.getRepository();

            if (repo instanceof GitRepository)
            {
                GitRepository grepo = (GitRepository)repo;
                buildLogger.addBuildLogEntry(grepo.getAccessData().getRepositoryUrl());
}
            else
            {
                buildLogger.addBuildLogEntry("Repo is not a git repository");
            }
        }

        return TaskResultBuilder.newBuilder(taskContext).success().build();
    }
}

Which in turn gives a log output something like this:

simple	11-Dec-2013 13:54:41	Starting task 'Test' of type 'com.atlassian.bamboo.test.test-git-plugin:git-repo-task'
simple	11-Dec-2013 13:54:44	https://bitbucket.org/atlassianlabs/bamboo-continuous-plugin-deployment.git

Ken Wood
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 11, 2013

Thanks, I'm going to try this tonight!

Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 11, 2013

Any luck? :-)

Ken Wood
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 12, 2013

Been in the debugger fighting this:

java.lang.ClassCastException: com.atlassian.bamboo.plugins.git.GitRepository cannot be cast to com.atlassian.bamboo.plugins.git.GitRepository

which appears to be related two loading a class with one classloader and trying to cast it to the same class loaded with a different classloader...

Stuck for now.

Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 12, 2013

Make sure you have

&lt;scope&gt;provided&lt;/scope&gt;

in your pom.xml for the bamboo-git-plugin, as per my example:

&lt;dependency&gt;
    &lt;groupId&gt;com.atlassian.bamboo.plugins&lt;/groupId&gt;
    &lt;artifactId&gt;atlassian-bamboo-plugin-git&lt;/artifactId&gt;
    &lt;version&gt;3.0.25&lt;/version&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;

Ken Wood
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 12, 2013

Yep, I had that, but still failed. Must have been old crud lying around because I wiped out the 'target' directory and did a fresh build, now it works:

13-Dec-2013 12:34:13 In GetPlanRepositoryUrl
13-Dec-2013 12:34:13 com.atlassian.bamboo.plugins.git.GitRepository
13-Dec-2013 12:34:13 Inside the GitRepository instanceof clause

13-Dec-2013 12:34:13 http://dev298.dev.e2open.com:7990/scm/e2a/e2a.git<br< a="">> 13-Dec-2013 12:34:13 Identified repository http://dev298.dev.e2open.com:7990/scm/e2a/e2a.git

0 votes
Ken Wood
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 9, 2013

Yes, I changed the import statement, and I removed the <scope> clause from the POM, and while it compiles, it throws a runtime error. It appears that the API in bamboo 4.4.5 doesn't support everything.

I changed the POM to use Bamboo 5.2.2, and now no runtime error, and the log shows I'm getting an instance of GitRepository, but it's not returning a URL.

0 votes
Aseem Parikh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 9, 2013

It seems like you're headed in the right direction. I haven't developed a Bamboo plugin yet, but your compilation error makes sense. "com.atlassian.bamboo.plugins.git" is a package, not a class. If you change that import line to the following...

import com.atlassian.bamboo.plugins.git.*;

...then compilation should succeed.

I'm not sure what's going on with the Maven issue. The </dependency> tag seems to be missing, for starters, but I imagine that was simply a copy and paste error. If it's not there, then add it :) If you still get the same error, try a different version of the artifact -- preferably one that matches what Bamboo is using.

Once you get that working, you should be able to make use of the GitRepository class in the package mentioned above and add the corresponding code:

...
else if (repo instanceof GitRepository)
{
    GitRepository git = (GitRepository)repo;
    return git.getLocationIdentifier();
}

Hope that's enough for you to work with.

Ken Wood
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 9, 2013

Yes, I changed the import statement, and I removed the <scope> clause from the POM, and while it compiles, it throws a runtime error. It appears that the API in bamboo 4.4.5 doesn't support everything.

I changed the POM to use Bamboo 5.2.2, and now no runtime error, and the log shows I'm getting an instance of GitRepository, but it's not returning a URL.

Ken Wood
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 9, 2013

Looks like the API in Bamboo 5.2.2 still doesn’t work

Here’s the code:

private String GetPlanRepositoryUrl(){

for (RepositoryDefinition repoDef : taskContext.getBuildContext().getRepositoryDefinitions())

{

buildLogger.addErrorLogEntry("In GetPlanRepositoryUrl");

Repository repo = repoDef.getRepository();

buildLogger.addErrorLogEntry(repo.getClass().getName());

// here is where the API lets me down:

buildLogger.addErrorLogEntry(repo.getLocationIdentifier());

if (repo instanceof SvnRepository)

{

SvnRepository svn = (SvnRepository)repo;

return svn.getLocationIdentifier();

}

if (repo instanceof GitRepository)

{

GitRepository git = (GitRepository)repo;

return git.getLocationIdentifier();

}

}

buildLogger.addErrorLogEntry("No SVN or GIT repository found in plan");

return "";

}

Here’s what I see in the output:

error 10-Dec-2013 10:29:11 In GetPlanRepositoryUrl

error 10-Dec-2013 10:29:11 com.atlassian.bamboo.plugins.git.GitRepository

error 10-Dec-2013 10:29:11 Repository location information not supplied by plugin

error 10-Dec-2013 10:29:11 No SVN or GIT repository found in plan

Aseem Parikh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 9, 2013

That seems like it should work fine.

When you edit the source repositories for the plan, did you make sure to select "Git" as the source repo type? If you're using github or bitbucket as the type, your approach might need some tweaking.

Ken Wood
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 9, 2013

In bamboo in the repository definition the repository is of type 'git', and the url is http://dev298.dev.e2open.com:7990/scm/e2a/e2a.git

Still seems to me something is missing as the result of asking for the git repo's url is "Repository location information not supplied by plugin"

TAGS
AUG Leaders

Atlassian Community Events