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

fix: Take SPDX ID from GitHub API when searching for the license #634

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/com/lpvs/service/LPVSGitHubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ public void commentResults(
* Retrieves the license of the GitHub repository associated with the pull request.
*
* @param webhookConfig LPVSQueue configuration for the pull request.
* @return License key of the GitHub repository or null if not available.
* @return License SPDX ID and name for the GitHub repository or null if not available.
*/
public String getRepositoryLicense(LPVSQueue webhookConfig) {
public String[] getRepositoryLicense(LPVSQueue webhookConfig) {
try {
String repositoryName = LPVSPayloadUtil.getRepositoryName(webhookConfig);
String repositoryOrganization =
Expand All @@ -371,7 +371,7 @@ public String getRepositoryLicense(LPVSQueue webhookConfig) {
if (license == null) {
return null;
} else {
return license.getKey();
return new String[] {license.getSpdxId(), license.getName()};
}
} catch (IOException | IllegalArgumentException e) {
log.error("Can't authorize getRepositoryLicense(): " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ public void processWebHook(LPVSQueue webhookConfig) {
filePath = filePath.split(":::::")[0];
}
// check repository license
String repositoryLicense = gitHubService.getRepositoryLicense(webhookConfig);
String[] repositoryLicense = gitHubService.getRepositoryLicense(webhookConfig);

if (repositoryLicense != null) {
LPVSLicense repoLicense =
licenseService.getLicenseBySpdxIdAndName(
repositoryLicense, Optional.empty());
repositoryLicense[0],
Optional.ofNullable(repositoryLicense[1]));
webhookConfig.setRepositoryLicense(repoLicense.getSpdxId());
} else {
webhookConfig.setRepositoryLicense(null);
Expand Down
15 changes: 10 additions & 5 deletions src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3829,7 +3829,8 @@ void setUp() {
} catch (IOException e) {
log.error("mocked_repo.getLicense error " + e);
}
when(mocked_license.getKey()).thenReturn(test_license_key);
when(mocked_license.getSpdxId()).thenReturn(test_license_key);
when(mocked_license.getName()).thenReturn(test_license_key);
}

@Test
Expand All @@ -3841,7 +3842,8 @@ public void testGetRepositoryLicense__ApiUrlAbsentLisencePresent() {
.thenReturn(mocked_instance_gh);

// main test
assertEquals(test_license_key, gh_service.getRepositoryLicense(webhookConfig));
assertEquals(
2, Arrays.stream(gh_service.getRepositoryLicense(webhookConfig)).count());

// verification of calling methods on `Mock`s
// `mocked_static_gh` verify
Expand Down Expand Up @@ -3869,7 +3871,8 @@ public void testGetRepositoryLicense__ApiUrlAbsentLisencePresent() {
verifyNoMoreInteractions(mocked_repo);

// `mocked_license` verify
verify(mocked_license, times(1)).getKey();
verify(mocked_license, times(1)).getSpdxId();
verify(mocked_license, times(1)).getName();
verifyNoMoreInteractions(mocked_license);
}
}
Expand Down Expand Up @@ -4032,7 +4035,8 @@ public void testGetRepositoryLicense__ApiUrlPresentLisencePresent() {
.thenReturn(mocked_instance_gh);

// main test
assertEquals(test_license_key, gh_service.getRepositoryLicense(webhookConfig));
assertEquals(
2, Arrays.stream(gh_service.getRepositoryLicense(webhookConfig)).count());

// verification of calling methods on `Mock`s
// `mocked_static_gh` verify
Expand Down Expand Up @@ -4064,7 +4068,8 @@ public void testGetRepositoryLicense__ApiUrlPresentLisencePresent() {
verifyNoMoreInteractions(mocked_repo);

// `mocked_license` verify
verify(mocked_license, times(1)).getKey();
verify(mocked_license, times(1)).getSpdxId();
verify(mocked_license, times(1)).getName();
verifyNoMoreInteractions(mocked_license);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,11 @@ void setUp() {
when(mockGitHubService.getPullRequestFiles(webhookConfigMain))
.thenReturn(filePathTestNoDeletion);
when(mockGitHubService.getRepositoryLicense(webhookConfigMain))
.thenReturn(licenseNameTest);
.thenReturn(new String[] {spdxIdTest, licenseNameTest});

mockLicenseService = mock(LPVSLicenseService.class);
when(mockLicenseService.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty()))
when(mockLicenseService.getLicenseBySpdxIdAndName(
spdxIdTest, Optional.of(licenseNameTest)))
.thenReturn(lpvsLicenseTest);

mockDetectService = mock(LPVSDetectService.class);
Expand Down Expand Up @@ -312,7 +313,7 @@ public void testProcessWebHook____DeletionAbsentLicensePresent() throws Exceptio
verify(mockGitHubService, times(1)).getPullRequestFiles(webhookConfigMain);
verify(mockGitHubService, times(1)).getRepositoryLicense(webhookConfigMain);
verify(mockLicenseService, times(1))
.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty());
.getLicenseBySpdxIdAndName(spdxIdTest, Optional.of(licenseNameTest));
try {
verify(mockDetectService, times(1))
.runScan(webhookConfigMain, filePathTestNoDeletion);
Expand Down Expand Up @@ -374,10 +375,11 @@ void setUp() {
when(mockGitHubService.getPullRequestFiles(webhookConfigMain))
.thenReturn(filePathTestWithDeletion);
when(mockGitHubService.getRepositoryLicense(webhookConfigMain))
.thenReturn(licenseNameTest);
.thenReturn(new String[] {spdxIdTest, licenseNameTest});

mockLicenseService = mock(LPVSLicenseService.class);
when(mockLicenseService.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty()))
when(mockLicenseService.getLicenseBySpdxIdAndName(
spdxIdTest, Optional.of(licenseNameTest)))
.thenReturn(lpvsLicenseTest);

mockDetectService = mock(LPVSDetectService.class);
Expand Down Expand Up @@ -415,7 +417,7 @@ public void testProcessWebHook____DeletionPresentLicensePresent() throws Excepti
verify(mockGitHubService, times(1)).getPullRequestFiles(webhookConfigMain);
verify(mockGitHubService, times(1)).getRepositoryLicense(webhookConfigMain);
verify(mockLicenseService, times(1))
.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty());
.getLicenseBySpdxIdAndName(spdxIdTest, Optional.of(licenseNameTest));
try {
verify(mockDetectService, times(1))
.runScan(webhookConfigMain, filePathTestWithDeletionTruncated);
Expand Down Expand Up @@ -474,10 +476,11 @@ void setUp() {
when(mockGitHubService.getPullRequestFiles(webhookConfigMain))
.thenReturn(filePathTestNoDeletion);
when(mockGitHubService.getRepositoryLicense(webhookConfigMain))
.thenReturn(licenseNameTest);
.thenReturn(new String[] {spdxIdTest, licenseNameTest});

mockLicenseService = mock(LPVSLicenseService.class);
when(mockLicenseService.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty()))
when(mockLicenseService.getLicenseBySpdxIdAndName(
spdxIdTest, Optional.of(licenseNameTest)))
.thenReturn(lpvsLicenseTest);

mockDetectService = mock(LPVSDetectService.class);
Expand Down Expand Up @@ -519,9 +522,7 @@ public void testProcessWebHook__DeletionAbsentLicenseFound() throws Exception {
verify(mockGitHubService, times(1)).getPullRequestFiles(webhookConfigMain);
verify(mockGitHubService, times(1)).getRepositoryLicense(webhookConfigMain);
verify(mockLicenseService, times(1))
.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty());
verify(mockLicenseService, times(1))
.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty());
.getLicenseBySpdxIdAndName(spdxIdTest, Optional.of(licenseNameTest));
try {
verify(mockDetectService, times(1))
.runScan(webhookConfigMain, filePathTestNoDeletion);
Expand Down Expand Up @@ -583,10 +584,11 @@ void setUp() {
when(mockGitHubService.getPullRequestFiles(webhookConfigMain))
.thenReturn(filePathTestWithDeletion);
when(mockGitHubService.getRepositoryLicense(webhookConfigMain))
.thenReturn(licenseNameTest);
.thenReturn(new String[] {spdxIdTest, licenseNameTest});

mockLicenseService = mock(LPVSLicenseService.class);
when(mockLicenseService.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty()))
when(mockLicenseService.getLicenseBySpdxIdAndName(
spdxIdTest, Optional.of(licenseNameTest)))
.thenReturn(lpvsLicenseTest);

mockDetectService = mock(LPVSDetectService.class);
Expand Down Expand Up @@ -624,9 +626,7 @@ public void testProcessWebHook__DeletionPresentLicenseFound() throws Exception {
verify(mockGitHubService, times(1)).getPullRequestFiles(webhookConfigMain);
verify(mockGitHubService, times(1)).getRepositoryLicense(webhookConfigMain);
verify(mockLicenseService, times(1))
.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty());
verify(mockLicenseService, times(1))
.getLicenseBySpdxIdAndName(licenseNameTest, Optional.empty());
.getLicenseBySpdxIdAndName(spdxIdTest, Optional.of(licenseNameTest));
try {
verify(mockDetectService, times(1))
.runScan(webhookConfigMain, filePathTestWithDeletionTruncated);
Expand Down