Skip to content

Commit b60d57d

Browse files
committed
feat: new GetUploadCopyrights to get the copyrights for an upload
1 parent 493a841 commit b60d57d

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-1
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* support new maintenance endpoint.
77
* support new upload features.
88
* new `GetUploadFileById` to download an upload file.
9+
* new `GetUploadCopyrights` to get the copyrights for an upload.
910

1011
## 1.3.0 (2022-12-30)
1112

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// ---------------------------------------------------------------------------
2+
// <copyright file="CopyrightEntry.cs" company="Tethys">
3+
// Copyright (C) 2023 T. Graf
4+
// </copyright>
5+
//
6+
// Licensed under the MIT License.
7+
// SPDX-License-Identifier: MIT
8+
//
9+
// Unless required by applicable law or agreed to in writing,
10+
// software distributed under the License is distributed on an
11+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12+
// either express or implied.
13+
// ---------------------------------------------------------------------------
14+
15+
namespace Fossology.Rest.Dotnet.Model
16+
{
17+
using System.Collections.Generic;
18+
19+
using Newtonsoft.Json;
20+
21+
/// <summary>
22+
/// Information about a single copyright.
23+
/// </summary>
24+
public class CopyrightEntry
25+
{
26+
/// <summary>
27+
/// Gets or sets the copyright statement.
28+
/// </summary>
29+
[JsonProperty("copyright")]
30+
public string Copyright { get; set; }
31+
32+
/// <summary>
33+
/// Gets or sets the list of files where the copyright was found.
34+
/// </summary>
35+
[JsonProperty("filePath")]
36+
public List<string> FilePath { get; set; }
37+
38+
/// <summary>
39+
/// Initializes a new instance of the <see cref="CopyrightEntry"/> class.
40+
/// </summary>
41+
public CopyrightEntry()
42+
{
43+
this.FilePath = new List<string>();
44+
} // CopyrightEntry()
45+
46+
/// <inheritdoc />
47+
public override string ToString()
48+
{
49+
return $"#{this.FilePath.Count}: {this.Copyright}";
50+
} // ToString()
51+
} // CopyrightEntry
52+
}

Fossology.Rest.Dotnet.Test/FossologyClientTest.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,42 @@ public void TestGetUploadFileById()
497497
Assert.AreEqual(200, result.Code);
498498
}
499499

500+
/// <summary>
501+
/// Unit test.
502+
/// </summary>
503+
[TestMethod]
504+
public void TestGetUploadCopyrights()
505+
{
506+
#if true
507+
var uploadId = FindUpload(PackageName2);
508+
if (uploadId < 0)
509+
{
510+
this.TestUploadPackageFromUrl();
511+
// ugly but required: wait some time until report is available
512+
Thread.Sleep(3000);
513+
uploadId = FindUpload(PackageName2);
514+
} // if
515+
516+
UploadPackageAndRunJobs(uploadId);
517+
#else
518+
// use a specific upload
519+
var uploadId = 111;
520+
#endif
521+
522+
var client = new FossologyClient(LocalUrl, Token);
523+
var copyrights = client.GetUploadCopyrights(uploadId);
524+
Assert.IsNotNull(copyrights);
525+
526+
Assert.AreEqual(10, copyrights.Count);
527+
Assert.AreEqual(
528+
"copyright owner or by an individual or Legal Entity authorized to submit on behalf of",
529+
copyrights[0].Copyright);
530+
Assert.AreEqual(1, copyrights[0].FilePath.Count);
531+
Assert.AreEqual(
532+
"Tethys.xml_v1.0.0.zip/Tethys.Xml-1.0.0/LICENSE",
533+
copyrights[0].FilePath[0]);
534+
}
535+
500536
/// <summary>
501537
/// Unit test.
502538
/// </summary>

Fossology.Rest.Dotnet/FossologyClientUpload.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ public IReadOnlyList<Upload> GetUploadList(string groupName = "")
446446
} // GetUploadList()
447447

448448
/// <summary>
449-
/// Gets the summary for the upload with the specified id.
449+
/// Gets the licenses for an upload.
450450
/// </summary>
451451
/// <param name="id">The identifier.</param>
452452
/// <param name="agent">Agent name, one of (nomos, monk, ninka, ojo, scancode).</param>
@@ -545,5 +545,32 @@ public Result GetUploadFileById(int id, string filename)
545545

546546
return result;
547547
} // GetUploadFileById()
548+
549+
/// <summary>
550+
/// Gets the coüpyright for an upload.
551+
/// </summary>
552+
/// <param name="id">The identifier.</param>
553+
/// <returns>A list of <see cref="CopyrightEntry" /> objects.</returns>
554+
public List<CopyrightEntry> GetUploadCopyrights(int id)
555+
{
556+
Log.Debug($"Getting upload copyrights for upload {id}...");
557+
558+
var request = new RestRequest(this.Url + $"/uploads/{id}/copyrights");
559+
request.RequestFormat = DataFormat.Json;
560+
var response = this.api.Execute(request);
561+
if (response?.Content == null)
562+
{
563+
throw new FossologyApiException(ErrorCode.NoValidAnswer);
564+
} // if
565+
566+
var summary = JsonConvert.DeserializeObject<List<CopyrightEntry>>(
567+
response.Content,
568+
new JsonSerializerSettings
569+
{
570+
NullValueHandling = NullValueHandling.Ignore,
571+
});
572+
573+
return summary;
574+
} // GetUploadCopyrights()
548575
} // FossologyClient
549576
}

0 commit comments

Comments
 (0)