Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug][Blueprints] Duplicate CircleCI scopes in same connection allowed #7653

Open
2 of 3 tasks
jensrotne opened this issue Jun 21, 2024 · 2 comments
Open
2 of 3 tasks
Labels
pr-type/bug-fix This PR fixes a bug severity/p1 This bug affects functionality or significantly affect ux type/bug This issue is a bug

Comments

@jensrotne
Copy link

Search before asking

  • I had searched in the issues and found no similar issues.

What happened

I had an issue, where I was able to input the same CircleCI sco
pe identifier twice in the connections field of the blueprint. This causes the API to throw a 500 request, but the blueprint was still created and ended up in a bad state, where I had to manually delete it and try again.

screenshot

What do you expect to happen

To give me a 400 response telling me that I had a duplicate value in the connections scopes.

How to reproduce

Adding a Blueprint with the something like the following connection plugin:

{
  "pluginName": "circleci",
  "connectionId": 2,
  "scopes": [
    {
      "scopeId": "1"
    },
    {
      "scopeId": "1"
    },
    {
      "scopeId": "3"
    },
    {
      "scopeId": "4"
    }
  ]
}

This will cause the API to throw an error.

Anything else

No response

Version

v1.0.0-rc1

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

@jensrotne jensrotne added the type/bug This issue is a bug label Jun 21, 2024
@dosubot dosubot bot added pr-type/bug-fix This PR fixes a bug severity/p1 This bug affects functionality or significantly affect ux labels Jun 21, 2024
Copy link

dosubot bot commented Jun 21, 2024

To address the issue of duplicate CircleCI scope identifiers in the connections field of the blueprint and ensure a 400 response instead of a 500 error, you can implement validation both on the frontend and backend.

Frontend Validation

Add a validation step before submitting the data to check for duplicate scope identifiers. Here's an example of how you can modify the handleSubmit function:

const handleSubmit = (scopeIds: any) => {
  if (!selectedConnection) return;

  // Check for duplicate scope identifiers
  const uniqueScopeIds = new Set(scopeIds);
  if (uniqueScopeIds.size !== scopeIds.length) {
    // Handle the error, e.g., show a notification or set an error state
    console.error("Duplicate scope identifiers found");
    return;
  }

  onSubmit({
    pluginName: selectedConnection.plugin,
    connectionId: selectedConnection.id,
    scopes: scopeIds.map((scopeId: any) => ({ scopeId })),
  });
};

This code snippet adds a check for duplicate scope identifiers using a Set. If duplicates are found, it logs an error message and prevents the submission. You can replace the console.error with any error handling mechanism you prefer, such as displaying a notification to the user [1].

Backend Validation

Ensure the backend API is designed to return a 400 error for bad requests, including those with duplicate scope identifiers. Modify the PostScopeConfig function in the backend to handle this validation:

func PostScopeConfig(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
  // Validate for duplicate scope identifiers
  scopeIds := input.Body["scopeIds"].([]interface{})
  uniqueScopeIds := make(map[interface{}]bool)
  for _, id := range scopeIds {
    if uniqueScopeIds[id] {
      return nil, errors.BadInput.New("Duplicate scope identifiers found")
    }
    uniqueScopeIds[id] = true
  }

  return dsHelper.ScopeConfigApi.Post(input)
}

This backend function checks for duplicate scope identifiers and returns a 400 error if any are found, ensuring that the client receives a clear and appropriate response [2].

Implementing these changes should prevent the creation of blueprints with duplicate scope identifiers and provide a more user-friendly error response.

To continue talking to Dosu, mention @dosu.

@klesh
Copy link
Contributor

klesh commented Jun 24, 2024

Sounds reasonable to me. Anyone is free to work on the issue if you are interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-type/bug-fix This PR fixes a bug severity/p1 This bug affects functionality or significantly affect ux type/bug This issue is a bug
Projects
None yet
Development

No branches or pull requests

2 participants