Commit 575c0cc6 authored by Timo Furrer's avatar Timo Furrer Committed by Patrick Rice
Browse files

feat(config): initial push of the ability to use a config file for auth

Changelog: Improvements
parent d5a0677e
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -36,3 +36,5 @@ vendor

# reports
gl-code-quality-report.json

.mise/
+27 −6
Original line number Diff line number Diff line
@@ -44,16 +44,28 @@ stages:
  variables:
    GOPATH: $CI_PROJECT_DIR/.go
    GOLANGCI_LINT_CACHE: $CI_PROJECT_DIR/.golangci-lint
    MISE_VERSION: v2025.6.1
    MISE_DATA_DIR: $CI_PROJECT_DIR/.mise/mise-data
  before_script:
    - mkdir -p "${GOPATH}" "${GOLANGCI_LINT_CACHE}"
    - export PATH="${GOPATH}/bin:$PATH"
    # Install mise
    - curl --retry 6 https://mise.run | MISE_VERSION=${MISE_VERSION} MISE_INSTALL_PATH=/usr/local/bin/mise sh
    - eval "$(mise activate bash --shims)"
    - mise install
  cache:
    - key:
        files:
          - go.sum
      paths:
        - $GOPATH/pkg/mod/
        - $GOLANGCI_LINT_CACHE/
    key:
      files:
        - go.sum
    - key:
        prefix: mise-
        files: ['.tool-versions']
      paths:
        - $MISE_DATA_DIR

  # We only need to run Go-related jobs when actual Go files changed
  # or when running either on the default branch or for a tag.
  rules:
@@ -83,6 +95,15 @@ golangci-lint:
    paths: [$REPORT_FILENAME]
    when: always

buf-lint:
  extends:
    - .go:base
  stage: lint
  needs: []
  script:
    - buf format --exit-code
    - buf lint

verify-generated-code:
  extends:
    - .go:base
+3 −1
Original line number Diff line number Diff line
golang        1.23
golangci-lint 2.1.6
gofumpt       0.8.0
buf           1.55.1
+8 −10
Original line number Diff line number Diff line
@@ -8,24 +8,22 @@ help: ## Display this help

reviewable: setup generate fmt lint test ## Run before committing.

fmt: install-gofumpt ## Format code
fmt: ## Format code
	@buf format -w
	@gofumpt -l -w *.go testing/*.go examples/*.go

lint: install-golangci-lint ## Run linter
lint: Run linter
	@golangci-lint run
	@buf format --exit-code
	@buf lint

.PHONY: setup
setup: install-golangci-lint install-gofumpt ## Setup your local environment
setup: ## Setup your local environment
	go mod tidy

install-golangci-lint:
	@go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest

install-gofumpt:
	@go install mvdan.cc/gofumpt@latest

.PHONY: generate
generate: install-gofumpt ## Generate files
generate: ## Generate files
	buf generate # install from .tool-versions
	./scripts/generate_testing_client.sh
	./scripts/generate_service_interface_map.sh
	./scripts/generate_mock_api.sh
+86 −0
Original line number Diff line number Diff line
@@ -139,6 +139,92 @@ func main() {
}
```

#### Use the `config` package (experimental)

The `config` package defines a configuration file format (YAML) to configure GitLab instances
and their associated authentication methods combined in contexts (similar to what you might know from Kubernetes).

The configuration is located in the users config directory (e.g. XDG config dir), in `gitlab/config.yaml`.

A basic example for an OAuth flow for GitLab.com that stores the credentials in the systems keyring, looks like this:

```yaml
version: gitlab.com/config/v1beta1

instances:
    - name: gitlab-com
      server: https://gitlab.com

auths:
    - name: oauth-keyring
      auth-info:
        oauth2:
            access-token-source:
                keyring:
                    service: client-go
                    user: access-token
            refresh-token-source:
                keyring:
                    service: client-go
                    user: refresh-token
contexts:
    - name: gitlab-com-keyring
      instance: gitlab-com
      auth: oauth-keyring

current-context: gitlab-com-keyring
```

An application with `client-go` is able to effortlessly create a new client using that configuration:

```go
package main

import (
	"fmt"
	"log"

	"gitlab.com/gitlab-org/api/client-go"
	"gitlab.com/gitlab-org/api/client-go/config"
)

func main() {
	// Create a config with default location (~/.config/gitlab/config.yaml)
	cfg := config.New(
		config.WithOAuth2Settings(config.OAuth2Settings{
			AuthorizationFlowEnabled: true,
			CallbackServerListenAddr: ":7171",
			Browser: func(url string) error {
				fmt.Printf("Open: %s\n", url)
				return nil
			},
			ClientID:    "<your-client-id>",
			RedirectURL: "http://localhost:7171/auth/redirect",
			Scopes:      []string{"read_api"},
		}),
	)

	// Load the configuration
	if err := cfg.Load(); err != nil {
		log.Printf("Failed to load config: %v", err)
		return
	}

	client, err := cfg.NewClient(gitlab.WithUserAgent("my-app"))
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	}

	// Use the client
	user, _, err := client.Users.CurrentUser()
	if err != nil {
		log.Fatalf("Failed to get current user: %v", err)
	}

	fmt.Printf("Authenticated as: %s (%s)\n", user.Name, user.Username)
}
```

For complete usage of go-gitlab, see the full [package docs](https://godoc.org/gitlab.com/gitlab-org/api/client-go).

## Installation
Loading