githubapi

package
v0.0.0-...-88fb13e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 10 Imported by: 0

README

githubapi package

This package provides a single GHClient struct for all GitHub API operations (labels, issues, discussions, PRs).

  • All input types are defined in types.go.
  • The main interface is GitHubClient (see interfaces.go).
  • All methods are implemented on GHClient (see client.go).

This design allows for easy mocking and extension.

Documentation

Overview

Package githubapi contains GraphQL mutation definitions for GitHub API operations. This file centralizes all GraphQL mutations used by the GitHub client.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigurableMockGraphQLClient

type ConfigurableMockGraphQLClient struct {
	Responses map[string]*MockResponse
	DoFunc    func(context.Context, string, map[string]interface{}, interface{}) error
}

ConfigurableMockGraphQLClient provides a configurable mock that can handle common GraphQL operations

func NewDefaultMockGraphQL

func NewDefaultMockGraphQL() *ConfigurableMockGraphQLClient

NewDefaultMockGraphQL creates a mock with sensible defaults for successful operations

func NewErrorMockGraphQL

func NewErrorMockGraphQL(errorOperations map[string]string) *ConfigurableMockGraphQLClient

NewErrorMockGraphQL creates a mock that returns errors for specified operations

func (*ConfigurableMockGraphQLClient) Do

func (m *ConfigurableMockGraphQLClient) Do(ctx context.Context, query string, variables map[string]interface{}, response interface{}) error

type GHClient

type GHClient struct {
	Owner string
	Repo  string
	// contains filtered or unexported fields
}

GHClient is the main client for all GitHub API operations

func CreateTestClient

func CreateTestClient(mockGQL GraphQLClient) *GHClient

CreateTestClient creates a GHClient with the provided mock GraphQL client

func NewGHClient

func NewGHClient(ctx context.Context, owner, repo string) (*GHClient, error)

NewGHClient creates a new GitHub API client for the specified owner and repository. It initializes the GraphQL client using the go-gh library and validates that the owner and repo parameters are not empty. The client uses GraphQL exclusively for all GitHub operations including creating issues, discussions, pull requests, and managing labels.

func NewGHClientWithClients

func NewGHClientWithClients(owner, repo string, gqlClient GraphQLClient) (*GHClient, error)

NewGHClientWithClients creates a new GitHub API client with provided GraphQL client for testing. This constructor allows dependency injection of mock GraphQL clients for unit testing while maintaining the same validation and initialization logic as NewGHClient.

func (*GHClient) AddItemToProjectV2

func (c *GHClient) AddItemToProjectV2(ctx context.Context, projectID, itemNodeID string) error

AddItemToProjectV2 adds an item (issue, PR, discussion) to a ProjectV2 by item node ID. The item must be a valid GitHub content item with a node ID.

func (*GHClient) ConfigureProjectV2Fields

func (c *GHClient) ConfigureProjectV2Fields(ctx context.Context, projectID string, fields []types.ProjectV2Field) error

ConfigureProjectV2Fields creates custom fields for a ProjectV2 based on the configuration. This should be called after creating the basic project to add custom fields like Priority, Status, etc.

func (*GHClient) CreateDiscussion

func (c *GHClient) CreateDiscussion(ctx context.Context, discussion types.Discussion) (*types.CreatedItemInfo, error)

CreateDiscussion creates a new discussion in the repository and returns detailed information about the created item. It uses GraphQL to create the discussion with the specified title, body, category, and labels. The method automatically finds the correct category ID and adds labels after creation.

func (*GHClient) CreateIssue

func (c *GHClient) CreateIssue(ctx context.Context, issue types.Issue) (*types.CreatedItemInfo, error)

CreateIssue creates a new issue in the repository and returns detailed information about the created item. It validates that the GraphQL client is initialized and creates the issue with the specified title, body, labels, and assignees using GraphQL mutations.

func (*GHClient) CreateLabel

func (c *GHClient) CreateLabel(ctx context.Context, label types.Label) error

CreateLabel creates a new label in the repository using the provided label data. It validates that the GraphQL client is initialized and creates the label with the specified name, description, and color using GraphQL mutations.

func (*GHClient) CreatePR

func (c *GHClient) CreatePR(ctx context.Context, pullRequest types.PullRequest) (*types.CreatedItemInfo, error)

CreatePR creates a new pull request in the repository and returns detailed information about the created item. It validates the head and base branches, creates the PR via GraphQL API, and adds labels/assignees if specified.

func (*GHClient) CreateProjectV2

func (c *GHClient) CreateProjectV2(ctx context.Context, projectConfig types.ProjectV2Configuration) (*types.ProjectV2, error)

CreateProjectV2 creates a new ProjectV2 for the repository owner using the provided configuration. It returns the created project with its ID and URL for further operations.

func (*GHClient) DeleteDiscussion

func (c *GHClient) DeleteDiscussion(ctx context.Context, nodeID string) error

DeleteDiscussion deletes a discussion by its node ID using the GraphQL deleteDiscussion mutation

func (*GHClient) DeleteIssue

func (c *GHClient) DeleteIssue(ctx context.Context, nodeID string) error

DeleteIssue deletes an issue by its node ID

func (*GHClient) DeleteLabel

func (c *GHClient) DeleteLabel(ctx context.Context, name string) error

DeleteLabel deletes a label by its name

func (*GHClient) DeletePR

func (c *GHClient) DeletePR(ctx context.Context, nodeID string) error

DeletePR deletes a pull request by its node ID

func (*GHClient) GetProjectV2

func (c *GHClient) GetProjectV2(ctx context.Context, projectID string) (*types.ProjectV2, error)

GetProjectV2 retrieves project information by project ID. This is useful for verifying project existence and getting project details.

func (*GHClient) ListDiscussions

func (c *GHClient) ListDiscussions(ctx context.Context) ([]types.Discussion, error)

ListDiscussions retrieves all existing discussions from the repository

func (*GHClient) ListIssues

func (c *GHClient) ListIssues(ctx context.Context) ([]types.Issue, error)

ListIssues retrieves all existing issues from the repository

func (*GHClient) ListLabels

func (c *GHClient) ListLabels(ctx context.Context) ([]string, error)

Label operations

func (*GHClient) ListPRs

func (c *GHClient) ListPRs(ctx context.Context) ([]types.PullRequest, error)

ListPRs retrieves all existing pull requests from the repository

func (*GHClient) SetLogger

func (c *GHClient) SetLogger(logger common.Logger)

SetLogger sets the logger for debug output

func (*GHClient) UpdateProjectV2Description

func (c *GHClient) UpdateProjectV2Description(ctx context.Context, projectID, description string) error

UpdateProjectV2Description updates the description of an existing ProjectV2.

type GitHubClient

type GitHubClient interface {
	// Creation operations
	// ListLabels retrieves all existing labels from the repository
	ListLabels(ctx context.Context) ([]string, error)
	// CreateLabel creates a new label in the repository using the provided label data
	CreateLabel(ctx context.Context, label types.Label) error
	// CreateIssue creates a new issue and returns detailed information about the created item
	CreateIssue(ctx context.Context, issue types.Issue) (*types.CreatedItemInfo, error)
	// CreateDiscussion creates a new discussion and returns detailed information about the created item
	CreateDiscussion(ctx context.Context, discussion types.Discussion) (*types.CreatedItemInfo, error)
	// CreatePR creates a new pull request and returns detailed information about the created item
	CreatePR(ctx context.Context, pullRequest types.PullRequest) (*types.CreatedItemInfo, error)

	// Listing operations for cleanup
	// ListIssues retrieves all existing issues from the repository
	ListIssues(ctx context.Context) ([]types.Issue, error)
	// ListDiscussions retrieves all existing discussions from the repository
	ListDiscussions(ctx context.Context) ([]types.Discussion, error)
	// ListPRs retrieves all existing pull requests from the repository
	ListPRs(ctx context.Context) ([]types.PullRequest, error)

	// Deletion operations for cleanup
	// DeleteIssue deletes an issue by its node ID
	DeleteIssue(ctx context.Context, nodeID string) error
	// DeleteDiscussion deletes a discussion by its node ID
	DeleteDiscussion(ctx context.Context, nodeID string) error
	// DeletePR deletes a pull request by its node ID
	DeletePR(ctx context.Context, nodeID string) error
	// DeleteLabel deletes a label by its name
	DeleteLabel(ctx context.Context, name string) error

	// ProjectV2 operations
	// CreateProjectV2 creates a new ProjectV2 for the repository owner
	CreateProjectV2(ctx context.Context, config types.ProjectV2Configuration) (*types.ProjectV2, error)
	// ConfigureProjectV2Fields creates custom fields for a ProjectV2 based on the configuration
	ConfigureProjectV2Fields(ctx context.Context, projectID string, fields []types.ProjectV2Field) error
	// UpdateProjectV2Description updates the description of an existing ProjectV2
	UpdateProjectV2Description(ctx context.Context, projectID, description string) error
	// AddItemToProjectV2 adds an item (issue, PR, discussion) to a ProjectV2
	AddItemToProjectV2(ctx context.Context, projectID, itemNodeID string) error
	// GetProjectV2 retrieves project information by ID
	GetProjectV2(ctx context.Context, projectID string) (*types.ProjectV2, error)

	// SetLogger sets the logger for debug output during API operations
	SetLogger(logger common.Logger)
}

GitHubClient defines the interface for all GitHub API operations needed by the hydration process. This interface enables easy mocking for tests and ensures consistent API across different implementations. All methods should return appropriate errors when operations fail.

type GraphQLClient

type GraphQLClient interface {
	Do(ctx context.Context, query string, variables map[string]interface{}, response interface{}) error
}

GraphQLClient interface for testability

type MockCategory

type MockCategory struct {
	ID   string
	Name string
}

type MockResponse

type MockResponse struct {
	// Repository responses
	RepositoryID string
	Labels       []string
	Categories   []MockCategory

	// Mutation responses
	LabelID          string
	IssueID          string
	IssueNumber      int
	PRID             string
	PRNumber         int
	DiscussionID     string
	DiscussionNumber int
	UserID           string

	// Error simulation
	ShouldError  bool
	ErrorMessage string
}

MockResponse represents different types of GraphQL responses that can be configured

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL