forked from deanishe/awgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitea.go
49 lines (42 loc) · 1.09 KB
/
gitea.go
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
// Copyright (c) 2018 Dean Jackson <[email protected]>
// MIT Licence - http://opensource.org/licenses/MIT
package update
import (
"fmt"
"net/url"
"strings"
aw "github.com/deanishe/awgo"
)
// Gitea is a Workflow Option. It sets a Workflow Updater for the specified Gitea repo.
// Repo name should be the URL of the repo, e.g. "git.deanishe.net/deanishe/alfred-ssh".
func Gitea(repo string) aw.Option {
return newOption(&source{URL: giteaURL(repo), fetch: getURL})
}
func giteaURL(repo string) string {
if repo == "" {
return ""
}
u, err := url.Parse(repo)
if err != nil {
return ""
}
// If no scheme is specified, assume HTTPS and re-parse URL.
// This is necessary because URL.Host isn't present on URLs
// without a scheme (hostname is added to path)
if u.Scheme == "" {
u.Scheme = "https"
u, err = url.Parse(u.String())
if err != nil {
return ""
}
}
if u.Host == "" {
return ""
}
path := strings.Split(strings.Trim(u.Path, "/"), "/")
if len(path) != 2 {
return ""
}
u.Path = fmt.Sprintf("/api/v1/repos/%s/%s/releases", path[0], path[1])
return u.String()
}