diff --git a/scrape/apps.go b/scrape/apps.go index 0028a5724f8..9f23227661e 100644 --- a/scrape/apps.go +++ b/scrape/apps.go @@ -12,6 +12,7 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "net/http" "strconv" "strings" @@ -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 } diff --git a/scrape/apps_test.go b/scrape/apps_test.go index 339c685f98e..2b88716b25e 100644 --- a/scrape/apps_test.go +++ b/scrape/apps_test.go @@ -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) + } +}