UnifiedTicketingUtil.java 10.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package de.hftstuttgart.utils;

import de.hftstuttgart.models.ModocotResult;
import de.hftstuttgart.models.ModocotResultSummary;
import de.hftstuttgart.unifiedticketing.core.Filter;
import de.hftstuttgart.unifiedticketing.core.Ticket;
import de.hftstuttgart.unifiedticketing.core.TicketBuilder;
import de.hftstuttgart.unifiedticketing.core.TicketSystem;
import de.hftstuttgart.unifiedticketing.exceptions.UnifiedticketingException;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class UnifiedTicketingUtil {

    private final static Logger LOG = LogManager.getLogger(UnifiedTicketingUtil.class);

    private final static String MODOCOT_LABEL = "MoDoCoT created";
    private final static String MODOCOT_TITLE = " | " + MODOCOT_LABEL;

    public static String createTicketDescriptionFromResult(TicketSystem ts, ModocotResult result, boolean compilationError) {
        StringBuilder sb = new StringBuilder();

        // heading
        if (compilationError) {
            sb.append(String.format("# %s is not compilable", result.name));
        } else {
            sb.append(String.format("# Unittest for %s fails", result.name));
        }

        sb.append("\n\n");

        // meta data table
        sb.append("|||\n");
        sb.append("|:-|:-|\n");

        if (compilationError) {
            sb.append(String.format("|File|`%s`|\n", result.name));
        } else {
            sb.append(String.format("|Test|`%s`|\n", result.name));
        }
        sb.append(String.format("|Reason|`%s`|\n", result.failureReason));

        if (compilationError) {
            sb.append(String.format("|Line|`%s`|\n", result.lineNumber));
            sb.append(String.format("|Column|`%s`|\n", result.columnNumber));
            sb.append(String.format("|Position|`%s`|\n", result.position));
        } else {
            sb.append(String.format("|Type|`%s`|\n", result.failureType));
        }

        sb.append("\n\n");

        // stacktrace
        sb.append("<details>\n\n");
        sb.append("<summary>show stacktrace</summary>\n\n");
        sb.append("```");
        sb.append(result.stacktrace);
        sb.append("\n```");
        sb.append("\n\n</details>\n");

        return sb.toString();
    }

    public static String createTicketTitleFromResult(TicketSystem ts, ModocotResult result, boolean compilationError) {
        StringBuilder sb = new StringBuilder();
        String separator = " | ";

        if (compilationError) sb.append("compilation fails");
        else sb.append("test method fails");
        sb.append(separator);

        sb.append(result.name);

        if (!compilationError) {
            sb.append(separator);
            sb.append(result.failureReason);
        }

        // if label-support is not present, place global identifier into title
        if (!ts.hasLabelSupport()) sb.append(MODOCOT_TITLE);

        sb.append(separator);
        sb.append(getHashForFailure(result));

        return sb.toString();
    }

    public static Ticket createTicketFromResult(TicketSystem ts, ModocotResult result, boolean compilationError) {
        TicketBuilder tb = ts.createTicket()
            .title(createTicketTitleFromResult(ts, result, compilationError))
            .description(createTicketDescriptionFromResult(ts, result, compilationError));

        if (ts.hasLabelSupport()) tb.labels(Collections.singleton(MODOCOT_LABEL));

        return tb.create();
    }

    public static Set<Ticket> fetchExistingModocotTickets(TicketSystem ts) {
        Set<Ticket> ret = new HashSet<>();
        Filter f = ts.find();

        // depending on label support, identify MoDoCoT tickets by label or title containing string
        if (ts.hasLabelSupport()) {
            LOG.debug(String.format(
                "ticketsystem has label support, using label %s to find modocot tickets", MODOCOT_LABEL));
            f.withLabel(MODOCOT_LABEL);
        }
        else {
            LOG.debug(String.format(
                "ticketsystem without labels, searching for ticket titles containing %s", MODOCOT_TITLE));
            f.withTitleContain(MODOCOT_TITLE);
        }

        LOG.debug("prepare pagination cycling");
        // set first page and page size for pagination
        int page = 1;
        int pageSize = 10;
        f.setPageSize(pageSize);
        LOG.debug(String.format("using pagination with %s elements per page", pageSize));
        // declare list for received tickets
        List<Ticket> received;
        // go into do-while to evaluate after receiving if another round is needed
        do {
            LOG.debug(String.format("calling page %s", page));
            f.setPage(page++);
            received = f.get();
            ret.addAll(received);
        } while (f.getLastReceivedItemCount() >= pageSize);

        return ret;
    }

    public static String getHashForFailure(ModocotResult result) {
        MessageDigest digest;

        try {
            digest = MessageDigest.getInstance("SHA-512");
        } catch (NoSuchAlgorithmException e) {
            throw new UnifiedticketingException(e);
        }

154
        byte[] data = digest.digest(result.name.getBytes(StandardCharsets.UTF_8));
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
        StringBuilder hexString = new StringBuilder(2 * data.length);
        for (byte character: data)
        {
            String hex = Integer.toHexString(0xff & character);
            if (hex.length() == 1)
            {
                hexString.append('0');
            }
            hexString.append(hex);
        }

        return hexString.substring(hexString.length() - 9, hexString.length() - 1);
    }

    public static void processResult(TicketSystem ts, Set<Ticket> tickets, ModocotResult result, boolean compilationError) {
        LOG.debug(String.format("retrieving hash for %s", result.name));
        String hash = getHashForFailure(result);

        // check if corresponding ticket exists yet, otherwise create new one
        Ticket ticket = tickets.stream()
            .filter(t -> t.getTitle().endsWith(hash))
            .findFirst()
            .orElse(null);

        if (ticket != null) {
            // if yet existing, remove from found list
            LOG.debug("found ticket with matching hash, removing from collection");
            tickets.remove(ticket);
            LOG.debug("updating ticket with new result");
            updateTicketFromResult(ts, ticket, result, compilationError);
        } else {
            LOG.debug("no ticket found, creating new one");
            createTicketFromResult(ts, result, compilationError);
        }
    }

    /**
     * search file for unified-ticketing URI's and report to every set ticket system,
     * not waiting for it to finish and catching an eventually interrupted thread.
     *
     * @param meta student uploaded file
     * @param resultSummary summary from the testrunner container
     */
    public static void reportResults(InputStream meta, ModocotResultSummary resultSummary) {
        try {
            reportResults(meta, resultSummary, false);
        } catch (InterruptedException e) {
            LOG.error(String.format("Unified-Ticketing got interrupted with: %s", e.getMessage()));
        }
    }

    /**
     * report all failures and compilation errors to all configured ticket systems.
     * You can optionally wait for the ticket creation to finish, which is done in a separate thread.
     *
     * @param meta student uploaded file
     * @param resultSummary summary from the testrunner container
     * @param wait if we should block until the ticket creation has finished
     * @throws InterruptedException
     */
    public static void reportResults(InputStream meta, ModocotResultSummary resultSummary, boolean wait) throws InterruptedException {

        LOG.debug("preparing thread for ticket result submitting");
        Thread unifiedTicketingUtil = new Thread(() -> {
            // read student transmitted file
            try (BufferedReader br = new BufferedReader(new InputStreamReader(meta))) {
                String line = null;
                while ((line = br.readLine()) != null) {
                    TicketSystem ts = null;

                    // try each line as URI for a unified-ticketing instantiation
                    try {
                        ts = TicketSystem.fromUri(line);
                        LOG.info(String.format("ticket system for reporting found: %s", ts.baseUrl));
                    } catch (UnifiedticketingException e) {
                        // ignore if line didn't match a unified-ticketing URI
                    }

                    // if a ticketsystem got instantiated, start the submission.
                    // If errors occur, log it this time.
                    try {
                        if (ts != null) reportToTicketsystem(ts, resultSummary);
                    } catch (UnifiedticketingException e) {
                        LOG.warn(String.format(
                            "reporting fails to ticketsystem %s failed with: ",
                            ts.baseUrl,
                            e.getMessage()));
                    }
                }
            } catch (IOException e) {
                LOG.error(String.format("couldn't read config to find lines for ticket reporting: %s", e.getMessage()));
            }
        });

        LOG.debug("starting ticket submitting thread");
        unifiedTicketingUtil.start();

        if (wait) {
            LOG.debug("wait for ticket submission completion");
            unifiedTicketingUtil.join();
        } else {
            LOG.debug("tickets will be submitted in background");
        }
    }

    public static void reportToTicketsystem(TicketSystem ts, ModocotResultSummary resultSummary) {
        // tickets existing yet
        LOG.debug("fetching existing tickets");
        Set<Ticket> tickets = fetchExistingModocotTickets(ts);

        // for each fail or compile error
        LOG.debug("start failed tests reporting");
        resultSummary.failures.forEach(f -> processResult(ts, tickets, f, false));
        LOG.debug("start compilation errors reporting");
        resultSummary.compilationErrors.forEach(c -> processResult(ts, tickets, c, true));

        LOG.debug("closing all remaining tickets, no longer appeared");
        tickets.forEach(ticket -> ticket.close().save());
    }

    public static Ticket updateTicketFromResult(TicketSystem ts, Ticket ticket, ModocotResult result, boolean compilationError) {
        ticket
            .open()
278
            .setTitle(createTicketTitleFromResult(ts, result, compilationError))
279
280
281
282
283
284
            .setDescription(createTicketDescriptionFromResult(ts, result, compilationError));
        if (ts.hasLabelSupport()) ticket.addLabel(MODOCOT_LABEL);

        return ticket.save();
    }
}