Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,8 @@ import (
// If connectorID is provided, it uses that specific connector. Otherwise, it finds a connector with a valid installation_id.
// If search is provided, it fetches all repositories and filters them by the search term before applying pagination.
func (c *GithubConnectorService) GetGithubRepositoriesPaginated(userID string, page int, pageSize int, connectorID string, search string) ([]shared_types.GithubRepository, int, error) {
connectors, err := c.storage.GetAllConnectors(userID)
accessToken, err := c.getAccessTokenForUser(userID, connectorID)
if err != nil {
c.logger.Log(logger.Error, err.Error(), "")
return nil, 0, err
}

if len(connectors) == 0 {
c.logger.Log(logger.Error, "No connectors found for user", userID)
return []shared_types.GithubRepository{}, 0, nil
}

var connectorToUse *shared_types.GithubConnector

// If connectorID is provided, find that specific connector
if connectorID != "" {
for i := range connectors {
if connectors[i].ID.String() == connectorID {
connectorToUse = &connectors[i]
break
}
}
if connectorToUse == nil {
c.logger.Log(logger.Error, fmt.Sprintf("Connector with id %s not found for user", connectorID), userID)
return nil, 0, fmt.Errorf("connector not found")
}
} else {
// Find connector with valid installation_id (not empty)
for i := range connectors {
if connectors[i].InstallationID != "" && connectors[i].InstallationID != " " {
connectorToUse = &connectors[i]
break
}
}
// If no connector with installation_id found, return error
if connectorToUse == nil {
c.logger.Log(logger.Error, "No connector with valid installation_id found for user", userID)
return nil, 0, fmt.Errorf("no connector with valid installation found")
}
}

// Validate installation_id is not empty
if connectorToUse.InstallationID == "" || connectorToUse.InstallationID == " " {
c.logger.Log(logger.Error, fmt.Sprintf("Connector %s has empty installation_id", connectorToUse.ID.String()), userID)
return nil, 0, fmt.Errorf("connector has no installation_id")
}

installation_id := connectorToUse.InstallationID
jwt := GenerateJwt(connectorToUse)

accessToken, err := c.getInstallationToken(jwt, installation_id)
if err != nil {
c.logger.Log(logger.Error, fmt.Sprintf("Failed to get installation token: %s", err.Error()), "")
return nil, 0, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/golang-jwt/jwt"
"github.com/raghavyuva/nixopus-api/internal/features/logger"
shared_types "github.com/raghavyuva/nixopus-api/internal/types"
)

Expand Down Expand Up @@ -72,3 +73,68 @@ func GenerateJwt(app_credentials *shared_types.GithubConnector) string {

return tokenString
}

// getAccessTokenForUser retrieves an installation access token for the user.
// It uses the connectorID if provided, otherwise finds a connector with valid installation_id.
// This is a shared helper function used by multiple service methods.
func (c *GithubConnectorService) getAccessTokenForUser(userID string, connectorID string) (string, error) {
connectors, err := c.storage.GetAllConnectors(userID)
if err != nil {
c.logger.Log(logger.Error, err.Error(), "")
return "", err
}

if len(connectors) == 0 {
c.logger.Log(logger.Error, "No connectors found for user", userID)
return "", fmt.Errorf("no connectors found")
}

var connectorToUse *shared_types.GithubConnector

// If connectorID is provided, find that specific connector
if connectorID != "" {
for i := range connectors {
if connectors[i].ID.String() == connectorID {
connectorToUse = &connectors[i]
break
}
}
if connectorToUse == nil {
c.logger.Log(logger.Error, fmt.Sprintf("Connector with id %s not found for user", connectorID), userID)
return "", fmt.Errorf("connector not found")
}
} else {
// Find connector with valid installation_id (not empty)
for i := range connectors {
if connectors[i].InstallationID != "" && connectors[i].InstallationID != " " {
connectorToUse = &connectors[i]
break
}
}
if connectorToUse == nil {
c.logger.Log(logger.Error, "No connector with valid installation_id found for user", userID)
return "", fmt.Errorf("no connector with valid installation found")
}
}

// Validate installation_id is not empty
if connectorToUse.InstallationID == "" || connectorToUse.InstallationID == " " {
c.logger.Log(logger.Error, fmt.Sprintf("Connector %s has empty installation_id", connectorToUse.ID.String()), userID)
return "", fmt.Errorf("connector has no installation_id")
}

installationID := connectorToUse.InstallationID
jwt := GenerateJwt(connectorToUse)
if jwt == "" {
c.logger.Log(logger.Error, "Failed to generate app JWT", "")
return "", fmt.Errorf("failed to generate app JWT")
}

accessToken, err := c.getInstallationToken(jwt, installationID)
if err != nil {
c.logger.Log(logger.Error, fmt.Sprintf("Failed to get installation token: %s", err.Error()), "")
return "", err
}

return accessToken, nil
}
Loading
Loading