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

Added warning feature #8

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
60 changes: 56 additions & 4 deletions src/main/java/at/auerchri/github_auto_closer/GitHubAutoCloser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand All @@ -31,6 +33,16 @@ class GitHubAutoCloser {
"\n" +
"<sub>(Please note that this is an [automated](https://github.com/auchri/github_auto_closer) comment.)</sub>";

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" +
"<sub>(Please note that this is an [automated](https://github.com/auchri/github_auto_closer) comment.)</sub>";

// Labels for issues which should not be closed
private static final List<String> LABELS_KEEP = new ArrayList<>();

Expand All @@ -50,6 +62,8 @@ class GitHubAutoCloser {
mIncludePullRequests = includePullRequests;
mDaysWithInactivity = daysWithInactivity;

warningBounds = 30;

if (labelToAdd != null) {
mLabelToAdd = new Label().setName(labelToAdd);
}
Expand All @@ -68,6 +82,8 @@ void run() {
RepositoryId repositoryId = new RepositoryId(mNamespace, mRepository);
List<Issue> issues = getIssues(issueService, repositoryId);

boolean wasError;

if (issues == null) {
return;
}
Expand All @@ -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++;
Expand Down Expand Up @@ -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
*
Expand Down