Skip to content

Commit

Permalink
Add alphanumeric param to AutolinkOptions (#2450)
Browse files Browse the repository at this point in the history
Fixes: #2449.
  • Loading branch information
douglascayers committed Sep 5, 2022
1 parent af69917 commit 6e95126
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 42 deletions.
16 changes: 16 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions github/repos_autolinks.go
Expand Up @@ -12,15 +12,17 @@ import (

// AutolinkOptions specifies parameters for RepositoriesService.AddAutolink method.
type AutolinkOptions struct {
KeyPrefix *string `json:"key_prefix,omitempty"`
URLTemplate *string `json:"url_template,omitempty"`
KeyPrefix *string `json:"key_prefix,omitempty"`
URLTemplate *string `json:"url_template,omitempty"`
IsAlphanumeric *bool `json:"is_alphanumeric,omitempty"`
}

// Autolink represents autolinks to external resources like JIRA issues and Zendesk tickets.
type Autolink struct {
ID *int64 `json:"id,omitempty"`
KeyPrefix *string `json:"key_prefix,omitempty"`
URLTemplate *string `json:"url_template,omitempty"`
ID *int64 `json:"id,omitempty"`
KeyPrefix *string `json:"key_prefix,omitempty"`
URLTemplate *string `json:"url_template,omitempty"`
IsAlphanumeric *bool `json:"is_alphanumeric,omitempty"`
}

// ListAutolinks returns a list of autolinks configured for the given repository.
Expand Down
37 changes: 26 additions & 11 deletions github/repos_autolinks_test.go
Expand Up @@ -62,7 +62,11 @@ func TestRepositoriesService_AddAutolink(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

opt := &AutolinkOptions{KeyPrefix: String("TICKET-"), URLTemplate: String("https://example.com/TICKET?query=<num>")}
opt := &AutolinkOptions{
KeyPrefix: String("TICKET-"),
URLTemplate: String("https://example.com/TICKET?query=<num>"),
IsAlphanumeric: Bool(true),
}
mux.HandleFunc("/repos/o/r/autolinks", func(w http.ResponseWriter, r *http.Request) {
v := new(AutolinkOptions)
json.NewDecoder(r.Body).Decode(v)
Expand All @@ -71,16 +75,23 @@ func TestRepositoriesService_AddAutolink(t *testing.T) {
t.Errorf("Request body = %+v, want %+v", v, opt)
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"key_prefix": "TICKET-","url_template": "https://example.com/TICKET?query=<num>"}`))
w.Write([]byte(`
{
"key_prefix": "TICKET-",
"url_template": "https://example.com/TICKET?query=<num>",
"is_alphanumeric": true
}
`))
})
ctx := context.Background()
autolink, _, err := client.Repositories.AddAutolink(ctx, "o", "r", opt)
if err != nil {
t.Errorf("Repositories.AddAutolink returned error: %v", err)
}
want := &Autolink{
KeyPrefix: String("TICKET-"),
URLTemplate: String("https://example.com/TICKET?query=<num>"),
KeyPrefix: String("TICKET-"),
URLTemplate: String("https://example.com/TICKET?query=<num>"),
IsAlphanumeric: Bool(true),
}

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

r := &AutolinkOptions{
KeyPrefix: String("kp"),
URLTemplate: String("URLT"),
KeyPrefix: String("kp"),
URLTemplate: String("URLT"),
IsAlphanumeric: Bool(true),
}

want := `{
"key_prefix": "kp",
"url_template": "URLT"
"url_template": "URLT",
"is_alphanumeric": true
}`

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

r := &Autolink{
ID: Int64(1),
KeyPrefix: String("kp"),
URLTemplate: String("URLT"),
ID: Int64(1),
KeyPrefix: String("kp"),
URLTemplate: String("URLT"),
IsAlphanumeric: Bool(true),
}

want := `{
"id": 1,
"key_prefix": "kp",
"url_template": "URLT"
"url_template": "URLT",
"is_alphanumeric": true
}`

testJSONMarshal(t, r, want)
Expand Down
4 changes: 4 additions & 0 deletions test/README.md
Expand Up @@ -31,6 +31,10 @@ Run tests using:

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

Some tests create repositories. By default, the new repositories will be owned
by the user identified by the OAuth token. Set the `GITHUB_OWNER='<GH_OWNER>'`
environment variable to specify a different owner, such as an organization.

Additionally there are a set of integration tests for the Authorizations API.
These tests require a GitHub user (username and password), and also that a
[GitHub Application](https://github.com/settings/applications/new) (with
Expand Down
32 changes: 20 additions & 12 deletions test/integration/github_test.go
Expand Up @@ -39,17 +39,6 @@ func init() {
client = github.NewClient(tc)
auth = true
}

// Environment variables required for Authorization integration tests
vars := []string{envKeyGitHubUsername, envKeyGitHubPassword, envKeyClientID, envKeyClientSecret}

for _, v := range vars {
value := os.Getenv(v)
if value == "" {
print("!!! " + fmt.Sprintf(msgEnvMissing, v) + " !!!\n\n")
}
}

}

func checkAuth(name string) bool {
Expand All @@ -60,6 +49,18 @@ func checkAuth(name string) bool {
}

func createRandomTestRepository(owner string, autoinit bool) (*github.Repository, error) {
// determine the owner to use if one wasn't specified
if owner == "" {
owner = os.Getenv("GITHUB_OWNER")
if owner == "" {
me, _, err := client.Users.Get(context.Background(), "")
if err != nil {
return nil, err
}
owner = *me.Login
}
}

// create random repo name that does not currently exist
var repoName string
for {
Expand All @@ -76,7 +77,14 @@ func createRandomTestRepository(owner string, autoinit bool) (*github.Repository
}

// create the repository
repo, _, err := client.Repositories.Create(context.Background(), "", &github.Repository{Name: github.String(repoName), AutoInit: github.Bool(autoinit)})
repo, _, err := client.Repositories.Create(
context.Background(),
owner,
&github.Repository{
Name: github.String(repoName),
AutoInit: github.Bool(autoinit),
},
)
if err != nil {
return nil, err
}
Expand Down
52 changes: 38 additions & 14 deletions test/integration/repos_test.go
Expand Up @@ -24,13 +24,8 @@ func TestRepositories_CRUD(t *testing.T) {
return
}

// get authenticated user
me, _, err := client.Users.Get(context.Background(), "")
if err != nil {
t.Fatalf("Users.Get('') returned error: %v", err)
}

repo, err := createRandomTestRepository(*me.Login, false)
// create a random repository
repo, err := createRandomTestRepository("", true)
if err != nil {
t.Fatalf("createRandomTestRepository returned error: %v", err)
}
Expand Down Expand Up @@ -91,13 +86,8 @@ func TestRepositories_EditBranches(t *testing.T) {
return
}

// get authenticated user
me, _, err := client.Users.Get(context.Background(), "")
if err != nil {
t.Fatalf("Users.Get('') returned error: %v", err)
}

repo, err := createRandomTestRepository(*me.Login, true)
// create a random repository
repo, err := createRandomTestRepository("", true)
if err != nil {
t.Fatalf("createRandomTestRepository returned error: %v", err)
}
Expand Down Expand Up @@ -198,3 +188,37 @@ func TestRepositories_DownloadReleaseAsset(t *testing.T) {
t.Fatalf("Repositories.DownloadReleaseAsset(andersjanmyr, goose, 484892, true) returned error: %v", err)
}
}

func TestRepositories_Autolinks(t *testing.T) {
if !checkAuth("TestRepositories_Autolinks") {
return
}

// create a random repository
repo, err := createRandomTestRepository("", true)
if err != nil {
t.Fatalf("createRandomTestRepository returned error: %v", err)
}

opts := &github.AutolinkOptions{
KeyPrefix: github.String("TICKET-"),
URLTemplate: github.String("https://example.com/TICKET?query=<num>"),
IsAlphanumeric: github.Bool(false),
}

actionlink, _, err := client.Repositories.AddAutolink(context.Background(), *repo.Owner.Login, *repo.Name, opts)
if err != nil {
t.Fatalf("Repositories.AddAutolink() returned error: %v", err)
}

if !cmp.Equal(actionlink.KeyPrefix, opts.KeyPrefix) ||
!cmp.Equal(actionlink.URLTemplate, opts.URLTemplate) ||
!cmp.Equal(actionlink.IsAlphanumeric, opts.IsAlphanumeric) {
t.Errorf("Repositories.AddAutolink() returned %+v, want %+v", actionlink, opts)
}

_, err = client.Repositories.Delete(context.Background(), *repo.Owner.Login, *repo.Name)
if err != nil {
t.Fatalf("Repositories.Delete() returned error: %v", err)
}
}

0 comments on commit 6e95126

Please sign in to comment.