GithubTicketSystemBuilder.java 2.56 KB
Newer Older
1
2
3
4
package de.hftstuttgart.unifiedticketing.systems.github;

import de.hftstuttgart.unifiedticketing.core.Logging;
import de.hftstuttgart.unifiedticketing.core.TicketSystemBuilder;
5
import de.hftstuttgart.unifiedticketing.exceptions.AssertionException;
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

import java.util.logging.Level;
import java.util.logging.Logger;

public class GithubTicketSystemBuilder extends TicketSystemBuilder<GithubTicketSystemBuilder, GithubTicketSystem>
{
    private static final Logger logger = Logging.getLogger(GithubTicketSystemBuilder.class.getName());

    protected String acceptHeader;
    protected String apiKey;
    protected boolean https;
    protected String owner;
    protected String repo;
    protected String username;

    /**
     * starts builder process for a {@link GithubTicketSystem} instance
     */
    public GithubTicketSystemBuilder()
    {
        super();

        acceptHeader = "application/vnd.github.v3+json";
        https = true;

        logger.log(Level.FINEST, "Builder for Github System instance started");
    }

    public GithubTicketSystemBuilder withAuthentication(String username, String apiKey)
    {
        this.username = username;
        this.apiKey = apiKey;
        logger.log(Level.FINEST, "set authentication values");
        return this;
    }

    public GithubTicketSystemBuilder withHttp()
    {
        this.https = false;
        logger.log(Level.FINEST, "set http");
        return this;
    }

    public GithubTicketSystemBuilder withHttps()
    {
        this.https = true;
        logger.log(Level.FINEST, "set https");
        return this;
    }

    public GithubTicketSystemBuilder withOwner(String owner)
    {
        this.owner = owner;
        logger.log(Level.FINEST, String.format("set owner to %s", this.owner));
        return this;
    }

    public GithubTicketSystemBuilder withRepo(String repo)
    {
        this.repo = repo;
        logger.log(Level.FINEST, String.format("set repo to %s", this.repo));
        return this;
    }

    @Override
    public GithubTicketSystem build()
    {
73
74
75
76
77
78
79
80
        if (baseUrl == null
            || owner == null
            || repo == null)
        {
            String msg = "one of mandatory fields 'baseUrl', 'owner' or 'repo' missing!";
            throw new AssertionException(msg);
        }

81
82
83
84
85
86
87
88
89
90
91
92
93
94
        return new GithubTicketSystem(
            acceptHeader,
            String.format(
                "%s://%s/repos/%s/%s/issues",
                (https) ? "https" : "http",
                baseUrl,
                owner,
                repo
            ),
            username,
            apiKey
        );
    }
}