Skip to content

Commit

Permalink
[MI-3676] Review fixes: Code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
raghavaggarwal2308 committed Oct 27, 2023
1 parent aad0dbb commit 13dcb55
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
6 changes: 3 additions & 3 deletions server/instance_cloud_oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ func (ci *cloudOAuthInstance) GetClient(connection *Connection) (Client, error)
func (ci *cloudOAuthInstance) getClientForConnection(connection *Connection) (*jira.Client, *http.Client, error) {
oauth2Conf := ci.GetOAuthConfig()
ctx := context.Background()
tokenSource := oauth2Conf.TokenSource(ctx, connection.OAuth2Token)
if ci.JWTInstance != nil && tokenSource == nil {
ci.Plugin.API.LogDebug("Returning a JWT token client in case the stored JWT instance is not nil and the user's oauth token is nil")
if ci.JWTInstance != nil && connection.OAuth2Token == nil {
ci.Plugin.API.LogDebug("Returning a JWT token client since the stored JWT instance is not nil and the user's oauth token is nil")
return ci.JWTInstance.getClientForConnection(connection)
}

tokenSource := oauth2Conf.TokenSource(ctx, connection.OAuth2Token)
client := oauth2.NewClient(ctx, tokenSource)

// Get a new token, if Access Token has expired
Expand Down
51 changes: 28 additions & 23 deletions server/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,44 +141,49 @@ func (p *instancesArray) Resize(n int) {
*p = make(instancesArray, n)
}

func (p *Plugin) InstallInstance(instance Instance) error {
func (p *Plugin) InstallInstance(newInstance Instance) error {
var updated *Instances
err := UpdateInstances(p.instanceStore,
func(instances *Instances) error {
if !p.enterpriseChecker.HasEnterpriseFeatures() {
if instances != nil && len(instances.IDs()) > 0 && !instances.checkIfExists(instance.GetID()) {
if instances != nil && len(instances.IDs()) > 0 && !instances.checkIfExists(newInstance.GetID()) {
return errors.Errorf(licenseErrorString)
}
}

if instance.Common().Type == CloudOAuthInstanceType && len(instances.IDs()) > 0 && instances.checkIfExists(instance.GetID()) {
p.API.LogDebug("Getting the stored JWT instance data to store it inside the OAuth instance data.")
jwtInstance, err := p.instanceStore.LoadInstance(instance.GetID())
if err != nil {
p.API.LogError("Error occurred while fetching the instance", "ID", instance.GetID(), "Error", err.Error())
return err
}

// Storing the JWT instance data inside the OAuth instance data. We will use this to use the stored JWT token in case the user has not connected to OAuth yet.
p.API.LogDebug("Instance type stored in KV store", "ID", jwtInstance.GetID(), "Type", jwtInstance.Common().Type)
if jwtInstance.Common().Type == CloudInstanceType {
instance.(*cloudOAuthInstance).JWTInstance = jwtInstance.(*cloudInstance)
} else if jwtInstance.Common().Type == CloudOAuthInstanceType {
instance.(*cloudOAuthInstance).JWTInstance = jwtInstance.(*cloudOAuthInstance).JWTInstance
}

if instance.(*cloudOAuthInstance).JWTInstance != nil {
p.API.LogDebug("JWT instance is successfully stored inside cloud OAuth instance data.", "JWTInstance", instance.(*cloudOAuthInstance).JWTInstance)
if newInstance.Common().Type == CloudOAuthInstanceType && len(instances.IDs()) > 0 && instances.checkIfExists(newInstance.GetID()) {
oAuthInstance, ok := newInstance.(*cloudOAuthInstance)
if !ok {
p.API.LogError("Instance type is `cloud-oauth` but failed to assert instance.(*cloudOAuthInstance).", "ID", newInstance.GetID())
} else {
p.API.LogDebug("Failed to store JWT instance inside cloud OAuth instance data or there was no JWT instance installed previously.", "JWTInstance", instance.(*cloudOAuthInstance).JWTInstance)
p.API.LogDebug("Getting the stored JWT instance data to store it inside the OAuth instance data.")
previousInstance, err := p.instanceStore.LoadInstance(newInstance.GetID())
if err != nil {
p.API.LogError("Error occurred while fetching the instance", "ID", newInstance.GetID(), "Error", err.Error())
return err
}

// Storing the JWT instance data inside the OAuth instance data. We will use this to use the stored JWT token in case the user has not connected to OAuth yet.
p.API.LogDebug("Instance type stored in KV store", "ID", previousInstance.GetID(), "Type", previousInstance.Common().Type)
if previousInstance.Common().Type == CloudInstanceType {
oAuthInstance.JWTInstance = previousInstance.(*cloudInstance)
} else if previousInstance.Common().Type == CloudOAuthInstanceType {
oAuthInstance.JWTInstance = previousInstance.(*cloudOAuthInstance).JWTInstance
}

if oAuthInstance.JWTInstance != nil {
p.API.LogDebug("JWT instance is successfully stored inside cloud OAuth instance data.", "JWTInstance", oAuthInstance.JWTInstance)
} else {
p.API.LogDebug("No JWT instance inside cloud OAuth instance data.", "JWTInstance", oAuthInstance.JWTInstance)
}
}
}

err := p.instanceStore.StoreInstance(instance)
err := p.instanceStore.StoreInstance(newInstance)
if err != nil {
return err
}
instances.Set(instance.Common())
instances.Set(newInstance.Common())
updated = instances
return nil
})
Expand Down

0 comments on commit 13dcb55

Please sign in to comment.