/
org.go
42 lines (31 loc) · 915 Bytes
/
org.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// SPDX-License-Identifier: Apache-2.0
package github
import (
"context"
"net/http"
"github.com/sirupsen/logrus"
"github.com/go-vela/types/library"
)
// GetOrgName gets org name from Github.
func (c *client) GetOrgName(ctx context.Context, 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
}