How do I use Bitbucket AuditService in an v2 add-on?

When including AuditService into atlassian-plugin.xml

  <component-import key="auditService" interface="com.atlassian.bitbucket.audit.AuditService" />

And adding the corresponding OSGi package to pom.xml with

    <build>
        <plugins>
            <plugin>
                <groupId>com.atlassian.maven.plugins</groupId>
                <artifactId>bitbucket-maven-plugin</artifactId>
                <version>${amps.version}</version>
                <extensions>true</extensions>
                <configuration>
....
                    <instructions>
                        <DynamicImport-Package>
                            com.atlassian.upm.api.license.entity;version="2.0.1",
                            com.atlassian.upm.api.license;version="2.0.1", com.atlassian.upm.api.util;version="2.0.1",
                            com.atlassian.bitbucket.audit; resolution:=optional
                        </DynamicImport-Package>
                    </instructions>

The plugin does not start with

[INFO] 2019-01-18 08:31:40,648 ERROR [spring-startup]  c.a.plugin.osgi.factory.OsgiPlugin Plugin 'com.izymes.workzone' never resolved service '&auditService' with filter '(objectClass=com.atlassian.bitbucket.audit.AuditService)'

com.atlassian.bitbucket.audit.AuditService is part of the bitbucket-api jar (along with all the other public components) and this dependency is included in the app’s pom.xml

Not including the package into DynamicImport-Package doesn’t help either …

1 Like

Hi @izymesdev,

Have you found a solution for AuditService yet? I’m having the same issue trying to use AuditService in a Bitbucket plugin:

ERROR [spring-startup]  c.a.plugin.osgi.factory.OsgiPlugin Plugin 'packagename' never resolved service '&auditService' with filter '(&(objectClass=com.atlassian.bitbucket.audit
.AuditService)(objectClass=com.atlassian.bitbucket.audit.AuditService))'

The service is imported like this (shortened):

import com.atlassian.bitbucket.audit.AuditService;

@ExportAsService{MyService.class}
@Named("myService")
public class MyService {
  @ComponentImport
  private final AuditService auditService;

  @Inject
  public MyService(final AuditService auditService) {
    this.auditService = auditService
  } 
}

I haven’t solved the AuditService problem - however my original goal was to add add-on config events to the audit log.

You can do this by adding your own event classes that extend ApplicationEvent and are annotated with @Audited like

@Audited(converter = WorkzoneConfigChangedAuditEventConverter.class, priority = Priority.HIGH)
public class WorkzoneConfigChangedAuditEvent extends ApplicationEvent {
..

Where the converter looks like

public class WorkzoneConfigChangedAuditEventConverter implements AuditEntryConverter<WorkzoneConfigChangedAuditEvent> {

    @Nonnull
    @Override
    public AuditEntry convert(@Nonnull WorkzoneConfigChangedAuditEvent event, AuditEntryBuilder builder) {
...

Finally use the EventPublisher component to publish the audit event.

See https://confluence.atlassian.com/bitbucketserver/audit-logging-in-bitbucket-server-776640417.html on how to configure audit logging.

1 Like

Thank you very much for your response!

Unfortunately this does not fit my needs, because I want to fetch the project creation dates from the audit log (as there is no other way in the api to access these, only by querying directly on the database).

It looks like this was broken in Bitbucket Server 4.5.0 (April 2016), and was just not noticed for ~2 1/2 years. I’ve created BSERV-11993 to track a fix. While the class for the interface is OSGi-exported, the actual service is not. So apps can classload the AuditService interface, but any component import for it will never resolve.

@izymesdev, I’m glad you were able to come up with a satisfactory workaround. Apologies for not noticing this question sooner!

@sezu, just FYI, I don’t think querying the audit logs for project creation dates will be reliable, long term. If your app needs to track project creation, you’d be better off listening for ProjectCreatedEvent and implementing your own recording for the date. That won’t help with existing projects, unfortunately, but the audit log would be at best a partial solution anyway.

Best regards,
Bryan Turner
Atlassian Bitbucket

3 Likes

Readers may consider updates to the platform wide audit features detailed in New auditing features coming to Server/Data Center - What you need to know

2 Likes