diff --git a/src/main/java/at/auerchri/github_auto_closer/GitHubAutoCloser.java b/src/main/java/at/auerchri/github_auto_closer/GitHubAutoCloser.java index 6e0571f..7578a8c 100644 --- a/src/main/java/at/auerchri/github_auto_closer/GitHubAutoCloser.java +++ b/src/main/java/at/auerchri/github_auto_closer/GitHubAutoCloser.java @@ -21,6 +21,8 @@ class GitHubAutoCloser { private int mDaysWithInactivity; private Label mLabelToAdd; + private int warningBounds; + private static final String CLOSE_MESSAGE = "Hey there!\n" + "\n" + "We're automatically closing this issue since there was no activity in this issue since %1$d days ago. We therefore assume that the user has lost interest or resolved the problem on their own. Closed issues that remain inactive for a long period may get automatically locked.\n" + @@ -31,6 +33,16 @@ class GitHubAutoCloser { "\n" + "(Please note that this is an [automated](https://github.com/auchri/github_auto_closer) comment.)"; + private static final String WARNING_MESSAGE = "Hey there!\n" + + "\n" + + "We've detected that this issue since has had no activity in %1$d days. We therefore assume that the user has lost interest or resolved the problem on their own. Closed issues that remain inactive for a long period may get automatically locked.\n" + + "\n" + + "Don't worry though; this is just a warning that the issue will be closed in %2$d days is there is no further activity on this issue.\n" + + "\n" + + "Thanks!\n" + + "\n" + + "(Please note that this is an [automated](https://github.com/auchri/github_auto_closer) comment.)"; + // Labels for issues which should not be closed private static final List LABELS_KEEP = new ArrayList<>(); @@ -50,6 +62,8 @@ class GitHubAutoCloser { mIncludePullRequests = includePullRequests; mDaysWithInactivity = daysWithInactivity; + warningBounds = 30; + if (labelToAdd != null) { mLabelToAdd = new Label().setName(labelToAdd); } @@ -68,6 +82,8 @@ void run() { RepositoryId repositoryId = new RepositoryId(mNamespace, mRepository); List issues = getIssues(issueService, repositoryId); + boolean wasError; + if (issues == null) { return; } @@ -87,10 +103,16 @@ void run() { continue; } - boolean wasError = closeIssue(issueService, repositoryId, issue, daysWithoutActivity); - - if (wasError) { - break; + if (daysWithoutActivity < warningBounds) { + wasError = warnIssue(issueService, repositoryId, issue, daysWithoutActivity); + if (wasError) { + break; + } + } else { + wasError = closeIssue(issueService, repositoryId, issue, daysWithoutActivity); + if (wasError) { + break; + } } nClosedIssues++; @@ -143,6 +165,36 @@ private boolean issueHasLabels(Issue issue) { return false; } + + /** + * Posts a warning to an issue + * + * @param issueService The issue service + * @param repositoryId The repository of the issue + * @param issue The issue which is given a warning + * @param daysWithoutActivity Amount of days without activity + * @return True if there was an error + */ + private boolean warnIssue(IssueService issueService, RepositoryId repositoryId, Issue issue, long daysWithoutActivity) { + String message = String.format(WARNING_MESSAGE, daysWithoutActivity, 30-daysWithoutActivity); + + if (mLabelToAdd != null) { + issue.getLabels().add(mLabelToAdd); + } + + try { + issueService.createComment(repositoryId, issue.getNumber(), message); + issueService.editIssue(repositoryId, issue); + + Logger.log(Logger.Level.INFO, "Gave warning to issue %1$d (%2$s)", issue.getNumber(), issue.getTitle()); + } catch (Exception e) { + Logger.log(Logger.Level.ERROR, e, "Error giving warning to issue %1$d", issue.getNumber()); + return true; + } + + return false; + } + /** * Closes an issue *