Skip to content

Commit

Permalink
feat(fossid-webapp): Support two new API functions
Browse files Browse the repository at this point in the history
This commit adds support for the functions `add_license_identification` and
`add_file_comment`.

Signed-off-by: Nicolas Nobelis <nicolas.nobelis@bosch.io>
  • Loading branch information
nnobelis authored and MarcelBochtler committed Sep 25, 2023
1 parent 66fbc5c commit 12d2bde
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
68 changes: 68 additions & 0 deletions clients/fossid-webapp/src/main/kotlin/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import java.util.Base64
import okio.buffer
import okio.sink

import org.ossreviewtoolkit.clients.fossid.model.identification.common.LicenseMatchType
import org.ossreviewtoolkit.clients.fossid.model.report.ReportType
import org.ossreviewtoolkit.clients.fossid.model.report.SelectionType
import org.ossreviewtoolkit.clients.fossid.model.result.MatchedLines
Expand Down Expand Up @@ -498,6 +499,73 @@ suspend fun FossIdRestService.unmarkAsIdentified(
)
}

/**
* Add license identification [licenseIdentifier] to file with [path] for the given [scanCode].
*
* The HTTP request is sent with [user] and [apiKey] as credentials.
*/
suspend fun FossIdRestService.addLicenseIdentification(
user: String,
apiKey: String,
scanCode: String,
path: String,
licenseIdentifier: String,
identificationOn: LicenseMatchType,
isDirectory: Boolean
): EntityResponseBody<Nothing> {
val base64Path = base64Encoder.encodeToString(path.toByteArray())
val directoryFlag = if (isDirectory) "1" else "0"
return addLicenseIdentification(
PostRequestBody(
"add_license_identification",
FILES_AND_FOLDERS_GROUP,
user,
apiKey,
mapOf(
"scan_code" to scanCode,
"path" to base64Path,
"license_identifier" to licenseIdentifier,
"identification_on" to identificationOn.name.lowercase(),
"is_directory" to directoryFlag
)
)
)
}

/**
* Add a [comment] to file with [path] for the given [scanCode].
*
* The HTTP request is sent with [user] and [apiKey] as credentials.
*/
suspend fun FossIdRestService.addFileComment(
user: String,
apiKey: String,
scanCode: String,
path: String,
comment: String,
isImportant: Boolean = false,
includeInReport: Boolean = false
): EntityResponseBody<Nothing> {
val base64Path = base64Encoder.encodeToString(path.toByteArray())
val isImportantFlag = if (isImportant) "1" else "0"
val includeInReportFlag = if (includeInReport) "1" else "0"
return addFileComment(
PostRequestBody(
"add_file_comment",
FILES_AND_FOLDERS_GROUP,
user,
apiKey,
mapOf(
"scan_code" to scanCode,
"path" to base64Path,
"comment" to comment,
"is_important" to isImportantFlag,
"include_in_report" to includeInReportFlag
)
)
)
}

/**
* If this string starts with [prefix], return the string without the prefix, otherwise return [missingPrefixValue].
*/
Expand Down
6 changes: 6 additions & 0 deletions clients/fossid-webapp/src/main/kotlin/FossIdRestService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ interface FossIdRestService {
@POST("api.php")
suspend fun unmarkAsIdentified(@Body body: PostRequestBody): EntityResponseBody<Nothing>

@POST("api.php")
suspend fun addLicenseIdentification(@Body body: PostRequestBody): EntityResponseBody<Nothing>

@POST("api.php")
suspend fun addFileComment(@Body body: PostRequestBody): EntityResponseBody<Nothing>

@GET("index.php?form=login")
suspend fun getLoginPage(): ResponseBody
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"id" : "39b6448c-7541-4387-9127-88d5e8bbdc9b",
"name" : "apiphp",
"request" : {
"url" : "/api.php",
"method" : "POST",
"bodyPatterns" : [ {
"equalToJson" : "{ \"action\": \"add_file_comment\", \"group\": \"files_and_folders\", \"data\": { \"username\": \"\", \"key\": \"\", \"scan_code\": \"semver4j_20201203_090342\", \"path\": \"c3JjL21haW4vamF2YS9jb20vdmR1cm1vbnQvc2VtdmVyNGovUmFuZ2UuamF2YQ==\", \"comment\": \"TestORT\", \"is_important\": \"0\", \"include_in_report\": \"0\" }}",
"ignoreArrayOrder" : true,
"ignoreExtraElements" : true
} ]
},
"response" : {
"status" : 200,
"body" : "{ \"operation\": \"files_and_folders_add_file_comment\", \"status\": \"1\", \"data\": { \"operation\": \"files_add_file_comment\", \"status\": \"1\", \"data\": null, \"message\": \"Success\" }, \"message\": \"Success\"}",
"headers" : {
"Content-Type" : "text/html; charset=UTF-8",
"Date" : "Thu, 05 Dec 2021 11:54:04 GMT",
"Server" : "Apache/2.4.38 (Debian)",
"Vary" : "Accept-Encoding"
}
},
"uuid" : "39b6448c-7541-4387-9127-88d5e8bbdc9b",
"persistent" : true,
"insertionIndex" : 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"id" : "7794f1fa-b8b9-4170-9811-9cb276522ee9",
"name" : "apiphp",
"request" : {
"url" : "/api.php",
"method" : "POST",
"bodyPatterns" : [ {
"equalToJson" : "{ \"action\": \"add_license_identification\", \"group\": \"files_and_folders\", \"data\": { \"username\": \"\", \"key\": \"\", \"scan_code\": \"semver4j_20201203_090342\", \"path\": \"c3JjL21haW4vamF2YS9jb20vdmR1cm1vbnQvc2VtdmVyNGovUmFuZ2UuamF2YQ==\", \"license_identifier\": \"Apache-2.0\", \"identification_on\": \"snippet\", \"is_directory\": \"0\" }}",
"ignoreArrayOrder" : true,
"ignoreExtraElements" : true
} ]
},
"response" : {
"status" : 200,
"body" : "{ \"operation\": \"files_and_folders_add_license_identification\", \"status\": \"1\", \"data\": { \"identification_id\": \"10397697\" }, \"message\": \"Success, the identification with ID: 10397697 was created.\"}",
"headers" : {
"Content-Type" : "text/html; charset=UTF-8",
"Date" : "Thu, 05 Dec 2021 11:54:04 GMT",
"Server" : "Apache/2.4.38 (Debian)",
"Vary" : "Accept-Encoding"
}
},
"uuid" : "7794f1fa-b8b9-4170-9811-9cb276522ee9",
"persistent" : true,
"insertionIndex" : 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import io.kotest.matchers.shouldNot
import io.kotest.matchers.types.shouldBeTypeOf

import org.ossreviewtoolkit.clients.fossid.model.Scan
import org.ossreviewtoolkit.clients.fossid.model.identification.common.LicenseMatchType
import org.ossreviewtoolkit.clients.fossid.model.identification.identifiedFiles.IdentifiedFile
import org.ossreviewtoolkit.clients.fossid.model.identification.ignored.IgnoredFile
import org.ossreviewtoolkit.clients.fossid.model.identification.markedAsIdentified.MarkedAsIdentifiedFile
Expand Down Expand Up @@ -278,4 +279,30 @@ class FossIdClientReturnTypeTest : StringSpec({
checkResponse("unmark file as identified")
}
}

"A license identification can be added to a file" {
service.addLicenseIdentification(
"",
"",
SCAN_CODE_1,
"src/main/java/com/vdurmont/semver4j/Range.java",
"Apache-2.0",
LicenseMatchType.SNIPPET,
false
).shouldNotBeNull().run {
checkResponse("add license identification")
}
}

"A comment can be added to a file" {
service.addFileComment(
"",
"",
SCAN_CODE_1,
"src/main/java/com/vdurmont/semver4j/Range.java",
"TestORT"
).shouldNotBeNull().run {
checkResponse("add file comment")
}
}
})

0 comments on commit 12d2bde

Please sign in to comment.