GithubTicketSystem.java 4.46 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
154
155
package de.hftstuttgart.unifiedticketing.systems.github;

import de.hftstuttgart.unifiedticketing.core.*;
import de.hftstuttgart.unifiedticketing.exceptions.AssertionException;
import de.hftstuttgart.unifiedticketing.exceptions.UnifiedticketingException;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GithubTicketSystem extends TicketSystem<GithubTicket, GithubTicketSystem, GithubTicketBuilder, GithubFilter>
{
    private final static Logger logger = Logging.getLogger(GithubTicketSystem.class.getName());

    protected final String acceptHeader;
    protected final String apiKey;
    protected final String username;

    /**
     * creates a new instance of this class from an uri.<br>
     * <br>
     * <b>Attention:</b> This method should not be called from the enduser!<br>
     * Instead call the same method from {@link TicketSystem}.<br>
     * To call this method directly, the uri has to be shortend by the {@code "unifiedticketing:github:"} part.
     * @param uri gitlab specific part of full uri
     * @return new github ticket system instance
     */
    public static GithubTicketSystem fromUri(String uri)
    {
        Matcher matcher = Pattern.compile("^((http|https):\\/\\/)?(.*?(:[0-9]+.*?)?)::(.*?):(.*?)(:(.*?):(.*?))?$").matcher(uri);

        if (!matcher.matches())
        {
            String msg = "uri didn't match regex";
            logger.log(Level.SEVERE, msg);
            throw new AssertionException(msg);
        }

        GithubTicketSystemBuilder builder = new GithubTicketSystemBuilder();

        if (matcher.group(2) != null && matcher.group(2).length() > 0)
        {
            if (matcher.group(2).equalsIgnoreCase("http")) builder.withHttp();
            else builder.withHttps();
        }

        if (matcher.group(3) == null || matcher.group(3).length() == 0)
        {
            String msg = "no base url identified in uri";
            throw new AssertionException(msg);
        }
        builder.withBaseUrl(matcher.group(3));

        if (matcher.group(5) == null || matcher.group(5).length() == 0)
        {
            String msg = "no owner identified in uri";
            throw new AssertionException(msg);
        }
        builder.withOwner(matcher.group(5));

        if (matcher.group(6) == null || matcher.group(6).length() == 0)
        {
            String msg = "no repo identified in uri";
            throw new AssertionException(msg);
        }
        builder.withRepo(matcher.group(6));

        if (matcher.group(8) != null && matcher.group(9) != null)
        {
            builder.withAuthentication(matcher.group(8), matcher.group(9));
        }
        else logger.log(Level.INFO, "no authentication given, creating anonymous instance");

        return builder.build();
    }

    protected GithubTicketSystem(String acceptHeader, String baseUrl)
    {
        this(acceptHeader, baseUrl, null, null);
    }

    protected GithubTicketSystem(String acceptHeader, String baseUrl, String username, String apiKey)
    {
        super();

        this.acceptHeader = acceptHeader;
        this.apiKey = apiKey;
        this.baseUrl = baseUrl;
        this.username = username;
    }

    /**
     * starts builder process for new github ticket
     */
    @Override
    public GithubTicketBuilder createTicket()
    {
        return new GithubTicketBuilder(this);
    }

    @Override
    public GithubFilter find()
    {
        return new GithubFilter(this);
    }

    @Override
    public GithubTicket getTicketById(String id)
    {
        logger.log(Level.FINER, "redirecting request to find method");
        List<GithubTicket> ret = this.find()
            .withId(id)
            .get();

        if (ret == null) return null;
        else if (ret.size() != 1)
        {
            if (getConfigTrue(ConfigurationOptions.RETURN_NULL_ON_ERROR)) return null;
            else throw new UnifiedticketingException("more or less than 1 dedicated ticket found");
        }
        else return ret.get(0);
    }

    @Override
    public boolean hasAssigneeSupport()
    {
        return true;
    }

    @Override
    public boolean hasDefaultPagination()
    {
        return true;
    }

    @Override
    public boolean hasLabelSupport()
    {
        return true;
    }

    @Override
    public boolean hasPaginationSupport()
    {
        return true;
    }

    @Override
    public boolean hasReturnNullOnErrorSupport()
    {
        return true;
    }
}