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.notify;
24 
25 import org.openjdk.skara.forge.HostedRepository;
26 import org.openjdk.skara.vcs.*;
27 
28 import java.io.*;
29 import java.time.format.DateTimeFormatter;
30 
31 class CommitFormatters {
32     static String commitToTextBrief(HostedRepository repository, Commit commit) {
33         var writer = new StringWriter();
34         var printer = new PrintWriter(writer);
35 
36         printer.println("Changeset: " + commit.hash().abbreviate());
37         printer.println("Author:    " + commit.author().name() + " <" + commit.author().email() + ">");
38         if (!commit.author().equals(commit.committer())) {
39             printer.println("Committer: " + commit.committer().name() + " <" + commit.committer().email() + ">");
40         }
41         printer.println("Date:      " + commit.date().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss +0000")));
42         printer.println("URL:       " + repository.webUrl(commit.hash()));
43 
44         return writer.toString();
45     }
46 
47     private static String patchToText(Patch patch) {
48         if (patch.status().isAdded()) {
49             return "+ " + patch.target().path().orElseThrow();
50         } else if (patch.status().isDeleted()) {
51             return "- " + patch.source().path().orElseThrow();
52         } else if (patch.status().isModified()) {
53             return "! " + patch.target().path().orElseThrow();
54         } else {
55             return "= " + patch.target().path().orElseThrow();
56         }
57     }
58 
59     static String commitToText(HostedRepository repository, Commit commit) {
60         var writer = new StringWriter();
61         var printer = new PrintWriter(writer);
62 
63         printer.print(commitToTextBrief(repository, commit));
64         printer.println();
65         printer.println(String.join("\n", commit.message()));
66         printer.println();
67 
68         for (var diff : commit.parentDiffs()) {
69             for (var patch : diff.patches()) {
70                 printer.println(patchToText(patch));
71             }
72         }
73 
74         return writer.toString();
75     }
76 }