GitHubMilestoneHasNoOpenIssuesTask outputs to a file

Rather than having the task fail if the milestone is not due today, it now
outputs to a file true or false. This allows the pipeline to determine
if it should continue or not without causing a failure.

Closes gh-11158
This commit is contained in:
Rob Winch 2022-04-20 13:49:34 -05:00 committed by Eleftheria Stein
parent 7dc4364f43
commit 78f059e446
2 changed files with 17 additions and 4 deletions

View File

@ -21,16 +21,21 @@ import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFile; import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.Optional; import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
import org.gradle.work.DisableCachingByDefault;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor; import org.yaml.snakeyaml.constructor.Constructor;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.springframework.gradle.github.RepositoryRef; import org.springframework.gradle.github.RepositoryRef;
@DisableCachingByDefault(because = "the due date needs to be checked every time in case it changes")
public abstract class GitHubMilestoneHasNoOpenIssuesTask extends DefaultTask { public abstract class GitHubMilestoneHasNoOpenIssuesTask extends DefaultTask {
@Input @Input
private RepositoryRef repository = new RepositoryRef(); private RepositoryRef repository = new RepositoryRef();
@ -44,10 +49,13 @@ public abstract class GitHubMilestoneHasNoOpenIssuesTask extends DefaultTask {
@Input @Optional @Input @Optional
private String gitHubAccessToken; private String gitHubAccessToken;
@OutputFile
public abstract RegularFileProperty getIsOpenIssuesFile();
private GitHubMilestoneApi milestones = new GitHubMilestoneApi(); private GitHubMilestoneApi milestones = new GitHubMilestoneApi();
@TaskAction @TaskAction
public void checkHasNoOpenIssues() throws FileNotFoundException { public void checkHasNoOpenIssues() throws IOException {
if (this.milestoneTitle == null) { if (this.milestoneTitle == null) {
File nextVersionFile = getNextVersionFile().getAsFile().get(); File nextVersionFile = getNextVersionFile().getAsFile().get();
Yaml yaml = new Yaml(new Constructor(NextVersionYml.class)); Yaml yaml = new Yaml(new Constructor(NextVersionYml.class));
@ -61,11 +69,15 @@ public abstract class GitHubMilestoneHasNoOpenIssuesTask extends DefaultTask {
} }
long milestoneNumber = this.milestones.findMilestoneNumberByTitle(this.repository, this.milestoneTitle); long milestoneNumber = this.milestones.findMilestoneNumberByTitle(this.repository, this.milestoneTitle);
boolean isOpenIssues = this.milestones.isOpenIssuesForMilestoneNumber(this.repository, milestoneNumber); boolean isOpenIssues = this.milestones.isOpenIssuesForMilestoneNumber(this.repository, milestoneNumber);
Path isOpenIssuesPath = getIsOpenIssuesFile().getAsFile().get().toPath();
Files.writeString(isOpenIssuesPath, String.valueOf(isOpenIssues));
if (isOpenIssues) { if (isOpenIssues) {
throw new IllegalStateException("The repository " + this.repository + " has open issues for milestone with the title " + this.milestoneTitle + " and number " + milestoneNumber); System.out.println("The repository " + this.repository + " has open issues for milestone with the title " + this.milestoneTitle + " and number " + milestoneNumber);
} }
else {
System.out.println("The repository " + this.repository + " has no open issues for milestone with the title " + this.milestoneTitle + " and number " + milestoneNumber); System.out.println("The repository " + this.repository + " has no open issues for milestone with the title " + this.milestoneTitle + " and number " + milestoneNumber);
} }
}
public RepositoryRef getRepository() { public RepositoryRef getRepository() {
return repository; return repository;

View File

@ -43,6 +43,7 @@ public class GitHubMilestonePlugin implements Plugin<Project> {
public void execute(GitHubMilestoneHasNoOpenIssuesTask githubCheckMilestoneHasNoOpenIssues) { public void execute(GitHubMilestoneHasNoOpenIssuesTask githubCheckMilestoneHasNoOpenIssues) {
githubCheckMilestoneHasNoOpenIssues.setGroup("Release"); githubCheckMilestoneHasNoOpenIssues.setGroup("Release");
githubCheckMilestoneHasNoOpenIssues.setDescription("Checks if there are any open issues for the specified repository and milestone"); githubCheckMilestoneHasNoOpenIssues.setDescription("Checks if there are any open issues for the specified repository and milestone");
githubCheckMilestoneHasNoOpenIssues.getIsOpenIssuesFile().value(project.getLayout().getBuildDirectory().file("github/milestones/is-open-issues"));
githubCheckMilestoneHasNoOpenIssues.setMilestoneTitle((String) project.findProperty("nextVersion")); githubCheckMilestoneHasNoOpenIssues.setMilestoneTitle((String) project.findProperty("nextVersion"));
if (!project.hasProperty("nextVersion")) { if (!project.hasProperty("nextVersion")) {
githubCheckMilestoneHasNoOpenIssues.getNextVersionFile().convention( githubCheckMilestoneHasNoOpenIssues.getNextVersionFile().convention(