1 /*
  2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.skara.bots.submit;
 24 
 25 import org.openjdk.skara.bot.WorkItem;
 26 import org.openjdk.skara.forge.*;
 27 import org.openjdk.skara.vcs.Repository;
 28 
 29 import java.io.*;
 30 import java.nio.file.Path;
 31 import java.time.*;
 32 import java.util.logging.Logger;
 33 
 34 public class SubmitBotWorkItem implements WorkItem {
 35     private final SubmitBot bot;
 36     private final SubmitExecutor executor;
 37     private final PullRequest pr;
 38     private final Logger log = Logger.getLogger("org.openjdk.skara.bots.submit");
 39 
 40     SubmitBotWorkItem(SubmitBot bot, SubmitExecutor executor, PullRequest pr) {
 41         this.bot = bot;
 42         this.executor = executor;
 43         this.pr = pr;
 44     }
 45 
 46     @Override
 47     public String toString() {
 48         return "SubmitWorkItem@" + bot.repository().name() + "#" + pr.id() + ":" + executor.checkName();
 49     }
 50 
 51     @Override
 52     public boolean concurrentWith(WorkItem other) {
 53         if (!(other instanceof SubmitBotWorkItem)) {
 54             return true;
 55         }
 56         SubmitBotWorkItem otherItem = (SubmitBotWorkItem)other;
 57         if (!executor.checkName().equals(otherItem.executor.checkName())) {
 58             return true;
 59         }
 60         if (!pr.id().equals(otherItem.pr.id())) {
 61             return true;
 62         }
 63         if (!bot.repository().name().equals(otherItem.bot.repository().name())) {
 64             return true;
 65         }
 66         return false;
 67     }
 68 
 69     @Override
 70     public void run(Path scratchPath) {
 71         // Is the check already up to date?
 72         var checks = pr.checks(pr.headHash());
 73         if (checks.containsKey(executor.checkName())) {
 74             var check = checks.get(executor.checkName());
 75             if (check.startedAt().isBefore(ZonedDateTime.now().minus(executor.timeout())) && check.status() == CheckStatus.IN_PROGRESS) {
 76                 log.info("Check for hash " + pr.headHash() + " is too old - running again");
 77             } else {
 78                 log.fine("Hash " + pr.headHash() + " already has a check - skipping");
 79                 return;
 80             }
 81         }
 82 
 83         var prFolder = scratchPath.resolve("submit").resolve(pr.repository().name());
 84 
 85         // Materialize the PR's target ref
 86         try {
 87             var localRepo = Repository.materialize(prFolder, pr.repository().url(), pr.targetRef());
 88             var headHash = localRepo.fetch(pr.repository().url(), pr.headHash().hex());
 89 
 90             var checkBuilder = CheckBuilder.create(executor.checkName(), headHash);
 91             pr.createCheck(checkBuilder.build());
 92 
 93             var checkUpdater = new CheckUpdater(pr, checkBuilder);
 94             executor.run(prFolder, checkBuilder, checkUpdater);
 95             pr.updateCheck(checkBuilder.build());
 96         } catch (IOException e) {
 97             throw new UncheckedIOException(e);
 98         }
 99     }
100 }