8000 Add support for apps webhook config endpoints by rpelliard · Pull Request #2096 · google/go-github · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add support for apps webhook config endpoints #2096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions github/apps_hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2021 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
"context"
)

// GetHookConfig returns the webhook configuration for a GitHub App.
// The underlying transport must be authenticated as an app.
//
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/apps#get-a-webhook-configuration-for-an-app
func (s *AppsService) GetHookConfig(ctx context.Context) (*HookConfig, *Response, error) {
req, err := s.client.NewRequest("GET", "app/hook/config", nil)
if err != nil {
return nil, nil, err
}

config := new(HookConfig)
resp, err := s.client.Do(ctx, req, &config)
if err != nil {
return nil, resp, err
}

return config, resp, nil
}

// UpdateHookConfig updates the webhook configuration for a GitHub App.
// The underlying transport must be authenticated as an app.
//
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/apps#update-a-webhook-configuration-for-an-app
func (s *AppsService) UpdateHookConfig(ctx context.Context, config *HookConfig) (*HookConfig, *Response, error) {
req, err := s.client.NewRequest("PATCH", "app/hook/config", config)
if err != nil {
return nil, nil, err
}

c := new(HookConfig)
resp, err := s.client.Do(ctx, req, c)
if err != nil {
return nil, resp, err
}

return c, resp, nil
}
103 changes: 103 additions & 0 deletions github/apps_hooks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright 2021 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestAppsService_GetHookConfig(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/app/hook/config", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{
"content_type": "json",
"insecure_ssl": "0",
"secret": "********",
"url": "https://example.com/webhook"
}`)
})

ctx := context.Background()
config, _, err := client.Apps.GetHookConfig(ctx)
if err != nil {
t.Errorf("Apps.GetHookConfig returned error: %v", err)
}

want := &HookConfig{
ContentType: String("json"),
InsecureSSL: String("0"),
Secret: String("********"),
URL: String("https://example.com/webhook"),
}
if !cmp.Equal(config, want) {
t.Errorf("Apps.GetHookConfig returned %+v, want %+v", config, want)
}

const methodName = "GetHookConfig"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Apps.GetHookConfig(ctx)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestAppsService_UpdateHookConfig(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

input := &HookConfig{
ContentType: String("json"),
InsecureSSL: String("1"),
Secret: String("s"),
URL: String("u"),
}
mux.HandleFunc("/app/hook/config", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PATCH")
testBody(t, r, `{"content_type":"json","insecure_ssl":"1","url":"u","secret":"s"}`+"\n")
fmt.Fprint(w, `{
"content_type": "json",
"insecure_ssl": "1",
"secret": "********",
"url": "u"
}`)
})

ctx := context.Background()
config, _, err := client.Apps.UpdateHookConfig(ctx, input)
if err != nil {
t.Errorf("Apps.UpdateHookConfig returned error: %v", err)
}

want := &HookConfig{
ContentType: String("json"),
InsecureSSL: String("1"),
Secret: String("********"),
URL: String("u"),
}
if !cmp.Equal(config, want) {
t.Errorf("Apps.UpdateHookConfig returned %+v, want %+v", config, want)
}

const methodName = "UpdateHookConfig"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Apps.UpdateHookConfig(ctx, input)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}
21 changes: 21 additions & 0 deletions github/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ BasicAuthTransport.

GitHub Apps authentication can be provided by the
https://github.com/bradleyfalzon/ghinstallation package.
It supports both authentication as an installation, using an installation access token,
and as an app, using a JWT.

To authenticate as an installation:

import "github.com/bradleyfalzon/ghinstallation"

Expand All @@ -89,6 +93,23 @@ https://github.com/bradleyfalzon/ghinstallation package.
// Use client...
}

To authenticate as an app, using a JWT:

import "github.com/bradleyfalzon/ghinstallation"

func main() {
// Wrap the shared transport for use with the application ID 1.
atr, err := ghinstallation.NewAppsTransportKeyFromFile(http.DefaultTransport, 1, "2016-10-19.private-key.pem")
if err != nil {
// Handle error.
}

// Use app transport with client
client := github.NewClient(&http.Client{Transport: atr})

// Use client...
}

Rate Limiting

GitHub imposes a rate limit on all API clients. Unauthenticated clients are
Expand Down
8 changes: 8 additions & 0 deletions github/github-accessors.go

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

10 changes: 10 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.

3 changes: 3 additions & 0 deletions github/orgs_audit_log.go
4DEA
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type HookConfig struct {
ContentType *string `json:"content_type,omitempty"`
InsecureSSL *string `json:"insecure_ssl,omitempty"`
URL *string `json:"url,omitempty"`

// Secret is returned obfuscated by GitHub, but it can be set for outgoing requests.
Secret *string `json:"secret,omitempty"`
}

// AuditEntry describes the fields that may be represented by various audit-log "action" entries.
Expand Down
0