From 0dc80ad3466aca91e3bdad4ad1f9bb65fba7d329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Santos=20Ara=C3=BAjo?= Date: Fri, 26 Jul 2024 16:05:07 -0300 Subject: [PATCH 1/7] creating app config --- scrape/apps.go | 32 ++++++++++++++++++++++++++++++-- scrape/apps_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/scrape/apps.go b/scrape/apps.go index 0028a5724f8..00014ca6ba4 100644 --- a/scrape/apps.go +++ b/scrape/apps.go @@ -12,6 +12,7 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "net/http" "strconv" "strings" @@ -133,13 +134,40 @@ type AppManifest struct { DefaultPermissions *github.InstallationPermissions `json:"default_permissions,omitempty"` } +type OwnerType string + +const ( + OwnerTypePersonal OwnerType = "personal" + OwnerTypeOrganization OwnerType = "organizational" +) + +type AppConfig struct { + OwnerType OwnerType + OrgName string +} + // 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") +// If the account type is "organizational", the path is set to "/organizations/{orgName}/settings/apps/new", +// where {orgName} is the name of the organization specified in the AppConfig. +// Otherwise, the path is set to "/settings/apps/new". + +func (c *Client) CreateApp(m *AppManifest, accType *AppConfig) (*http.Response, error) { + + path := "/settings/apps/new" + + if accType.OwnerType == "organizational" { + path = fmt.Sprintf("/organizations/%s/settings/apps/new", accType.OrgName) + } + + fmt.Println(path) + + u, err := c.baseURL.Parse(path) if err != nil { return nil, err } + fmt.Println(u) + body, err := json.Marshal(map[string]*AppManifest{"manifest": m}) if err != nil { return nil, err diff --git a/scrape/apps_test.go b/scrape/apps_test.go index 339c685f98e..cf086580cde 100644 --- a/scrape/apps_test.go +++ b/scrape/apps_test.go @@ -97,7 +97,31 @@ func Test_CreateApp(t *testing.T) { HookAttributes: map[string]string{ "url": "https://example.com/hook", }, + }, &AppConfig{ + OwnerType: OwnerTypePersonal, }); 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", + }, + }, &AppConfig{ + OwnerType: OwnerTypeOrganization, + OrgName: "example", + }); err != nil { + t.Fatalf("CreateAppWithOrg: %v", err) + } +} From 4e72d20895d138c24ed30db65b7f109aa0a06fb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Santos=20Ara=C3=BAjo?= Date: Sun, 28 Jul 2024 01:32:01 -0300 Subject: [PATCH 2/7] fix linting --- scrape/apps.go | 14 ++++---------- scrape/scrape.go | 1 + 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/scrape/apps.go b/scrape/apps.go index 00014ca6ba4..411a1f8649f 100644 --- a/scrape/apps.go +++ b/scrape/apps.go @@ -142,33 +142,27 @@ const ( ) type AppConfig struct { + // OwnerType specifies the type of GitHub account owner (personal or organizational). OwnerType OwnerType - OrgName string + // OrgName is the name of the organization if the OwnerType is organizational. + OrgName string } // CreateApp creates a new GitHub App with the given manifest configuration. -// If the account type is "organizational", the path is set to "/organizations/{orgName}/settings/apps/new", -// where {orgName} is the name of the organization specified in the AppConfig. -// Otherwise, the path is set to "/settings/apps/new". - func (c *Client) CreateApp(m *AppManifest, accType *AppConfig) (*http.Response, error) { - path := "/settings/apps/new" if accType.OwnerType == "organizational" { path = fmt.Sprintf("/organizations/%s/settings/apps/new", accType.OrgName) } - fmt.Println(path) - u, err := c.baseURL.Parse(path) if err != nil { return nil, err } - fmt.Println(u) - body, err := json.Marshal(map[string]*AppManifest{"manifest": m}) + if err != nil { return nil, err } diff --git a/scrape/scrape.go b/scrape/scrape.go index dc7aa88fad4..49fece02fd3 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -128,6 +128,7 @@ func (c *Client) Authenticate(username, password, otpseed string) error { values.Set("password", password) } resp, err := fetchAndSubmitForm(c.Client, "https://github.com/login", setPassword) + if err != nil { return err } From c5f2be5f328631e7fc41902a23ffb9e1a9aca1c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Santos=20Ara=C3=BAjo?= Date: Mon, 29 Jul 2024 17:53:50 -0300 Subject: [PATCH 3/7] feat: Refactor CreateApp function to use appConfig parameter instead of accType --- scrape/apps.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/scrape/apps.go b/scrape/apps.go index 411a1f8649f..3e1fa414483 100644 --- a/scrape/apps.go +++ b/scrape/apps.go @@ -149,11 +149,11 @@ type AppConfig struct { } // CreateApp creates a new GitHub App with the given manifest configuration. -func (c *Client) CreateApp(m *AppManifest, accType *AppConfig) (*http.Response, error) { +func (c *Client) CreateApp(m *AppManifest, appConfig *AppConfig) (*http.Response, error) { path := "/settings/apps/new" - if accType.OwnerType == "organizational" { - path = fmt.Sprintf("/organizations/%s/settings/apps/new", accType.OrgName) + if appConfig.OwnerType == "organizational" { + path = fmt.Sprintf("/organizations/%s/settings/apps/new", appConfig.OrgName) } u, err := c.baseURL.Parse(path) @@ -162,7 +162,6 @@ func (c *Client) CreateApp(m *AppManifest, accType *AppConfig) (*http.Response, } body, err := json.Marshal(map[string]*AppManifest{"manifest": m}) - if err != nil { return nil, err } From ed9925e8b1ef417a9bde6e505d013d1247a9c38a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Santos=20Ara=C3=BAjo?= Date: Mon, 29 Jul 2024 17:59:57 -0300 Subject: [PATCH 4/7] chaging variable name --- scrape/apps.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/scrape/apps.go b/scrape/apps.go index 3e1fa414483..f5c3b600b88 100644 --- a/scrape/apps.go +++ b/scrape/apps.go @@ -148,12 +148,11 @@ type AppConfig struct { OrgName string } -// CreateApp creates a new GitHub App with the given manifest configuration. -func (c *Client) CreateApp(m *AppManifest, appConfig *AppConfig) (*http.Response, error) { +func (c *Client) CreateApp(m *AppManifest, ac *AppConfig) (*http.Response, error) { path := "/settings/apps/new" - if appConfig.OwnerType == "organizational" { - path = fmt.Sprintf("/organizations/%s/settings/apps/new", appConfig.OrgName) + if ac.OwnerType == "organizational" { + path = fmt.Sprintf("/organizations/%s/settings/apps/new", ac.OrgName) } u, err := c.baseURL.Parse(path) From d4e8a7c73ca7920e4b196cf4e2d37a386eb0d254 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Santos=20Ara=C3=BAjo?= Date: Mon, 29 Jul 2024 18:58:14 -0300 Subject: [PATCH 5/7] Update scrape/apps.go Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- scrape/apps.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scrape/apps.go b/scrape/apps.go index f5c3b600b88..99a42a64812 100644 --- a/scrape/apps.go +++ b/scrape/apps.go @@ -149,7 +149,7 @@ type AppConfig struct { } func (c *Client) CreateApp(m *AppManifest, ac *AppConfig) (*http.Response, error) { - path := "/settings/apps/new" + url := "/settings/apps/new" if ac.OwnerType == "organizational" { path = fmt.Sprintf("/organizations/%s/settings/apps/new", ac.OrgName) From a2f4cfaf9351c00d5f977846327a2069163fcd3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Santos=20Ara=C3=BAjo?= Date: Mon, 29 Jul 2024 18:58:38 -0300 Subject: [PATCH 6/7] Update scrape/apps.go Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- scrape/apps.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scrape/apps.go b/scrape/apps.go index 99a42a64812..26eff78b0f5 100644 --- a/scrape/apps.go +++ b/scrape/apps.go @@ -152,7 +152,7 @@ func (c *Client) CreateApp(m *AppManifest, ac *AppConfig) (*http.Response, error url := "/settings/apps/new" if ac.OwnerType == "organizational" { - path = fmt.Sprintf("/organizations/%s/settings/apps/new", ac.OrgName) + path = fmt.Sprintf("/organizations/%v/settings/apps/new", ac.OrgName) } u, err := c.baseURL.Parse(path) From d6cf6fa4ae497500b50e00969ea44cc062c9df41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Santos=20Ara=C3=BAjo?= Date: Mon, 29 Jul 2024 19:27:23 -0300 Subject: [PATCH 7/7] Refactor CreateApp function to use orgName parameter instead of appConfig --- scrape/apps.go | 24 ++++++------------------ scrape/apps_test.go | 9 ++------- scrape/scrape.go | 1 - 3 files changed, 8 insertions(+), 26 deletions(-) diff --git a/scrape/apps.go b/scrape/apps.go index 26eff78b0f5..9f23227661e 100644 --- a/scrape/apps.go +++ b/scrape/apps.go @@ -134,28 +134,16 @@ type AppManifest struct { DefaultPermissions *github.InstallationPermissions `json:"default_permissions,omitempty"` } -type OwnerType string - -const ( - OwnerTypePersonal OwnerType = "personal" - OwnerTypeOrganization OwnerType = "organizational" -) - -type AppConfig struct { - // OwnerType specifies the type of GitHub account owner (personal or organizational). - OwnerType OwnerType - // OrgName is the name of the organization if the OwnerType is organizational. - OrgName string -} - -func (c *Client) CreateApp(m *AppManifest, ac *AppConfig) (*http.Response, error) { +// CreateApp creates a new GitHub App with the given manifest configuration. +// 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 ac.OwnerType == "organizational" { - path = fmt.Sprintf("/organizations/%v/settings/apps/new", ac.OrgName) + if orgName != "" { + url = fmt.Sprintf("/organizations/%v/settings/apps/new", orgName) } - u, err := c.baseURL.Parse(path) + 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 cf086580cde..2b88716b25e 100644 --- a/scrape/apps_test.go +++ b/scrape/apps_test.go @@ -97,9 +97,7 @@ func Test_CreateApp(t *testing.T) { HookAttributes: map[string]string{ "url": "https://example.com/hook", }, - }, &AppConfig{ - OwnerType: OwnerTypePersonal, - }); err != nil { + }, ""); err != nil { t.Fatalf("CreateApp: %v", err) } } @@ -118,10 +116,7 @@ func Test_CreateAppWithOrg(t *testing.T) { HookAttributes: map[string]string{ "url": "https://example.com/hook", }, - }, &AppConfig{ - OwnerType: OwnerTypeOrganization, - OrgName: "example", - }); err != nil { + }, "example"); err != nil { t.Fatalf("CreateAppWithOrg: %v", err) } } diff --git a/scrape/scrape.go b/scrape/scrape.go index 49fece02fd3..dc7aa88fad4 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -128,7 +128,6 @@ func (c *Client) Authenticate(username, password, otpseed string) error { values.Set("password", password) } resp, err := fetchAndSubmitForm(c.Client, "https://github.com/login", setPassword) - if err != nil { return err }