Skip to content

Commit 6e95126

Browse files
Add alphanumeric param to AutolinkOptions (#2450)
Fixes: #2449.
1 parent af69917 commit 6e95126

7 files changed

+131
-42
lines changed

github/github-accessors.go

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/repos_autolinks.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ import (
1212

1313
// AutolinkOptions specifies parameters for RepositoriesService.AddAutolink method.
1414
type AutolinkOptions struct {
15-
KeyPrefix *string `json:"key_prefix,omitempty"`
16-
URLTemplate *string `json:"url_template,omitempty"`
15+
KeyPrefix *string `json:"key_prefix,omitempty"`
16+
URLTemplate *string `json:"url_template,omitempty"`
17+
IsAlphanumeric *bool `json:"is_alphanumeric,omitempty"`
1718
}
1819

1920
// Autolink represents autolinks to external resources like JIRA issues and Zendesk tickets.
2021
type Autolink struct {
21-
ID *int64 `json:"id,omitempty"`
22-
KeyPrefix *string `json:"key_prefix,omitempty"`
23-
URLTemplate *string `json:"url_template,omitempty"`
22+
ID *int64 `json:"id,omitempty"`
23+
KeyPrefix *string `json:"key_prefix,omitempty"`
24+
URLTemplate *string `json:"url_template,omitempty"`
25+
IsAlphanumeric *bool `json:"is_alphanumeric,omitempty"`
2426
}
2527

2628
// ListAutolinks returns a list of autolinks configured for the given repository.

github/repos_autolinks_test.go

+26-11
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ func TestRepositoriesService_AddAutolink(t *testing.T) {
6262
client, mux, _, teardown := setup()
6363
defer teardown()
6464

65-
opt := &AutolinkOptions{KeyPrefix: String("TICKET-"), URLTemplate: String("https://example.com/TICKET?query=<num>")}
65+
opt := &AutolinkOptions{
66+
KeyPrefix: String("TICKET-"),
67+
URLTemplate: String("https://example.com/TICKET?query=<num>"),
68+
IsAlphanumeric: Bool(true),
69+
}
6670
mux.HandleFunc("/repos/o/r/autolinks", func(w http.ResponseWriter, r *http.Request) {
6771
v := new(AutolinkOptions)
6872
json.NewDecoder(r.Body).Decode(v)
@@ -71,16 +75,23 @@ func TestRepositoriesService_AddAutolink(t *testing.T) {
7175
t.Errorf("Request body = %+v, want %+v", v, opt)
7276
}
7377
w.WriteHeader(http.StatusOK)
74-
w.Write([]byte(`{"key_prefix": "TICKET-","url_template": "https://example.com/TICKET?query=<num>"}`))
78+
w.Write([]byte(`
79+
{
80+
"key_prefix": "TICKET-",
81+
"url_template": "https://example.com/TICKET?query=<num>",
82+
"is_alphanumeric": true
83+
}
84+
`))
7585
})
7686
ctx := context.Background()
7787
autolink, _, err := client.Repositories.AddAutolink(ctx, "o", "r", opt)
7888
if err != nil {
7989
t.Errorf("Repositories.AddAutolink returned error: %v", err)
8090
}
8191
want := &Autolink{
82-
KeyPrefix: String("TICKET-"),
83-
URLTemplate: String("https://example.com/TICKET?query=<num>"),
92+
KeyPrefix: String("TICKET-"),
93+
URLTemplate: String("https://example.com/TICKET?query=<num>"),
94+
IsAlphanumeric: Bool(true),
8495
}
8596

8697
if !cmp.Equal(autolink, want) {
@@ -157,13 +168,15 @@ func TestAutolinkOptions_Marshal(t *testing.T) {
157168
testJSONMarshal(t, &AutolinkOptions{}, "{}")
158169

159170
r := &AutolinkOptions{
160-
KeyPrefix: String("kp"),
161-
URLTemplate: String("URLT"),
171+
KeyPrefix: String("kp"),
172+
URLTemplate: String("URLT"),
173+
IsAlphanumeric: Bool(true),
162174
}
163175

164176
want := `{
165177
"key_prefix": "kp",
166-
"url_template": "URLT"
178+
"url_template": "URLT",
179+
"is_alphanumeric": true
167180
}`
168181

169182
testJSONMarshal(t, r, want)
@@ -173,15 +186,17 @@ func TestAutolink_Marshal(t *testing.T) {
173186
testJSONMarshal(t, &Autolink{}, "{}")
174187

175188
r := &Autolink{
176-
ID: Int64(1),
177-
KeyPrefix: String("kp"),
178-
URLTemplate: String("URLT"),
189+
ID: Int64(1),
190+
KeyPrefix: String("kp"),
191+
URLTemplate: String("URLT"),
192+
IsAlphanumeric: Bool(true),
179193
}
180194

181195
want := `{
182196
"id": 1,
183197
"key_prefix": "kp",
184-
"url_template": "URLT"
198+
"url_template": "URLT",
199+
"is_alphanumeric": true
185200
}`
186201

187202
testJSONMarshal(t, r, want)

test/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Run tests using:
3131

3232
GITHUB_AUTH_TOKEN=XXX go test -v -tags=integration ./integration
3333

34+
Some tests create repositories. By default, the new repositories will be owned
35+
by the user identified by the OAuth token. Set the `GITHUB_OWNER='<GH_OWNER>'`
36+
environment variable to specify a different owner, such as an organization.
37+
3438
Additionally there are a set of integration tests for the Authorizations API.
3539
These tests require a GitHub user (username and password), and also that a
3640
[GitHub Application](https://github.com/settings/applications/new) (with

test/integration/github_test.go

+20-12
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,6 @@ func init() {
3939
client = github.NewClient(tc)
4040
auth = true
4141
}
42-
43-
// Environment variables required for Authorization integration tests
44-
vars := []string{envKeyGitHubUsername, envKeyGitHubPassword, envKeyClientID, envKeyClientSecret}
45-
46-
for _, v := range vars {
47-
value := os.Getenv(v)
48-
if value == "" {
49-
print("!!! " + fmt.Sprintf(msgEnvMissing, v) + " !!!\n\n")
50-
}
51-
}
52-
5342
}
5443

5544
func checkAuth(name string) bool {
@@ -60,6 +49,18 @@ func checkAuth(name string) bool {
6049
}
6150

6251
func createRandomTestRepository(owner string, autoinit bool) (*github.Repository, error) {
52+
// determine the owner to use if one wasn't specified
53+
if owner == "" {
54+
owner = os.Getenv("GITHUB_OWNER")
55+
if owner == "" {
56+
me, _, err := client.Users.Get(context.Background(), "")
57+
if err != nil {
58+
return nil, err
59+
}
60+
owner = *me.Login
61+
}
62+
}
63+
6364
// create random repo name that does not currently exist
6465
var repoName string
6566
for {
@@ -76,7 +77,14 @@ func createRandomTestRepository(owner string, autoinit bool) (*github.Repository
7677
}
7778

7879
// create the repository
79-
repo, _, err := client.Repositories.Create(context.Background(), "", &github.Repository{Name: github.String(repoName), AutoInit: github.Bool(autoinit)})
80+
repo, _, err := client.Repositories.Create(
81+
context.Background(),
82+
owner,
83+
&github.Repository{
84+
Name: github.String(repoName),
85+
AutoInit: github.Bool(autoinit),
86+
},
87+
)
8088
if err != nil {
8189
return nil, err
8290
}

test/integration/repos_test.go

+38-14
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,8 @@ func TestRepositories_CRUD(t *testing.T) {
2424
return
2525
}
2626

27-
// get authenticated user
28-
me, _, err := client.Users.Get(context.Background(), "")
29-
if err != nil {
30-
t.Fatalf("Users.Get('') returned error: %v", err)
31-
}
32-
33-
repo, err := createRandomTestRepository(*me.Login, false)
27+
// create a random repository
28+
repo, err := createRandomTestRepository("", true)
3429
if err != nil {
3530
t.Fatalf("createRandomTestRepository returned error: %v", err)
3631
}
@@ -91,13 +86,8 @@ func TestRepositories_EditBranches(t *testing.T) {
9186
return
9287
}
9388

94-
// get authenticated user
95-
me, _, err := client.Users.Get(context.Background(), "")
96-
if err != nil {
97-
t.Fatalf("Users.Get('') returned error: %v", err)
98-
}
99-
100-
repo, err := createRandomTestRepository(*me.Login, true)
89+
// create a random repository
90+
repo, err := createRandomTestRepository("", true)
10191
if err != nil {
10292
t.Fatalf("createRandomTestRepository returned error: %v", err)
10393
}
@@ -198,3 +188,37 @@ func TestRepositories_DownloadReleaseAsset(t *testing.T) {
198188
t.Fatalf("Repositories.DownloadReleaseAsset(andersjanmyr, goose, 484892, true) returned error: %v", err)
199189
}
200190
}
191+
192+
func TestRepositories_Autolinks(t *testing.T) {
193+
if !checkAuth("TestRepositories_Autolinks") {
194+
return
195+
}
196+
197+
// create a random repository
198+
repo, err := createRandomTestRepository("", true)
199+
if err != nil {
200+
t.Fatalf("createRandomTestRepository returned error: %v", err)
201+
}
202+
203+
opts := &github.AutolinkOptions{
204+
KeyPrefix: github.String("TICKET-"),
205+
URLTemplate: github.String("https://example.com/TICKET?query=<num>"),
206+
IsAlphanumeric: github.Bool(false),
207+
}
208+
209+
actionlink, _, err := client.Repositories.AddAutolink(context.Background(), *repo.Owner.Login, *repo.Name, opts)
210+
if err != nil {
211+
t.Fatalf("Repositories.AddAutolink() returned error: %v", err)
212+
}
213+
214+
if !cmp.Equal(actionlink.KeyPrefix, opts.KeyPrefix) ||
215+
!cmp.Equal(actionlink.URLTemplate, opts.URLTemplate) ||
216+
!cmp.Equal(actionlink.IsAlphanumeric, opts.IsAlphanumeric) {
217+
t.Errorf("Repositories.AddAutolink() returned %+v, want %+v", actionlink, opts)
218+
}
219+
220+
_, err = client.Repositories.Delete(context.Background(), *repo.Owner.Login, *repo.Name)
221+
if err != nil {
222+
t.Fatalf("Repositories.Delete() returned error: %v", err)
223+
}
224+
}

0 commit comments

Comments
 (0)