aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/launchpad/config.go
blob: 6669d0fa40deef5f07e8c7ebd00a9d4bcff9acda (plain) (blame)
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
package launchpad

import (
	"bufio"
	"errors"
	"fmt"
	"net/http"
	"os"
	"regexp"
	"strings"
	"time"

	"github.com/MichaelMure/git-bug/bridge/core"
	"github.com/MichaelMure/git-bug/repository"
)

var ErrBadProjectURL = errors.New("bad Launchpad project URL")

const (
	target         = "launchpad-preview"
	keyProject     = "project"
	defaultTimeout = 60 * time.Second
)

func (l *Launchpad) Configure(repo repository.RepoCommon, params core.BridgeParams) (core.Configuration, error) {
	if params.Token != "" {
		fmt.Println("warning: --token is ineffective for a Launchpad bridge")
	}
	if params.Owner != "" {
		fmt.Println("warning: --owner is ineffective for a Launchpad bridge")
	}

	conf := make(core.Configuration)
	var err error
	var project string

	if params.Project != "" {
		project = params.Project

	} else if params.URL != "" {
		// get project name from url
		project, err = splitURL(params.URL)
		if err != nil {
			return nil, err
		}

	} else {
		// get project name from terminal prompt
		project, err = promptProjectName()
		if err != nil {
			return nil, err
		}
	}

	// verify project
	ok, err := validateProject(project)
	if err != nil {
		return nil, err
	}
	if !ok {
		return nil, fmt.Errorf("project doesn't exist")
	}

	conf[keyProject] = project
	conf[core.KeyTarget] = target

	err = l.ValidateConfig(conf)
	if err != nil {
		return nil, err
	}

	return conf, nil
}

func (*Launchpad) ValidateConfig(conf core.Configuration) error {
	if _, ok := conf[keyProject]; !ok {
		return fmt.Errorf("missing %s key", keyProject)
	}

	if _, ok := conf[core.KeyTarget]; !ok {
		return fmt.Errorf("missing %s key", core.KeyTarget)
	}

	return nil
}

func promptProjectName() (string, error) {
	for {
		fmt.Print("Launchpad project name: ")

		line, err := bufio.NewReader(os.Stdin).ReadString('\n')
		if err != nil {
			return "", err
		}

		line = strings.TrimRight(line, "\n")

		if line == "" {
			fmt.Println("Project name is empty")
			continue
		}

		return line, nil
	}
}

func validateProject(project string) (bool, error) {
	url := fmt.Sprintf("%s/%s", apiRoot, project)

	client := &http.Client{
		Timeout: defaultTimeout,
	}

	resp, err := client.Get(url)
	if err != nil {
		return false, err
	}

	return resp.StatusCode == http.StatusOK, nil
}

// extract project name from url
func splitURL(url string) (string, error) {
	re, err := regexp.Compile(`launchpad\.net[\/:]([^\/]*[a-z]+)`)
	if err != nil {
		panic("regexp compile:" + err.Error())
	}

	res := re.FindStringSubmatch(url)
	if res == nil {
		return "", ErrBadProjectURL
	}

	return res[1], nil
}