Skip to content

fix: Prevent revision deletion from removing entire API#833

Open
smithimage wants to merge 1 commit intoAzure:mainfrom
smithimage:fix/revision-deletion-removes-whole-api
Open

fix: Prevent revision deletion from removing entire API#833
smithimage wants to merge 1 commit intoAzure:mainfrom
smithimage:fix/revision-deletion-removes-whole-api

Conversation

@smithimage
Copy link

This fix addresses issue #709 where deleting an API revision from source control would incorrectly delete the entire API (all revisions) instead of just the specific revision that was removed.

Root Cause:
When processing a deleted revision folder (e.g., apis/myApi;rev=4), the code would call tryGetRevisionNumberInSourceControl(rootName) to check if the root API still exists. This function uses TryGetFileContents to read the root API's apiInformation.json. However, TryGetFileContents can fail to read files from the git commit in certain scenarios (e.g., path resolution issues), returning None even when the file actually exists in the repository.

When None was returned, the old code assumed the entire API had been deleted and would call deleteApi(rootName), which cascades to DeleteAllRevisions, removing the entire API including all its revisions.

Fix:

  1. Added IsApiNameInSourceControl check before the revision number lookup. This function uses GetArtifactFiles (Git.GetExistingFilesInCommit) which returns ALL files in the commit tree, making it more reliable for existence checks.

  2. If the root API exists in source control (via isNameInSourceControl) but tryGetRevisionNumberInSourceControl returns None (file read issue), we now safely delete only the specific revision instead of the entire API.

  3. If the root API does NOT exist in source control, we correctly delete the entire API as intended.

Code Improvements:

  • Refactored processRevisionedApi method for better readability
  • Extracted deleteRevisionIfNotCurrent into a separate method
  • Removed processRootApi wrapper (directly call deleteFromApim)
  • Added comprehensive XML documentation comments
  • Improved inline comments for better clarity
  • Fixed log message to include revision number when deleting

This ensures that revision deletions only affect the specific revision, while still allowing full API deletion when the entire API folder is removed.

This fix addresses issue Azure#709 where deleting an API revision from source control
would incorrectly delete the entire API (all revisions) instead of just the
specific revision that was removed.

Root Cause:
When processing a deleted revision folder (e.g., apis/myApi;rev=4), the code
would call tryGetRevisionNumberInSourceControl(rootName) to check if the root
API still exists. This function uses TryGetFileContents to read the root API's
apiInformation.json. However, TryGetFileContents can fail to read files from
the git commit in certain scenarios (e.g., path resolution issues), returning
None even when the file actually exists in the repository.

When None was returned, the old code assumed the entire API had been deleted
and would call deleteApi(rootName), which cascades to DeleteAllRevisions,
removing the entire API including all its revisions.

Fix:
1. Added IsApiNameInSourceControl check before the revision number lookup.
   This function uses GetArtifactFiles (Git.GetExistingFilesInCommit) which
   returns ALL files in the commit tree, making it more reliable for
   existence checks.

2. If the root API exists in source control (via isNameInSourceControl) but
   tryGetRevisionNumberInSourceControl returns None (file read issue), we now
   safely delete only the specific revision instead of the entire API.

3. If the root API does NOT exist in source control, we correctly delete the
   entire API as intended.

Code Improvements:
- Refactored processRevisionedApi method for better readability
- Extracted deleteRevisionIfNotCurrent into a separate method
- Removed processRootApi wrapper (directly call deleteFromApim)
- Added comprehensive XML documentation comments
- Improved inline comments for better clarity
- Fixed log message to include revision number when deleting

This ensures that revision deletions only affect the specific revision, while
still allowing full API deletion when the entire API folder is removed.
@smithimage
Copy link
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants