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.
Community moderators have prevented the ability to post new answers.
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):
<dependency> <groupId>com.atlassian.bamboo.plugins</groupId> <artifactId>atlassian-bamboo-plugin-git</artifactId> <version>3.0.25</version> <scope>provided</scope> </dependency>
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
Thanks, I'm going to try this tonight!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Make sure you have
<scope>provided</scope>
in your pom.xml for the bamboo-git-plugin, as per my example:
<dependency> <groupId>com.atlassian.bamboo.plugins</groupId> <artifactId>atlassian-bamboo-plugin-git</artifactId> <version>3.0.25</version> <scope>provided</scope> </dependency>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.