forked from juju/charmstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpad.go
119 lines (109 loc) · 3.14 KB
/
lpad.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
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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charmstore
import (
"fmt"
"strings"
"time"
"gopkg.in/juju/charm.v2"
"launchpad.net/lpad"
)
type PublishBranchError struct {
URL string
Err error
}
type PublishBranchErrors []PublishBranchError
func (errs PublishBranchErrors) Error() string {
return fmt.Sprintf("%d branch(es) failed to be published", len(errs))
}
// PublishCharmsDistro publishes all branch tips found in
// the /charms distribution in Launchpad onto store under
// the "cs:" scheme.
// apiBase specifies the Launchpad base API URL, such
// as lpad.Production or lpad.Staging.
// Errors found while processing one or more branches are
// all returned as a PublishBranchErrors value.
func PublishCharmsDistro(store *Store, apiBase lpad.APIBase) error {
oauth := &lpad.OAuth{Anonymous: true, Consumer: "juju"}
root, err := lpad.Login(apiBase, oauth)
if err != nil {
return err
}
distro, err := root.Distro("charms")
if err != nil {
return err
}
tips, err := distro.BranchTips(time.Time{})
if err != nil {
return err
}
var errs PublishBranchErrors
for _, tip := range tips {
if !strings.HasSuffix(tip.UniqueName, "/trunk") {
continue
}
burl, curl, err := uniqueNameURLs(tip.UniqueName)
if err != nil {
errs = append(errs, PublishBranchError{tip.UniqueName, err})
logger.Errorf("%v", err)
continue
}
logger.Infof("%s\n", burl)
if tip.Revision == "" {
errs = append(errs, PublishBranchError{burl, fmt.Errorf("branch has no revisions")})
logger.Errorf("branch has no revisions\n")
continue
}
// Charm is published in the personal URL and in any explicitly
// assigned official series.
urls := []*charm.URL{curl}
schema, name := curl.Schema, curl.Name
for _, series := range tip.OfficialSeries {
curl = &charm.URL{
Reference: charm.Reference{Schema: schema, Name: name, Revision: -1},
Series: series,
}
curl.Series = series
curl.User = ""
urls = append(urls, curl)
}
err = PublishBazaarBranch(store, urls, burl, tip.Revision)
if err == ErrRedundantUpdate {
continue
}
if err != nil {
errs = append(errs, PublishBranchError{burl, err})
logger.Errorf("%v", err)
}
}
if errs != nil {
return errs
}
return nil
}
// uniqueNameURLs returns the branch URL and the charm URL for the
// provided Launchpad branch unique name. The unique name must be
// in the form:
//
// ~<user>/charms/<series>/<charm name>/trunk
//
// For testing purposes, if name has a prefix preceding a string in
// this format, the prefix is stripped out for computing the charm
// URL, and the unique name is returned unchanged as the branch URL.
func uniqueNameURLs(name string) (burl string, curl *charm.URL, err error) {
u := strings.Split(name, "/")
if len(u) > 5 {
u = u[len(u)-5:]
burl = name
} else {
burl = "lp:" + name
}
if len(u) < 5 || u[1] != "charms" || u[4] != "trunk" || len(u[0]) == 0 || u[0][0] != '~' {
return "", nil, fmt.Errorf("unwanted branch name: %s", name)
}
curl, err = charm.ParseURL(fmt.Sprintf("cs:%s/%s/%s", u[0], u[2], u[3]))
if err != nil {
return "", nil, err
}
return burl, curl, nil
}