-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into chore/v0.15.0-rc1
- Loading branch information
Showing
8 changed files
with
398 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) 2022 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
package github | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/go-vela/types/library" | ||
) | ||
|
||
// GetOrgName gets org name from Github. | ||
func (c *client) GetOrgName(u *library.User, o string) (string, error) { | ||
c.Logger.WithFields(logrus.Fields{ | ||
"org": o, | ||
"user": u.GetName(), | ||
}).Tracef("retrieving org information for %s", o) | ||
|
||
// create GitHub OAuth client with user's token | ||
client := c.newClientToken(u.GetToken()) | ||
|
||
// send an API call to get the org info | ||
orgInfo, resp, err := client.Organizations.Get(ctx, o) | ||
|
||
orgName := orgInfo.GetLogin() | ||
|
||
// if org is not found, return the personal org | ||
if resp.StatusCode == http.StatusNotFound { | ||
user, _, err := client.Users.Get(ctx, "") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
orgName = user.GetLogin() | ||
} else if err != nil { | ||
return "", err | ||
} | ||
|
||
return orgName, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright (c) 2022 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
package github | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
"github.com/go-vela/types/library" | ||
) | ||
|
||
func TestGithub_GetOrgName(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
resp := httptest.NewRecorder() | ||
_, engine := gin.CreateTestContext(resp) | ||
|
||
// setup mock server | ||
engine.GET("/api/v3/orgs/:org", func(c *gin.Context) { | ||
c.Header("Content-Type", "application/json") | ||
c.Status(http.StatusOK) | ||
c.File("testdata/get_org.json") | ||
}) | ||
|
||
s := httptest.NewServer(engine) | ||
defer s.Close() | ||
|
||
// setup types | ||
u := new(library.User) | ||
u.SetName("foo") | ||
u.SetToken("bar") | ||
|
||
want := "github" | ||
|
||
client, _ := NewTest(s.URL) | ||
|
||
// run test | ||
got, err := client.GetOrgName(u, "github") | ||
|
||
if resp.Code != http.StatusOK { | ||
t.Errorf("GetOrgName returned %v, want %v", resp.Code, http.StatusOK) | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("GetOrgName returned err: %v", err) | ||
} | ||
|
||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("GetOrgName is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestGithub_GetOrgName_Personal(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
resp := httptest.NewRecorder() | ||
_, engine := gin.CreateTestContext(resp) | ||
|
||
// setup mock server | ||
engine.GET("/api/v3/user", func(c *gin.Context) { | ||
c.Header("Content-Type", "application/json") | ||
c.Status(http.StatusOK) | ||
c.File("testdata/user.json") | ||
}) | ||
|
||
s := httptest.NewServer(engine) | ||
defer s.Close() | ||
|
||
// setup types | ||
u := new(library.User) | ||
u.SetName("foo") | ||
u.SetToken("bar") | ||
|
||
want := "octocat" | ||
|
||
client, _ := NewTest(s.URL) | ||
|
||
// run test | ||
got, err := client.GetOrgName(u, "octocat") | ||
|
||
if resp.Code != http.StatusOK { | ||
t.Errorf("GetOrgName returned %v, want %v", resp.Code, http.StatusOK) | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("GetOrgName returned err: %v", err) | ||
} | ||
|
||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("GetOrgName is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestGithub_GetOrgName_Fail(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
resp := httptest.NewRecorder() | ||
_, engine := gin.CreateTestContext(resp) | ||
|
||
// setup mock server | ||
engine.GET("/api/v3/orgs/:org", func(c *gin.Context) { | ||
c.Header("Content-Type", "application/json") | ||
c.Status(http.StatusNotFound) | ||
}) | ||
|
||
s := httptest.NewServer(engine) | ||
defer s.Close() | ||
|
||
// setup types | ||
u := new(library.User) | ||
u.SetName("foo") | ||
u.SetToken("bar") | ||
|
||
client, _ := NewTest(s.URL) | ||
|
||
// run test | ||
_, err := client.GetOrgName(u, "octocat") | ||
|
||
if err == nil { | ||
t.Error("GetOrgName should return error") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.