package com.airbnb.chancery;

import com.airbnb.chancery.model.CallbackPayload;
import com.airbnb.chancery.model.Repository;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotNull;

/* TODO: some metrics */
@Slf4j
public class RefLogger {
    private final RefFilter refFilter;
    private final PayloadExpressionEvaluator refTemplate;
    private final GithubClient ghClient;

    public RefLogger(RefLoggerConfig config, GithubClient ghClient) {
        this.ghClient = ghClient;
        refFilter = new RefFilter(config.getRefFilter());
        refTemplate = new PayloadExpressionEvaluator(config.getRefTemplate());
    }

    @Subscribe
    @AllowConcurrentEvents
    private void receiveCallback(@NotNull CallbackPayload payload) throws GithubFailure.forRateLimit {
        if (!refFilter.matches(payload))
            return;

        if (payload.isDeleted())
            return;

        final String ref = refTemplate.evaluateForPayload(payload);
        final Repository repo = payload.getRepository();
        final String hash = payload.getAfter();
        final String owner = repo.getOwner().getName();
        final String repoName = repo.getName();

        log.info("Creating ref {} to {} in in {}/{}", ref, hash, owner, repoName);
        ghClient.createReference(owner, repoName, ref, hash);
    }
}

@@ -792,22 +792,21 @@
 ts%0A    p
-rivate
+ublic
  void re
@@ -852,16 +852,28 @@
 payload)
+%0A           
  throws 
@@ -894,16 +894,24 @@
 forR
-ateLimit
+eferenceCreation
  %7B%0A 
@@ -1342,19 +1342,16 @@
 %7B%7D to %7B%7D
- in
  in %7B%7D/%7B

fix typos