-
Notifications
You must be signed in to change notification settings - Fork 313
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
fix(composer): Restore any modified files after analysis #9590
Merged
+91
−30
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
plugins/package-managers/composer/src/main/kotlin/LockfileProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.plugins.packagemanagers.composer | ||
|
||
import java.io.File | ||
|
||
import kotlin.io.path.moveTo | ||
|
||
import org.semver4j.Semver | ||
|
||
private const val COMPOSER_LOCK_FILE = "composer.lock" | ||
|
||
class LockfileProvider(private val definitionFile: File) { | ||
private val workingDir = definitionFile.parentFile | ||
|
||
val lockfile = workingDir.resolve(COMPOSER_LOCK_FILE) | ||
|
||
fun <T> ensureLockfile(block: (File) -> T): T { | ||
if (lockfile.isFile) return block(lockfile) | ||
|
||
val definitionFileBackup = enableLockfileCreation() | ||
|
||
return try { | ||
require(createLockFile()) | ||
block(lockfile) | ||
} finally { | ||
lockfile.delete() | ||
definitionFileBackup?.also { it.toPath().moveTo(definitionFile.toPath(), overwrite = true) } | ||
} | ||
} | ||
|
||
private fun enableLockfileCreation(): File? { | ||
var definitionFileBackup: File? = null | ||
val lockConfig = ComposerCommand.run(workingDir, "--no-interaction", "config", "lock") | ||
|
||
if (lockConfig.isSuccess && lockConfig.stdout.trim() == "false") { | ||
File.createTempFile("composer", "json", workingDir).also { | ||
mnonnenmacher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// The above call already creates an empty file, so the copy call needs to overwrite it. | ||
definitionFileBackup = definitionFile.copyTo(it, overwrite = true) | ||
} | ||
|
||
// Ensure that the build is not configured to disallow the creation of lockfiles. | ||
val unsetLock = ComposerCommand.run(workingDir, "--no-interaction", "config", "--unset", "lock") | ||
if (unsetLock.isError) { | ||
definitionFileBackup?.delete() | ||
return null | ||
} | ||
} | ||
|
||
return definitionFileBackup | ||
} | ||
|
||
private fun createLockFile(): Boolean { | ||
val args = buildList { | ||
add("--no-interaction") | ||
add("update") | ||
add("--ignore-platform-reqs") | ||
|
||
val composerVersion = Semver(ComposerCommand.getVersion(workingDir)) | ||
if (composerVersion.major >= 2) { | ||
add("--no-install") | ||
add("--no-audit") | ||
} | ||
} | ||
|
||
val update = ComposerCommand.run(workingDir, *args.toTypedArray()) | ||
return update.isSuccess | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might be wrong, but to my understanding ORT's analyzers normally do not do this kind of clean-up.
If true, what's the reason for doing this here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that this kind of cleanup is probably done inconsistently / not done at all for package managers currently. But I believe we should come up with a policy to start doing it consistently. Besides the annoyance of modified files when analyzing locally, leaving modified files behind also creates problems in CI pipelines that use
git describe
-based mechanisms to calculate the project version after analysis, as such approaches would eventually add a-dirty
suffix to the version number.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These could be also easily addressed with dropping file changes via
git
.I'm undecided if it's worth to maintain the logic for it, what do others think? maybe any @oss-review-toolkit/core-devs ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our premise has always been that the ORT analyzer can be used without changes to your build, and that includes making fixups like resetting modified files with Git in a CI pipeline in my view.
Note that approving this PR does not mean that you agree to do the same changes for other package managers. As such I'd appreciate if the discussion could be limited to the code in this PR, and to pursue the general discussion elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion we should always try to not modify the project files with the analyzer, and we have to, clean up afterward, and I think that was always our philosophy, although we might have forgotten to implement it consistently. For example, in NPM we could simply delete the
node_modules
directory, but instead we move it away so that it can be restored after the analysis.