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

How to retrieve logged-in UserId in Pre-Receive Hook plugin

P December 3, 2015

Hi, we are trying to build a custom pre-hook and as part of this pre-hook, we need to retrieve the logged-in UserId.
We tried to update the atlassain-plugin.xml under plugin_project/src/test/resource.

Tried to build the hook using this api but fails in the process of build time.. meaning need input on the correct way to reference this API.

<component-import key="authenticationContext" interface="com.atlassian.stash.user.StashAuthenticationContext"/>

 

Below is the code snippet in the pre-hook where it fails on build-time/compile time

public class  MyPreReceiveRepositoryHook implements  PreReceiveRepositoryHook

{

private  StashAuthenticationContext stashAuthenticationContext=null;

public MyPreReceiveRepositoryHook(StashAuthenticationContext stashAuthenticationContext) {

        this.stashAuthenticationContext = stashAuthenticationContext;

    }

 

@Override

public  boolean  onReceive(RepositoryHookContext context, Collection<RefChange> refChanges, HookResponse hookResponse)

    {

com.atlassian.stash.user.StashUser currentUser = stashAuthenticationContext.getCurrentUser();

    if( null != currentUser )

    {

    System.out.println(" StashAuthenticationContext uid--- " + currentUser.getDisplayName() );

    }

    else
    {

System.out.println(" StashAuthenticationContext API call failed :: StashAuthenticationContext ");

    }

 

-Thanks.

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
P December 16, 2015

Bryan,  .. dont see any diff when comparing with what you shared .. still does not show up on the stash gui when i include the references to the StashAuthentication packages..

I have uploaded my version of code at https://bitbucket.org/pp4253/test-prehookplugin/src/ecec8bdd35d1?at=master

Do you see anything obvious that i may be missing ?

-Thanks

 

Bryan Turner
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 16, 2015

Parvinder, There are several differences between the your plugin and mine. The important one, though, is that mine is a simple "transformed" plugin and yours is...halfway between "transformed" and "transformerless". Let's get into it a little. - "transformed" means Stash processes your plugin for you, during startup, and makes various changes to your jar file so that it will be a valid OSGi bundle - "transformerless" means _you_ have done all that work yourself, and Stash should be able to simply use your plugin as-is In your pom.xml, you've got an Atlassian-Plugin-Key defined in the instructions for the maven-stash-plugin. That means your plugin is "transformerless". That means directives like "component-import" in atlassian-plugin.xml _will not work_. But that's how you're trying to get access to StashAuthenticationContext. That's why your plugin fails when you try to install it; "component-import" in atlassian-plugin.xml only works in "transformed" plugins. Compare that to the MyPluginComponentImpl that was generated by the Atlassian SDK. There's no "component-import" in atlassian-plugin.xml for ApplicationProperties. Instead, the field is annotated is @ComponentImport. That's picked up by the atlassian-spring-scanner-maven-plugin defined in your pom.xml and is part of building a "transformerless" plugin. The <atlassian-scanner:scan-indexes/> directive in your plugin-context.xml picks up the output of the atlassian-spring-scanner-maven-plugin at runtime. So, how to fix your hook. You need to do two things: * Annotate MyPreReceiveRepositoryHook with @Scanned (at the class level) * Annotate MyPreReceiveRepositoryHook.authenticationContext with @ComponentImport (at the field level) With those two changes, your plugin should install and you should see your hook in the "Hooks" section of repository settings. Best regards, Bryan Turner Atlassian Bitbucket

P December 19, 2015

Bryan, you duh Man!! .. That was it.. Awesome clarification.. .. Thank you.

0 votes
P December 18, 2015

Bryan, you Duh Man!! .. That was it.. awesome clarification.. -Thanks

0 votes
P December 4, 2015

Hi Bryan ..

I am following the tutorial listed below .. https://developer.atlassian.com/blog/2015/01/beer-o-clock-stash-plugin-tutorial/

It seems to be 3.1.1 Stash .. , where do I make the changes in this case ?

-Thanks.

Bryan Turner
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 4, 2015

Parvinder, Are you using Maven? How familiar are you with Java development? Best regards, Bryan Turner Atlassian Bitbucket

P December 7, 2015

No,I am not using Maven outside this tutorial. I am familiar with Java development. The steps in the video tutorial is exactly what I have followed so far .. Tried updating the pom.xml of the plugin project which gets created when I follow the tutorial .. but no success so far Also, fyi, I am not able to comment more than 1 time .. so far.

P December 8, 2015

Hi Bryan, any more inputs ? BTW, the goal is to figure a way to retrieve the userId of the logged in user who is executing a "Remote code push" on the remote Bitbucket.. Hopefully in the Pre-hook code function "onReceive()".

Bryan Turner
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 11, 2015

Parvinder, Apologies for the delay in my response; I've been ill. Now that I'm back, rather than waste a lot of your time with back-and-forth comments here, I've created a simple plugin based on your example code. My plugin is available on Bitbucket at https://bitbucket.org/bpturner/bitbucket-server-example-hook. The repository has 2 branches, "bitbucket" and "stash". Each branch contains an _identical_ plugin, functionality-wise, with the correct build environment, dependencies and imports for Bitbucket Server and Stash, respectively. You're welcome to clone my repository and use it, fork it and modify it, or whatever approach to implementing your own plugin works for you. (I'd suggest changing the groupId and artifactId, as well as the organization and SCM details, though.) My hope is that you can compare the source for my plugin, which I've tested in Stash 3.11.5 and Bitbucket Server 4.2.0, and compare it to what you have locally. From the comparison, I'm hoping you'll be able to see what you were missing to get your own plugin to build. One note: Your comment in your question's source says "StashAuthenticationContext API call failed" when the current user is returned null. That's not accurate. An exception would indicate the API call failed. Returning null means there's no current user--or, in other words, that the request is anonymous. Since all push operations require write permission, which cannot be granted anonymously (only read permission can be granted to anonymous users), your PreReceiveHook should never see null. I just wanted to clarify that null does not indicate a failure. Best regards, Bryan Turner Atlassian Bitbucket

P December 14, 2015

Much appreciated .. When I am trying to clone the Stash branch ,am getting the below error C:\Applications\testBryan>git clone https://pp4253@bitbucket.org/bpturner/bitbucket-server-example-hook.git Cloning into 'bitbucket-server-example-hook'... fatal: unable to access 'https://pp4253@bitbucket.org/bpturner/bitbucket-server-example-hook.git/';: Failed to connect to bitbucket.org port 443: Timeout

Bryan Turner
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 14, 2015

Parvinder, I don't have any issues cloning it over HTTPS, so I'm not sure what advice I can offer. Try taking your username out of the URL, like this: C:\Temp>git clone https://bitbucket.org/bpturner/bitbucket-server-example-hook.git Cloning into 'bitbucket-server-example-hook'... remote: Counting objects: 31, done. remote: Compressing objects: 100% (17/17), done. remote: Total 31 (delta 4), reused 0 (delta 0) Unpacking objects: 100% (31/31), done. Checking connectivity... done. Best regards, Bryan Turner Atlassian Bitbucket

P December 15, 2015

Bryan, I was able to get the clone working .. question - the url seems to point to Bitbucket code, maybe I am missing but when I change the Branch destination to Stash, I still see Bitbucket code branch ? I will need the Stash version of this plugin. -Thanks

Bryan Turner
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 15, 2015

Parvinder, Given you didn't provide the commands you're trying to run, I can't say why they're not working. The "stash" branch is present in my repository and it has the Stash version of the code. Try: git clone -b stash https://bitbucket.org/bpturner/bitbucket-server-example-hook.git example That "-b stash" in there will make it clone the "stash" branch instead of the "bitbucket" branch (which is the default). Alternatively, in your current clone, you can run "git checkout -b stash origin/stash". Either of these should get you the correct source. If they don't, I'm going to need to see the commands you're running, and some of the pom.xml so I can verify it's actually still on the Bitbucket Server code. Best regards, Bryan Turner Atlassian Bitbucket

0 votes
Bryan Turner
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 3, 2015

Parvinder,

To use StashAuthenticationContext you need to have the right Maven dependencies (I'll assume you're using Maven to build). The dependency for that looks like this:

&lt;dependency&gt;
    &lt;groupId&gt;com.atlassian.stash&lt;/groupId&gt;
    &lt;artifactId&gt;stash-api&lt;/artifactId&gt;
    &lt;version&gt;3.11.5&lt;/version&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;

That said, such a plugin will only work in Stash 3.11 and prior. In the 4.0 major release the product was renamed to Bitbucket Server and all of its APIs were updated accordingly. In Bitbucket Server 4.0 and higher, StashAuthenticationContext is now simply AuthenticationContext (and StashUser is now ApplicationUser), and you'll need to add the following dependency:

&lt;dependency&gt;
    &lt;groupId&gt;com.atlassian.bitbucket.server&lt;/groupId&gt;
    &lt;artifactId&gt;bitbucket-api&lt;/artifactId&gt;
    &lt;version&gt;4.1.1&lt;/version&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;

To build a plugin for Bitbucket Server, you must use Java 8. Stash plugins can be built using Java 7.

If this doesn't help, please provide more exact information about the error you're getting and the mechanism you're using to try and build the plugin.

Best regards,
Bryan Turner
Atlassian Bitbucket

Bryan Turner
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 11, 2015

An example plugin with the correct Maven dependencies can be found at: https://bitbucket.org/bpturner/bitbucket-server-example-hook

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events