LCOV - code coverage report
Current view: top level - httpd - GitReferenceUpdatedTracker.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 11 11 100.0 %
Date: 2022-11-19 15:00:39 Functions: 2 2 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2021 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.httpd;
      16             : 
      17             : import com.google.gerrit.extensions.events.GitReferenceUpdatedListener;
      18             : import com.google.gerrit.extensions.registration.DynamicItem;
      19             : import com.google.inject.Inject;
      20             : import com.google.inject.ProvisionException;
      21             : import com.google.inject.Singleton;
      22             : 
      23             : /**
      24             :  * Stores the updated refs whenever they are updated, so that we can export this information in the
      25             :  * response headers.
      26             :  *
      27             :  * <p>This is only working for HTTP requests. {@link WebSession} is not bound outside of HTTP
      28             :  * requests.
      29             :  */
      30             : @Singleton
      31             : public class GitReferenceUpdatedTracker implements GitReferenceUpdatedListener {
      32             : 
      33             :   private final DynamicItem<WebSession> webSession;
      34             : 
      35             :   @Inject
      36          99 :   GitReferenceUpdatedTracker(DynamicItem<WebSession> webSession) {
      37          99 :     this.webSession = webSession;
      38          99 :   }
      39             : 
      40             :   @Override
      41             :   public void onGitReferenceUpdated(GitReferenceUpdatedListener.Event event) {
      42          99 :     WebSession currentSession = null;
      43             :     try {
      44          24 :       currentSession = webSession.get();
      45          99 :     } catch (ProvisionException ex) {
      46             :       // We couldn't bind the current session properly. This is expected to happen at any point we
      47             :       // perform ref updates without an HTTP request (git push for example).
      48             :       // If we can't get a WebSession, we don't need to track the updated references.
      49          99 :       return;
      50          24 :     }
      51          24 :     if (currentSession != null) {
      52          24 :       currentSession.addRefUpdatedEvents(event);
      53             :     }
      54          24 :   }
      55             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750