Skip to content

Commit cc676b6

Browse files
committed
Bug 1459967: Add a go implementation
1 parent 9131506 commit cc676b6

File tree

7 files changed

+290
-63
lines changed

7 files changed

+290
-63
lines changed

.gometalinter.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"Enable": [
3+
"deadcode",
4+
"errcheck",
5+
"gas",
6+
"goconst",
7+
"goimports",
8+
"golint",
9+
"gosimple",
10+
"gotype",
11+
"gotypex",
12+
"ineffassign",
13+
"interfacer",
14+
"maligned",
15+
"megacheck",
16+
"misspell",
17+
"nakedret",
18+
"safesql",
19+
"staticcheck",
20+
"structcheck",
21+
"unconvert",
22+
"unparam",
23+
"unused",
24+
"varcheck"
25+
]
26+
}

.taskcluster.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,34 @@ tasks:
6060
description: "Upload documentation for this project"
6161
owner: '{{ event.head.user.email }}'
6262
source: '{{ event.head.repo.url }}'
63+
- provisionerId: '{{ taskcluster.docker.provisionerId }}'
64+
workerType: '{{ taskcluster.docker.workerType }}'
65+
extra:
66+
github:
67+
events:
68+
- pull_request.opened
69+
- pull_request.synchronize
70+
- pull_request.reopened
71+
- push
72+
payload:
73+
maxRunTime: 3600
74+
image: 'golang:1.10'
75+
command:
76+
- /bin/bash
77+
- '-c'
78+
- >-
79+
mkdir -p /go/src/github.com/taskcluster/taskcluster-lib-urls &&
80+
cd /go/src/github.com/taskcluster/taskcluster-lib-urls &&
81+
git clone {{event.head.repo.url}} . &&
82+
git config advice.detachedHead false &&
83+
git checkout {{event.head.sha}} &&
84+
go get -v -d -t ./... &&
85+
go test -v -race ./... &&
86+
go get -u github.com/alecthomas/gometalinter &&
87+
gometalinter --install &&
88+
gometalinter
89+
metadata:
90+
name: taskcluster-lib-urls go test
91+
description: Run library test suite - golang 1.10
92+
owner: '{{ event.head.user.email }}'
93+
source: '{{ event.head.repo.url }}'

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Requirements
1818

1919
This is tested on and should run on any of Node.js `{8, 10}`.
2020

21-
Usage
21+
JS Usage
2222
-----
2323

2424
This package exports several methods for generating URLs conditionally based on
@@ -76,6 +76,20 @@ If you would like, you can set this up via [taskcluster-lib-loader](https://gith
7676
}
7777
```
7878

79+
Go Usage
80+
--------
81+
82+
The go package exports the following functions:
83+
84+
```go
85+
func Api(rootUrl string, service string, version string, path string) string
86+
func ApiReference(rootUrl string, service string, version string) string
87+
func Docs(rootUrl string, path string) string
88+
func ExchangeReference(rootUrl string, service string, version string) string
89+
func Schema(rootUrl string, service string, name string) string
90+
func Ui(rootUrl string, path string) string
91+
```
92+
7993
Testing
8094
-------
8195

specification.yml

Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,56 @@
1111
# This format is explained further in `docs/urls-spec.md`
1212
#################################################################################
1313
---
14-
type: api
15-
expectedUrl: https://taskcluster.example.com/api/auth/v1/ping
16-
oldExpectedUrl: https://auth.taskcluster.net/v1/ping
17-
argSets:
18-
- [auth, v1, ping]
19-
- [auth, v1, /ping]
20-
---
21-
type: api
22-
expectedUrl: https://taskcluster.example.com/api/auth/v1/foo/ping
23-
oldExpectedUrl: https://auth.taskcluster.net/v1/foo/ping
24-
argSets:
25-
- [auth, v1, foo/ping]
26-
- [auth, v1, /foo/ping]
27-
---
28-
type: docs
29-
expectedUrl: https://taskcluster.example.com/docs/something/in/docs
30-
oldExpectedUrl: https://docs.taskcluster.net/something/in/docs
31-
argSets:
32-
- [something/in/docs]
33-
- [/something/in/docs]
34-
---
35-
type: schema
36-
expectedUrl: https://taskcluster.example.com/schemas/auth/v1/something.json
37-
oldExpectedUrl: https://schemas.taskcluster.net/auth/v1/something.json
38-
argSets:
39-
- [auth, v1/something.json]
40-
- [auth, /v1/something.json]
41-
---
42-
type: schema
43-
expectedUrl: https://taskcluster.example.com/schemas/auth/v2/something.json
44-
oldExpectedUrl: https://schemas.taskcluster.net/auth/v2/something.json
45-
argSets:
46-
- [auth, v2/something.json]
47-
- [auth, /v2/something.json]
48-
---
49-
type: apiReference
50-
expectedUrl: https://taskcluster.example.com/references/auth/v1/api.json
51-
oldExpectedUrl: https://references.taskcluster.net/auth/v1/api.json
52-
argSets:
53-
- [auth, v1]
54-
---
55-
type: exchangeReference
56-
expectedUrl: https://taskcluster.example.com/references/auth/v1/exchanges.json
57-
oldExpectedUrl: https://references.taskcluster.net/auth/v1/exchanges.json
58-
argSets:
59-
- [auth, v1]
60-
---
61-
type: ui
62-
expectedUrl: https://taskcluster.example.com/something
63-
oldExpectedUrl: https://tools.taskcluster.net/something
64-
argSets:
65-
- [something]
66-
- [/something]
67-
---
68-
type: ui
69-
expectedUrl: https://taskcluster.example.com/
70-
oldExpectedUrl: https://tools.taskcluster.net/
71-
argSets:
72-
- ['']
73-
- [/]
14+
specs:
15+
- type: api
16+
expectedUrl: https://taskcluster.example.com/api/auth/v1/ping
17+
oldExpectedUrl: https://auth.taskcluster.net/v1/ping
18+
argSets:
19+
- [auth, v1, ping]
20+
- [auth, v1, /ping]
21+
- type: api
22+
expectedUrl: https://taskcluster.example.com/api/auth/v1/foo/ping
23+
oldExpectedUrl: https://auth.taskcluster.net/v1/foo/ping
24+
argSets:
25+
- [auth, v1, foo/ping]
26+
- [auth, v1, /foo/ping]
27+
- type: docs
28+
expectedUrl: https://taskcluster.example.com/docs/something/in/docs
29+
oldExpectedUrl: https://docs.taskcluster.net/something/in/docs
30+
argSets:
31+
- [something/in/docs]
32+
- [/something/in/docs]
33+
- type: schema
34+
expectedUrl: https://taskcluster.example.com/schemas/auth/v1/something.json
35+
oldExpectedUrl: https://schemas.taskcluster.net/auth/v1/something.json
36+
argSets:
37+
- [auth, v1/something.json]
38+
- [auth, /v1/something.json]
39+
- type: schema
40+
expectedUrl: https://taskcluster.example.com/schemas/auth/v2/something.json
41+
oldExpectedUrl: https://schemas.taskcluster.net/auth/v2/something.json
42+
argSets:
43+
- [auth, v2/something.json]
44+
- [auth, /v2/something.json]
45+
- type: apiReference
46+
expectedUrl: https://taskcluster.example.com/references/auth/v1/api.json
47+
oldExpectedUrl: https://references.taskcluster.net/auth/v1/api.json
48+
argSets:
49+
- [auth, v1]
50+
- type: exchangeReference
51+
expectedUrl: https://taskcluster.example.com/references/auth/v1/exchanges.json
52+
oldExpectedUrl: https://references.taskcluster.net/auth/v1/exchanges.json
53+
argSets:
54+
- [auth, v1]
55+
- type: ui
56+
expectedUrl: https://taskcluster.example.com/something
57+
oldExpectedUrl: https://tools.taskcluster.net/something
58+
argSets:
59+
- [something]
60+
- [/something]
61+
- type: ui
62+
expectedUrl: https://taskcluster.example.com/
63+
oldExpectedUrl: https://tools.taskcluster.net/
64+
argSets:
65+
- ['']
66+
- [/]

taskcluster_lib_urls.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package taskclusterurls
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
const oldRootURL = "https://taskcluster.net"
9+
10+
// API generates a url for a resource in a taskcluster service
11+
func API(rootURL string, service string, version string, path string) string {
12+
path = strings.TrimLeft(path, "/")
13+
switch r := strings.TrimRight(rootURL, "/"); r {
14+
case oldRootURL:
15+
return fmt.Sprintf("https://%s.taskcluster.net/%s/%s", service, version, path)
16+
default:
17+
return fmt.Sprintf("%s/api/%s/%s/%s", r, service, version, path)
18+
}
19+
}
20+
21+
// APIReference enerates a url for a taskcluster service reference doc
22+
func APIReference(rootURL string, service string, version string) string {
23+
switch r := strings.TrimRight(rootURL, "/"); r {
24+
case oldRootURL:
25+
return fmt.Sprintf("https://references.taskcluster.net/%s/%s/api.json", service, version)
26+
default:
27+
return fmt.Sprintf("%s/references/%s/%s/api.json", r, service, version)
28+
}
29+
}
30+
31+
// Docs generates a url for a taskcluster docs-site page
32+
func Docs(rootURL string, path string) string {
33+
path = strings.TrimLeft(path, "/")
34+
switch r := strings.TrimRight(rootURL, "/"); r {
35+
case oldRootURL:
36+
return fmt.Sprintf("https://docs.taskcluster.net/%s", path)
37+
default:
38+
return fmt.Sprintf("%s/docs/%s", r, path)
39+
}
40+
}
41+
42+
// ExchangeReference generates a url for a taskcluster exchange reference doc
43+
func ExchangeReference(rootURL string, service string, version string) string {
44+
switch r := strings.TrimRight(rootURL, "/"); r {
45+
case oldRootURL:
46+
return fmt.Sprintf("https://references.taskcluster.net/%s/%s/exchanges.json", service, version)
47+
default:
48+
return fmt.Sprintf("%s/references/%s/%s/exchanges.json", r, service, version)
49+
}
50+
}
51+
52+
// Schema generates a url for a taskcluster schema
53+
func Schema(rootURL string, service string, name string) string {
54+
name = strings.TrimLeft(name, "/")
55+
switch r := strings.TrimRight(rootURL, "/"); r {
56+
case oldRootURL:
57+
return fmt.Sprintf("https://schemas.taskcluster.net/%s/%s", service, name)
58+
default:
59+
return fmt.Sprintf("%s/schemas/%s/%s", r, service, name)
60+
}
61+
}
62+
63+
// UI generates a url for a page in taskcluster tools site
64+
func UI(rootURL string, path string) string {
65+
path = strings.TrimLeft(path, "/")
66+
switch r := strings.TrimRight(rootURL, "/"); r {
67+
case oldRootURL:
68+
return fmt.Sprintf("https://tools.taskcluster.net/%s", path)
69+
default:
70+
return fmt.Sprintf("%s/%s", r, path)
71+
}
72+
}

taskcluster_lib_urls_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package taskclusterurls
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"testing"
7+
8+
"gopkg.in/yaml.v2"
9+
)
10+
11+
const rootURL = "https://taskcluster.example.com"
12+
13+
type spec struct {
14+
FunctionType string `yaml:"type"`
15+
ExpectedURL string `yaml:"expectedUrl"`
16+
OldExpectedURL string `yaml:"oldExpectedUrl"`
17+
ArgSets [][]string `yaml:"argSets"`
18+
}
19+
20+
type document struct {
21+
Specs []spec `yaml:"specs"`
22+
}
23+
24+
func testFunc(t *testing.T, functionType string, root string, args ...string) (string, error) {
25+
switch functionType {
26+
case "api":
27+
return API(root, args[0], args[1], args[2]), nil
28+
case "apiReference":
29+
return APIReference(root, args[0], args[1]), nil
30+
case "docs":
31+
return Docs(root, args[0]), nil
32+
case "exchangeReference":
33+
return ExchangeReference(root, args[0], args[1]), nil
34+
case "schema":
35+
return Schema(root, args[0], args[1]), nil
36+
case "ui":
37+
return UI(root, args[0]), nil
38+
default:
39+
return "", fmt.Errorf("Unknown function type: %s", functionType)
40+
}
41+
}
42+
43+
func TestUrls(t *testing.T) {
44+
data, err := ioutil.ReadFile("specification.yml")
45+
if err != nil {
46+
t.Error(err)
47+
}
48+
var specs document
49+
err = yaml.Unmarshal([]byte(data), &specs)
50+
if err != nil {
51+
t.Error(err)
52+
}
53+
54+
for _, test := range specs.Specs {
55+
// First test "new" urls
56+
for _, argSet := range test.ArgSets {
57+
result, err := testFunc(t, test.FunctionType, rootURL, argSet...)
58+
if err != nil {
59+
t.Error(err)
60+
continue
61+
}
62+
if result != test.ExpectedURL {
63+
t.Errorf("Url is not correct. Got %s wanted %s", result, test.ExpectedURL)
64+
continue
65+
}
66+
result, err = testFunc(t, test.FunctionType, fmt.Sprintf("%s/", rootURL), argSet...)
67+
if err != nil {
68+
t.Error(err)
69+
}
70+
if result != test.ExpectedURL {
71+
t.Errorf("Url is not correct. Got %s wanted %s", result, test.ExpectedURL)
72+
continue
73+
}
74+
75+
// Now the old ones
76+
result, err = testFunc(t, test.FunctionType, oldRootURL, argSet...)
77+
if err != nil {
78+
t.Error(err)
79+
}
80+
if result != test.OldExpectedURL {
81+
t.Errorf("Url is not correct. Got %s wanted %s", result, test.OldExpectedURL)
82+
}
83+
result, err = testFunc(t, test.FunctionType, fmt.Sprintf("%s/", oldRootURL), argSet...)
84+
if err != nil {
85+
t.Error(err)
86+
}
87+
if result != test.OldExpectedURL {
88+
t.Errorf("Url is not correct. Got %s wanted %s", result, test.OldExpectedURL)
89+
}
90+
}
91+
}
92+
}

test/basic_test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ const ROOT_URL = 'https://taskcluster.example.com';
99
const OLD_ROOT_URL = 'https://taskcluster.net';
1010

1111
suite('basic test', function() {
12-
let testCases;
13-
yaml.loadAll(fs.readFileSync(SPEC_FILE, {encoding: 'utf8'}), testCase => {
12+
yaml.safeLoad(fs.readFileSync(SPEC_FILE, {encoding: 'utf8'})).specs.forEach(testCase => {
1413
const {type, expectedUrl, oldExpectedUrl, argSets} = testCase;
1514

1615
test(expectedUrl, function() {

0 commit comments

Comments
 (0)