generated from moderneinc/rewrite-recipe-starter
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: validate team name for codeowners
- Loading branch information
Showing
7 changed files
with
211 additions
and
4 deletions.
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
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
48 changes: 48 additions & 0 deletions
48
src/main/java/org/openrewrite/jenkins/github/InMemoryTeamNameValidator.java
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,48 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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. | ||
*/ | ||
package org.openrewrite.jenkins.github; | ||
|
||
import org.openrewrite.internal.lang.Nullable; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* This is a simple stopgap to prevent the same issues from recurring. | ||
* Ideally we'd have a near real-time view of the actual teams in GitHub. | ||
* At the moment there are over 2500, so making an API call for each | ||
* recipe run is likely to run into trouble. | ||
*/ | ||
class InMemoryTeamNameValidator implements TeamNameValidator { | ||
private static final Set<String> BANNED = banned(); | ||
|
||
@Override | ||
public boolean isValid(@Nullable String name) { | ||
return name != null && | ||
!name.isEmpty() && | ||
!BANNED.contains(name); | ||
} | ||
|
||
/** | ||
* Known calculated values that do not map to actual teams | ||
*/ | ||
private static Set<String> banned() { | ||
Set<String> banned = new HashSet<>(); | ||
banned.add("@jenkinsci/custom-tools-plugin-developers"); | ||
banned.add("@jenkinsci/-plugin-developers"); | ||
return banned; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/openrewrite/jenkins/github/TeamNameValidator.java
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,20 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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. | ||
*/ | ||
package org.openrewrite.jenkins.github; | ||
|
||
public interface TeamNameValidator { | ||
boolean isValid(String name); | ||
} |
59 changes: 59 additions & 0 deletions
59
src/test/java/org/openrewrite/jenkins/github/AddTeamToCodeownersScannedTest.java
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,59 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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. | ||
*/ | ||
package org.openrewrite.jenkins.github; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.openrewrite.jenkins.github.AddTeamToCodeowners.Scanned; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AddTeamToCodeownersScannedTest { | ||
@Mock | ||
private TeamNameGenerator<TeamNameInput> generator; | ||
@Mock | ||
private TeamNameValidator validator; | ||
|
||
@Test | ||
void shouldBeInvalid() { | ||
String teamName = "abc"; | ||
given(generator.generate(any())).willReturn(teamName); | ||
given(validator.isValid(teamName)).willReturn(false); | ||
Scanned scanned = new Scanned(generator, validator); | ||
|
||
boolean actual = scanned.hasValidTeamName(); | ||
|
||
assertThat(actual).isFalse(); | ||
} | ||
|
||
|
||
@Test | ||
void shouldBeValid() { | ||
String teamName = "abc"; | ||
given(generator.generate(any())).willReturn(teamName); | ||
given(validator.isValid(teamName)).willReturn(true); | ||
Scanned scanned = new Scanned(generator, validator); | ||
|
||
boolean actual = scanned.hasValidTeamName(); | ||
|
||
assertThat(actual).isTrue(); | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
src/test/java/org/openrewrite/jenkins/github/InMemoryTeamNameValidatorTest.java
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,45 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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. | ||
*/ | ||
package org.openrewrite.jenkins.github; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.NullAndEmptySource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class InMemoryTeamNameValidatorTest { | ||
private final InMemoryTeamNameValidator validator = new InMemoryTeamNameValidator(); | ||
|
||
@Test | ||
void shouldValidate() { | ||
String input = "@jenkinsci/log-parser-plugin-developers"; | ||
boolean actual = validator.isValid(input); | ||
assertThat(actual).isTrue(); | ||
} | ||
|
||
@ParameterizedTest | ||
@NullAndEmptySource | ||
@ValueSource(strings = { | ||
"@jenkinsci/custom-tools-plugin-developers", // actual: @jenkinsci/customtools-plugin-developers | ||
"@jenkinsci/-plugin-developers", // we didn't get anything for the artifactId | ||
}) | ||
void shouldNotValidate(String input) { | ||
boolean actual = validator.isValid(input); | ||
assertThat(actual).isFalse(); | ||
} | ||
} |