Skip to content

Commit

Permalink
Support GitHub Enterprise (srvaroa#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
srvaroa authored Jun 16, 2023
1 parent c792f04 commit 425adeb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ configuration files to benefit from newer features. Please follow our
[releases](https://github.com/srvaroa/labeler/releases) page to stay up
to date.

### GitHub Enterprise support

Add `GITHUB_API_HOST` to your env variables, it should be in the form
`http(s)://[hostname]/`

Please consider [https://github.com/sponsors/srvaroa](sponsoring the
project) if you're using Labeler in your organization!

### How to trigger action

To trigger the action on events, add a file `.github/workflows/main.yml`
Expand Down
21 changes: 17 additions & 4 deletions cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import (

func main() {

gh := getGithubClient()
gh, err := getGithubClient()
if err != nil {
log.Printf("Failed to retrieve a GitHub client: %+v", err)
return
}
eventPayload := getEventPayload()
eventName := os.Getenv("GITHUB_EVENT_NAME")

Expand Down Expand Up @@ -128,14 +132,23 @@ func getLabelerConfigV0(configRaw *[]byte) (labeler.LabelerConfigV1, error) {
}, err
}

func getGithubClient() *github.Client {
func getGithubClient() (*github.Client, error) {
ghToken := os.Getenv("GITHUB_TOKEN")
ghApiHost := os.Getenv("GITHUB_API_HOST")
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ghToken},
)
tc := oauth2.NewClient(ctx, ts)
return github.NewClient(tc)

if len(ghApiHost) == 0 {
log.Printf("Connecting to GitHub.com")
tc := oauth2.NewClient(ctx, ts)
return github.NewClient(tc), nil
} else {
log.Printf("Connecting to enterprise server at: %s", ghApiHost)
tc := oauth2.NewClient(ctx, ts)
return github.NewEnterpriseClient(ghApiHost, ghApiHost, tc)
}
}

func getEventPayload() *[]byte {
Expand Down

0 comments on commit 425adeb

Please sign in to comment.