Skip to content

Commit

Permalink
feat!: Add support for creating GitHub App with organizations (#3222)
Browse files Browse the repository at this point in the history
Fixes: #3210.

BREAKING CHANGE: The CreateApp function now requires two arguments: `AppManifest` and `orgName`, to support creating apps with organizations.
  • Loading branch information
wenked authored Jul 29, 2024
1 parent 7f7f194 commit f8929b8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
12 changes: 10 additions & 2 deletions scrape/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -134,8 +135,15 @@ type AppManifest struct {
}

// CreateApp creates a new GitHub App with the given manifest configuration.
func (c *Client) CreateApp(m *AppManifest) (*http.Response, error) {
u, err := c.baseURL.Parse("/settings/apps/new")
// orgName is optional, and if provided, the App will be created within the specified organization.
func (c *Client) CreateApp(m *AppManifest, orgName string) (*http.Response, error) {
url := "/settings/apps/new"

if orgName != "" {
url = fmt.Sprintf("/organizations/%v/settings/apps/new", orgName)
}

u, err := c.baseURL.Parse(url)
if err != nil {
return nil, err
}
Expand Down
21 changes: 20 additions & 1 deletion scrape/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,26 @@ func Test_CreateApp(t *testing.T) {
HookAttributes: map[string]string{
"url": "https://example.com/hook",
},
}); err != nil {
}, ""); err != nil {
t.Fatalf("CreateApp: %v", err)
}
}

func Test_CreateAppWithOrg(t *testing.T) {
client, mux, cleanup := setup()

defer cleanup()

mux.HandleFunc("/organizations/example/apps/settings/new", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
})

if _, err := client.CreateApp(&AppManifest{
URL: github.String("https://example.com"),
HookAttributes: map[string]string{
"url": "https://example.com/hook",
},
}, "example"); err != nil {
t.Fatalf("CreateAppWithOrg: %v", err)
}
}

0 comments on commit f8929b8

Please sign in to comment.