Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ It starts as a collection of utility commands, implemented in Go.
Use them to interact with GitHub, Konflux & co.

A minimum required version can be enforced in case of incompatible changes by bumping [MINIMUM_REQUIRED_VERSION](./MINIMUM_REQUIRED_VERSION).
relacs requires network access to GitHub and `GH_TOKEN` variable set on every command execution by design to verify the minimum required version.
relacs requires network access to GitHub for every command execution to verify the minimum required version.
You may set `GH_TOKEN` if the version check is rate-limited by GitHub.

## Prerequisites

Expand Down Expand Up @@ -49,15 +50,15 @@ This places the `relacs` binary into your `$GOPATH/bin`. Make sure that director

## Configuration

* `GH_TOKEN`: A personal GitHub token. Run:
* `GH_TOKEN`: A personal GitHub token, required by commands that interact with GitHub. Run:
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
```bash
export GH_TOKEN=$(gh auth token)
```
* `JIRA_EMAIL`. An email from Jira account. Run:
* `JIRA_EMAIL`. An email from Jira account, required by commands that interact with Jira. Run:
```bash
export JIRA_EMAIL="<YOUR_EMAIL>"
```
* `JIRA_TOKEN`. A personal API Jira token. Can be created on the [API tokens page](https://id.atlassian.com/manage-profile/security/api-tokens). Run:
* `JIRA_TOKEN`. A personal API Jira token, required by commands that interact with Jira. Can be created on the [API tokens page](https://id.atlassian.com/manage-profile/security/api-tokens). Run:
```bash
export JIRA_TOKEN="<YOUR_JIRA_API_TOKEN>"
```
Expand Down
5 changes: 1 addition & 4 deletions cmd/relacs/common/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ func RunCommand(run RunCommandFunc) func(cmd *cobra.Command, args []string) erro
return err
}

c, err := client.NewClient()
if err != nil {
return err
}
c := client.NewClient()

output, err := run(cmd, args, c)
var partial *PartialResultError
Expand Down
17 changes: 4 additions & 13 deletions cmd/relacs/github/check_release_pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,9 @@ func runCheckReleasePRCommand(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("unknown flag: --output")
}

c, err := client.NewClient()
if err != nil {
return err
}
c := client.NewClient()

_, err = RunReleasePRCheck(cmd, c)
_, err := RunReleasePRCheck(cmd, c)
if err != nil {
return err
}
Expand Down Expand Up @@ -233,13 +230,7 @@ func validatePullRequest(cmd *cobra.Command, client *client.Client, org, repo st
return out, nil
}

// Check if Jira client is configured
jiraClient := client.GetJira()
if jiraClient == nil {
return out, fmt.Errorf("jira client is not configured (JIRA_TOKEN environment variable is required)")
}

expectedFixVersion, err := jiraClient.GetLowestUnreleasedFixVersion(major, minor)
expectedFixVersion, err := client.GetJira().GetLowestUnreleasedFixVersion(major, minor)
if err != nil {
if errors.Is(err, jira.ErrNoFixVersion) {
out.Errors = append(out.Errors, fmt.Sprintf("failed to get lowest unreleased fix version: %v", err))
Expand All @@ -257,7 +248,7 @@ func validatePullRequest(cmd *cobra.Command, client *client.Client, org, repo st
}

for _, issueKey := range issueKeys {
issue, err := jiraClient.GetIssueFromKey(issueKey)
issue, err := client.GetJira().GetIssueFromKey(issueKey)
if err != nil {
var apiErr *jira.APIError
if errors.As(err, &apiErr) && !apiErr.IsServerError() &&
Expand Down
8 changes: 1 addition & 7 deletions cmd/relacs/konflux/prepare_stage_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,12 @@ func runPrepareStageRelease(cmd *cobra.Command, args []string, c *client.Client)
tag, _ := cmd.Flags().GetString("tag")
attempt, _ := cmd.Flags().GetInt("attempt")

// Fetch issues and CVEs from Jira
jiraClient := c.GetJira()
if jiraClient == nil {
return nil, fmt.Errorf("jira client not configured. Please set JIRA_EMAIL and JIRA_TOKEN environment variables")
}

snapshot, err := konflux.GetCustomSnapshot(ctx, c, applicationPrefix, org, repo, tag)
if err != nil {
return nil, fmt.Errorf("failed to get custom snapshot: %w", err)
}

releaseData, err := jiraClient.FetchReleaseData(snapshot)
releaseData, err := c.GetJira().FetchReleaseData(snapshot)
if err != nil {
return nil, fmt.Errorf("failed to fetch release data from Jira: %w", err)
}
Expand Down
55 changes: 19 additions & 36 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
package client

import (
"fmt"
"net/http"
"os"

"github.com/google/go-github/v79/github"
gh "github.com/google/go-github/v79/github"
"github.com/stackrox/relacs/pkg/client/github"
"github.com/stackrox/relacs/pkg/client/jira"
"github.com/stackrox/relacs/pkg/client/konflux"
)

// Client is a base client that wraps the GitHub client with authentication
// Client is a base client providing access to GitHub, Konflux and Jira.
// All sub-clients are initialized lazily, so commands only need the
// credentials for the services they actually use.
type Client struct {
gh *github.Client
kflx *konflux.Client
jira *jira.Client
}

// NewClient creates a new base client with GitHub authentication.
// It requires the GH_TOKEN environment variable to be set.
func NewClient() (*Client, error) {
token := os.Getenv("GH_TOKEN")
if token == "" {
return nil, fmt.Errorf("GH_TOKEN environment variable is required")
}

gh := github.NewClient(nil).WithAuthToken(token)

kflx := konflux.NewClient()

// Jira client is optional - only create if credentials are provided
jiraClient, err := jira.NewClient()
if err != nil {
// Jira is optional, so we don't fail if it's not configured
jiraClient = nil
func NewClient() *Client {
return &Client{
Comment thread
tommartensen marked this conversation as resolved.
Resolved
gh: github.NewClient(),
kflx: konflux.NewClient(),
jira: jira.NewClient(),
}

return &Client{gh: gh, kflx: kflx, jira: jiraClient}, nil
}

// GetGitHub returns the underlying GitHub client for advanced usage
func (c *Client) GetGitHub() *github.Client {
return c.gh
// GetGitHub returns the GitHub client, initializing it on first use.
// It requires the GH_TOKEN environment variable to be set.
func (c *Client) GetGitHub() (*gh.Client, error) {
return c.gh.GetClient()
}

func (c *Client) GetKonflux() *konflux.Client {
Expand All @@ -59,14 +47,9 @@ func NewTestClient(httpClient *http.Client) (*Client, error) {
httpClient = &http.Client{}
}

// Create GitHub client with custom HTTP client
gh := github.NewClient(httpClient).WithAuthToken("test-token")

// Create Konflux test client with gock-interceptible HTTP client
kflx := konflux.NewTestClient(httpClient)

// Always create a Jira test client with the shared HTTP client so gock can intercept requests
jiraClient := jira.NewTestClient(httpClient)

return &Client{gh: gh, kflx: kflx, jira: jiraClient}, nil
return &Client{
gh: github.NewTestClient(httpClient),
kflx: konflux.NewTestClient(httpClient),
jira: jira.NewTestClient(httpClient),
}, nil
}
48 changes: 48 additions & 0 deletions pkg/client/github/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package github

import (
"fmt"
"net/http"
"os"
"sync"

gh "github.com/google/go-github/v79/github"
)

type Client struct {
gh *gh.Client
once sync.Once
initErr error
}

func NewClient() *Client {
return &Client{}
}

func (c *Client) init() error {
c.once.Do(func() {
token := os.Getenv("GH_TOKEN")
if token == "" {
c.initErr = fmt.Errorf("GH_TOKEN environment variable is required")
return
}
c.gh = gh.NewClient(nil).WithAuthToken(token)
})
return c.initErr
}

// GetClient returns the underlying GitHub client, initializing it on first
// use. It requires the GH_TOKEN environment variable to be set.
func (c *Client) GetClient() (*gh.Client, error) {
if c.gh != nil {
return c.gh, nil
}
if err := c.init(); err != nil {
return nil, err
}
return c.gh, nil
}

func NewTestClient(httpClient *http.Client) *Client {
return &Client{gh: gh.NewClient(httpClient).WithAuthToken("test-token")}
}
59 changes: 37 additions & 22 deletions pkg/client/jira/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"regexp"
"strings"
"sync"

"github.com/Masterminds/semver/v3"
"github.com/stackrox/relacs/pkg/client/konflux"
Expand All @@ -34,31 +35,46 @@ func (e *APIError) IsServerError() bool {
return e.StatusCode >= 500
}

// Client represents a Jira client
// Client represents a Jira client. Credentials are read from JIRA_EMAIL
// and JIRA_TOKEN on first use.
type Client struct {
httpClient *http.Client
email string
token string
tokenValidated bool

once sync.Once
initErr error
}

// NewClient creates a new Jira client
func NewClient() (*Client, error) {
email := os.Getenv("JIRA_EMAIL")
if email == "" {
return nil, fmt.Errorf("JIRA_EMAIL environment variable is required")
}
func NewClient() *Client {
return &Client{httpClient: &http.Client{}}
}

token := os.Getenv("JIRA_TOKEN")
if token == "" {
return nil, fmt.Errorf("JIRA_TOKEN environment variable is required")
// init reads the Jira credentials, skipping clients created pre-configured
// (e.g. the test client), and validates the token. The credential lookup is
// only attempted once, but token validation is retried until it succeeds:
// folding it into the once would latch transient network failures for the
// lifetime of the client.
func (c *Client) init() error {
c.once.Do(func() {
if c.email != "" && c.token != "" {
return
}
c.email = os.Getenv("JIRA_EMAIL")
c.token = os.Getenv("JIRA_TOKEN")
if c.email == "" || c.token == "" {
c.initErr = fmt.Errorf("JIRA_EMAIL and JIRA_TOKEN environment variables are required")
}
})
if c.initErr != nil {
return c.initErr
}

return &Client{
httpClient: &http.Client{},
email: email,
token: token,
}, nil
if err := c.validateToken(); err != nil {
return fmt.Errorf("jira token validation failed: %w", err)
}
return nil
}

// NewTestClient creates a Jira client with a custom HTTP client for testing.
Expand Down Expand Up @@ -142,8 +158,8 @@ func ExtractCVEKeyAndYear(cve string) (string, string, error) {

// FetchReleaseData fetches issues and CVEs from Jira using JQL
func (c *Client) FetchReleaseData(snapshot konflux.Snapshot) (*ReleaseData, error) {
if err := c.validateToken(); err != nil {
return nil, fmt.Errorf("jira token validation failed: %w", err)
if err := c.init(); err != nil {
return nil, err
}

// Build the default JQL query using the product version from the snapshot
Expand Down Expand Up @@ -330,9 +346,8 @@ func (c *Client) validateToken() (err error) {
}

func (c *Client) GetIssueFromKey(key string) (*Issue, error) {

if err := c.validateToken(); err != nil {
return nil, fmt.Errorf("jira token validation failed: %w", err)
if err := c.init(); err != nil {
return nil, err
}

issueURL := fmt.Sprintf("%s/rest/api/3/issue/%s", config.JiraBaseURL, key)
Expand Down Expand Up @@ -376,8 +391,8 @@ func (c *Client) addAuthHeader(req *http.Request) {

// GetLowestUnreleasedFixVersion returns the lowest unreleased fixVersion matching major.minor.
func (c *Client) GetLowestUnreleasedFixVersion(major, minor int) (string, error) {
if err := c.validateToken(); err != nil {
return "", fmt.Errorf("jira token validation failed: %w", err)
if err := c.init(); err != nil {
return "", err
}

versions, err := c.listUnreleasedProjectVersions()
Expand Down
Loading