-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add handling for github enterprise #114
Changes from all commits
4dc8ed8
57ce9f3
51ba4a4
9128ad0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,8 +37,9 @@ type GitHub interface { | |
} | ||
|
||
type GitHubClient struct { | ||
client *github.Client | ||
clientV4 *githubv4.Client | ||
client *github.Client | ||
clientV4 *githubv4.Client | ||
isEnterprise bool | ||
|
||
owner string | ||
repository string | ||
|
@@ -68,6 +69,7 @@ func NewGitHubClient(source Source) (*GitHubClient, error) { | |
client := github.NewClient(httpClient) | ||
|
||
clientV4 := githubv4.NewClient(httpClient) | ||
var isEnterprise bool | ||
|
||
if source.GitHubAPIURL != "" { | ||
var err error | ||
|
@@ -91,10 +93,12 @@ func NewGitHubClient(source Source) (*GitHubClient, error) { | |
v4URL = source.GitHubAPIURL + "graphql" | ||
} | ||
clientV4 = githubv4.NewEnterpriseClient(v4URL, httpClient) | ||
isEnterprise = true | ||
} | ||
|
||
if source.GitHubV4APIURL != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
clientV4 = githubv4.NewEnterpriseClient(source.GitHubV4APIURL, httpClient) | ||
isEnterprise = true | ||
} | ||
|
||
if source.GitHubUploadsURL != "" { | ||
|
@@ -111,16 +115,20 @@ func NewGitHubClient(source Source) (*GitHubClient, error) { | |
} | ||
|
||
return &GitHubClient{ | ||
client: client, | ||
clientV4: clientV4, | ||
owner: owner, | ||
repository: source.Repository, | ||
accessToken: source.AccessToken, | ||
client: client, | ||
clientV4: clientV4, | ||
isEnterprise: isEnterprise, | ||
owner: owner, | ||
repository: source.Repository, | ||
accessToken: source.AccessToken, | ||
}, nil | ||
} | ||
|
||
func (g *GitHubClient) ListReleases() ([]*github.RepositoryRelease, error) { | ||
if g.accessToken != "" { | ||
if g.isEnterprise { | ||
return g.listReleasesV4EnterPrice() | ||
} | ||
return g.listReleasesV4() | ||
} | ||
opt := &github.ListOptions{PerPage: 100} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,84 @@ import ( | |
"context" | ||
"encoding/base64" | ||
"errors" | ||
"github.com/google/go-github/v39/github" | ||
"github.com/shurcooL/githubv4" | ||
"regexp" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/google/go-github/v39/github" | ||
"github.com/shurcooL/githubv4" | ||
) | ||
|
||
func (g *GitHubClient) listReleasesV4EnterPrice() ([]*github.RepositoryRelease, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you already have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know, but since it's a temporary solution, the logic of the method is almost the same, but for every reference I need to use another payload for making request, so I decided just to create new one, instead of huge if-else condition everywhere |
||
if g.clientV4 == nil { | ||
return nil, errors.New("github graphql is not been initialised") | ||
} | ||
var listReleasesEnterprise struct { | ||
Repository struct { | ||
Releases struct { | ||
Edges []struct { | ||
Node struct { | ||
ReleaseObjectEnterprise | ||
} | ||
} `graphql:"edges"` | ||
PageInfo struct { | ||
EndCursor githubv4.String | ||
HasNextPage bool | ||
} `graphql:"pageInfo"` | ||
} `graphql:"releases(first:$releasesCount, after: $releaseCursor, orderBy: {field: CREATED_AT, direction: DESC})"` | ||
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"` | ||
} | ||
|
||
vars := map[string]interface{}{ | ||
"repositoryOwner": githubv4.String(g.owner), | ||
"repositoryName": githubv4.String(g.repository), | ||
"releaseCursor": (*githubv4.String)(nil), | ||
"releasesCount": githubv4.Int(100), | ||
} | ||
|
||
var allReleases []*github.RepositoryRelease | ||
for { | ||
if err := g.clientV4.Query(context.TODO(), &listReleasesEnterprise, vars); err != nil { | ||
return nil, err | ||
} | ||
for _, r := range listReleasesEnterprise.Repository.Releases.Edges { | ||
r := r | ||
publishedAt, _ := time.ParseInLocation(time.RFC3339, r.Node.PublishedAt.Time.Format(time.RFC3339), time.UTC) | ||
createdAt, _ := time.ParseInLocation(time.RFC3339, r.Node.CreatedAt.Time.Format(time.RFC3339), time.UTC) | ||
var releaseID int64 | ||
decodedID, err := base64.StdEncoding.DecodeString(r.Node.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
re := regexp.MustCompile(`.*[^\d]`) | ||
decodedID = re.ReplaceAll(decodedID, []byte("")) | ||
if string(decodedID) == "" { | ||
return nil, errors.New("bad release id from graph ql api") | ||
} | ||
releaseID, err = strconv.ParseInt(string(decodedID), 10, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
allReleases = append(allReleases, &github.RepositoryRelease{ | ||
ID: &releaseID, | ||
TagName: &r.Node.TagName, | ||
Name: &r.Node.Name, | ||
Prerelease: &r.Node.IsPrerelease, | ||
Draft: &r.Node.IsDraft, | ||
URL: &r.Node.URL, | ||
PublishedAt: &github.Timestamp{Time: publishedAt}, | ||
CreatedAt: &github.Timestamp{Time: createdAt}, | ||
}) | ||
} | ||
if !listReleasesEnterprise.Repository.Releases.PageInfo.HasNextPage { | ||
break | ||
} | ||
vars["releaseCursor"] = listReleasesEnterprise.Repository.Releases.PageInfo.EndCursor | ||
|
||
} | ||
return allReleases, nil | ||
} | ||
|
||
func (g *GitHubClient) listReleasesV4() ([]*github.RepositoryRelease, error) { | ||
if g.clientV4 == nil { | ||
return nil, errors.New("github graphql is not been initialised") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is basically saying once
souce.GitHubAPIURL
is provided then it is an enterprise github deployment. Is that true? Could a github deployment be public but not enterprise version?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in this one case I belive if you are providing something what is not default github api, we should consider this as github enterprise but I may be wrong there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at least
isEnterprise = true
should be set wheneverNewEnterpriseClient
is calledThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is what is happening, when we have case with custom url, we are assuming that enterprise client will be used