Skip to content
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

GHES 3.10: Changes in generated code #18

Merged
merged 14 commits into from
Aug 2, 2024
20 changes: 20 additions & 0 deletions .github/workflows/add_to_octokit_project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Add PRs and issues to Octokit org project

on:
issues:
types: [reopened, opened]
pull_request_target:
types: [reopened, opened]

jobs:
add-to-project:
name: Add issue to project
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/[email protected]
with:
project-url: https://github.com/orgs/octokit/projects/10
github-token: ${{ secrets.OCTOKITBOT_PROJECT_ACTION_TOKEN }}
labeled: "Status: Stale"
label-operator: NOT
28 changes: 28 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: "Build and validate Go generated SDK"

on:
pull_request:
workflow_dispatch:
push:
branches:
- 3.10

jobs:
build:
strategy:
fail-fast: false
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- uses: actions/[email protected]
with:
go-version: '1.21.4'

- name: Build the SDK
run: go build ./...

- name: Run unit tests
run: go test ./...
29 changes: 29 additions & 0 deletions .github/workflows/immediate-response.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Issue/PR response
permissions:
issues: write
pull-requests: write
on:
issues:
types:
- opened
pull_request_target:
types:
- opened
jobs:
respond-to-issue:
if: ${{ github.actor != 'dependabot[bot]' && github.actor != 'renovate[bot]' && github.actor != 'githubactions[bot]' && github.actor != 'octokitbot' }}
runs-on: ubuntu-latest
steps:
- name: Determine issue or PR number
id: extract
run: echo "NUMBER=${{ github.event.issue.number || github.event.pull_request.number }}" >> "$GITHUB_OUTPUT"

- name: Respond to issue or PR
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ steps.extract.outputs.NUMBER }}
body: >
👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday!
We have a [process in place](https://github.com/octokit/.github/blob/main/community/prioritization_response.md#overview) for prioritizing and responding to your input.
Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with `Status: Up for grabs`.
You & others like you are the reason all of this works! So thank you & happy coding! 🚀
32 changes: 32 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: golangci-lint
on:
push:
branches:
- main
pull_request:

permissions:
contents: read
checks: write

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.21'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.55

# Optional: golangci-lint command line arguments.
#
# Note: By default, the `.golangci.yml` file should be at the root of the repository.
# The location of the configuration file can be changed by using `--config=`
# args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0

2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"editor.tabSize": 4,
"editor.formatOnSave": true
}
}
}
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
# github.com/octokit/go-sdk-enterprise-server

An "alpha" version of a generated Go SDK from [GitHub's OpenAPI spec](https://github.com/github/rest-api-description), built on [Kiota](https://github.com/microsoft/kiota).
An "alpha" version of a generated Go SDK for GitHub's Enterprise Server products, generated from [GitHub's OpenAPI spec](https://github.com/github/rest-api-description), built on [Kiota](https://github.com/microsoft/kiota).

You may also want:

- Go
- For the standard GitHub.com product
- [go-sdk repository](https://github.com/octokit/go-sdk)
- [pkg.go.dev docs link](https://pkg.go.dev/github.com/octokit/go-sdk)
- For GitHub Enterprise Cloud
- [go-sdk-enterprise-cloud repository](https://github.com/octokit/go-sdk-enterprise-cloud)
- [pkg.go.dev docs link](https://pkg.go.dev/github.com/octokit/go-sdk-enterprise-cloud)
- For GitHub Enterprise Server
- [go-sdk-enterprise-server repository](https://github.com/octokit/go-sdk-enterprise-server)
- [pkg.go.dev docs link](https://pkg.go.dev/github.com/octokit/go-sdk-enterprise-server)
- .NET
- For the standard GitHub.com product
- [dotnet-sdk repository](https://github.com/octokit/dotnet-sdk)
- [NuGet link](https://www.nuget.org/packages/GitHub.Octokit.SDK)
- For GitHub Enterprise Cloud
- [dotnet-sdk-enterprise-cloud repository](https://github.com/octokit/dotnet-sdk-enterprise-cloud)
- For GitHub Enterprise Server
- [dotnet-sdk-enterprise-server repository](https://github.com/octokit/dotnet-sdk-enterprise-server)
- For our classic non-generated, hand-maintained Octokit.net project
- [Octokit.net repository](https://github.com/octokit/octokit.net)
- For why we're building generative SDKs, see [Why a generated SDK?](#why-a-generated-sdk) below
- [source-generator](https://github.com/octokit/source-generator) (the repository that creates these generated SDKs)
- Contributions to this repository should take place in source-generator instead, as they'll be distributed here through mechanisms there.

## How do I use it?

Expand All @@ -16,6 +42,33 @@ See example client instantiations and requests in [example_test.go](pkg/example_
- Test coverage may be viewed in VS Code by running the command `Go: Toggle Test Coverage In Current Package`
- Alternately, you may run `go tool cover -html auth.cov -o auth.html` and open the generated `auth.html` file in a browser to view test coverage

### Initializing

Given that the GHES platform is a self hosted instance when using this SDK you'll need to initialize it with your host and protocol:

```go
client, err := pkg.NewApiClient(
pkg.WithUserAgent("octokit/go-sdk.example-functions"),
pkg.WithRequestTimeout(5*time.Second),
pkg.WithBaseUrl("https://hosted.instance"),
pkg.WithTokenAuthentication(token),
)
```

or by using the `SetBaseUrl` function from the `kiotaHttp.NewNetHttpRequestAdapter`

```go
tokenProvider := auth.NewTokenProvider(
auth.WithUserAgent("octokit/go-sdk.example-functions"),
)
adapter, err := kiotaHttp.NewNetHttpRequestAdapter(tokenProvider)
if err != nil {
log.Fatalf("Error creating request adapter: %v", err)
}
adapter.SetBaseUrl("https://hosted.instance")
client := github.NewApiClient(adapter)
```

### Authentication

This SDK supports [Personal Access Tokens (classic)](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#personal-access-tokens-classic), [fine-grained Personal Access Tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#fine-grained-personal-access-tokens), and [GitHub Apps](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/about-authentication-with-a-github-app) authentication.
Expand Down
18 changes: 10 additions & 8 deletions pkg/authentication/token_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"os"
"strings"
"testing"
"time"

abstractions "github.com/microsoft/kiota-abstractions-go"
http "github.com/microsoft/kiota-http-go"
"github.com/octokit/go-sdk-enterprise-server/pkg"
"github.com/octokit/go-sdk-enterprise-server/pkg/authentication"
"github.com/octokit/go-sdk-enterprise-server/pkg/github"
"github.com/octokit/go-sdk-enterprise-server/pkg/github/user"
"github.com/octokit/go-sdk-enterprise-server/pkg/headers"
)
Expand Down Expand Up @@ -155,19 +155,21 @@ func TestHappyPathIntegration(t *testing.T) {
t.Skip("in order to run integration tests, ensure a valid GITHUB_TOKEN exists in the environment")
}

provider := authentication.NewTokenProvider(
authentication.WithTokenAuthentication(token),
// TODO: Rework this test to fit the platform needs of GHES
client, err := pkg.NewApiClient(
pkg.WithUserAgent("octokit/go-sdk.example-functions"),
pkg.WithRequestTimeout(5*time.Second),
pkg.WithBaseUrl("https://api.github.com"),
pkg.WithTokenAuthentication(token),
)

adapter, err := http.NewNetHttpRequestAdapter(provider)
if err != nil {
log.Fatalf("Error creating request adapter: %v", err)
log.Fatalf("error creating client: %v", err)
}

headers := abstractions.NewRequestHeaders()
_ = headers.TryAdd("Accept", "application/vnd.github.v3+json")

client := github.NewApiClient(adapter)

// Create a new instance of abstractions.RequestConfiguration
requestConfig := &abstractions.RequestConfiguration[user.EmailsRequestBuilderGetQueryParameters]{
Headers: headers,
Expand Down
16 changes: 12 additions & 4 deletions pkg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import (
// Client (wrapper of *github.ApiClient) with the provided option functions.
// By default, it includes a rate limiting middleware.
func NewApiClient(optionFuncs ...ClientOptionFunc) (*Client, error) {
options := GetDefaultClientOptions()
options, err := GetDefaultClientOptions()
if err != nil {
return nil, fmt.Errorf("failed to get default client options: %v", err)
}
for _, optionFunc := range optionFuncs {
optionFunc(options)
}
Expand Down Expand Up @@ -118,12 +121,17 @@ type ClientOptions struct {

// GetDefaultClientOptions returns a new instance of ClientOptions with default values.
// This is used in the NewApiClient constructor before applying user-defined custom options.
func GetDefaultClientOptions() *ClientOptions {
func GetDefaultClientOptions() (*ClientOptions, error) {
turnOffCompression := kiotaHttp.NewCompressionOptions(false)
middlewares, err := kiotaHttp.GetDefaultMiddlewaresWithOptions(&turnOffCompression)
if err != nil {
return nil, fmt.Errorf("failed to get middleware with compression turned off: %v", err)
}
return &ClientOptions{
UserAgent: "octokit/go-sdk",
APIVersion: "2022-11-28",
Middleware: kiotaHttp.GetDefaultMiddlewares(),
}
Middleware: middlewares,
}, nil
}

// ClientOptionFunc provides a functional pattern for client configuration
Expand Down
9 changes: 9 additions & 0 deletions pkg/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ func ExampleApiClient_Octocat_withoutConvenienceConstructor() {
log.Fatalf("Error creating request adapter: %v", err)
}

// TODO: Rework this test to fit the platform needs of GHES
baseUrl := adapter.GetBaseUrl()

// Note: This SDK should be used against a GitHub Enterprise instance, and the below URL is the public GitHub one. It's here only so that tests pass when running `go test ./...`, as the OpenAPI schema for GHES understandably does not include a baseURL.
// When setting up this package for your own usage, call `adapter.SetBaseUrl` or `pkg.WithBaseUrl` with your own GHES base URL.
if baseUrl == "" {
adapter.SetBaseUrl("https://api.github.com")
}

client := github.NewApiClient(adapter)

s := "Salutations"
Expand Down
Loading
Loading