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

Add support for adding & removing labels when no longer stale #468

Merged

Conversation

benvillalobos
Copy link
Contributor

@benvillalobos benvillalobos commented May 26, 2021

Fixes #460

Changes

Add support for:

  • labels-to-remove-when-unstale
  • labels-to-add-when-unstale

Context

In order to prevent scenarios like this and to reduce the amount of upkeep required for maintainers, allow the action to remove/add labels to prevent marking issues stale over and over again.

Example of an ideal scenario is this:

  1. User submits issue
  2. We respond and need author feedback, apply that label
  3. Issue goes stale via automation
  4. Author (or someone else with the same issue) responds suggesting it's still an issue
  5. Automation should remove the needs-author-feedback label and add some author-responded or needs-dev-response label

This allows automation to ONLY run on needs-author-feedback issues, or be exempt on author-responded issues.

@benvillalobos benvillalobos requested a review from a team as a code owner May 26, 2021 00:23
src/classes/issues-processor.ts Outdated Show resolved Hide resolved
`Adding labels marked as add-labels-when-updated-from-stale...`
);

this._statistics?.incrementAddedItemsLabel(issue);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the statistic to increment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a statistic to reflect a call to GitHub to add (one or multiple - we don't care since it's an array) a label to an issue/PR, so it seems OK to me, but I will have a better insight once I reviewed it.

src/classes/issues-processor.ts Outdated Show resolved Hide resolved
src/interfaces/issues-processor-options.ts Outdated Show resolved Hide resolved
@@ -399,6 +402,8 @@ export class IssuesProcessor {
issue,
staleLabel,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to update the readme as well

@@ -571,6 +578,10 @@ export class IssuesProcessor {
if (this._shouldRemoveStaleWhenUpdated(issue) && issueHasComments) {
await this._removeStaleLabel(issue, staleLabel);

// Are there labels to remove or add when an issue is no longer stale?
await this._removeLabelsWhenUpdatedFromStale(issue, removeLabelsWhenUpdatedFromStale);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nice to defer the logic to reduce the complexity of this file.
One thing that could maybe be even better is to completely defer it to a dedicated class/service like milestones and assignees works.

@@ -911,6 +922,48 @@ export class IssuesProcessor {
return this.options.removeStaleWhenUpdated;
}

private async _removeLabelsWhenUpdatedFromStale(
issue: Issue,
labels: Readonly<string>[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
labels: Readonly<string>[]
labels: ReadonlyArray<string>

src/classes/issues-processor.ts Outdated Show resolved Hide resolved
);

for (const label of labels.values()) {
await this._removeLabel(issue, label);

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something, it doesn't look like there's a single method for that: https://docs.github.com/en/rest/reference/issues#labels

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed.
In that case, it would be nice I think to explicitly mention in the readme when describing the option that it will consume one operation per label removal, and it's to use carefully. WDYT?

src/classes/issues-processor.ts Outdated Show resolved Hide resolved
src/main.ts Outdated Show resolved Hide resolved
}
}

private async _addLabelsWhenUpdatedFromStale(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add tests, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #468 (comment), I should have replied to this comment.

src/classes/issues-processor.ts Outdated Show resolved Hide resolved

private async _addLabelsWhenUpdatedFromStale(
issue: Issue,
addLabels: Readonly<string>[]

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we can modify the parameter this is passing into, but changing it to a ReadonlyArray gets type complaints: `The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type 'string[]'.'

src/classes/issues-processor.ts Outdated Show resolved Hide resolved
src/classes/issues-processor.ts Outdated Show resolved Hide resolved
src/classes/issues-processor.ts Outdated Show resolved Hide resolved
@benvillalobos
Copy link
Contributor Author

I noticed there was no easy way to check if specific labels were added / removed from issues, so I kept the "make sure some label was added to an issue" test similar to the others, where it just checks that an issue had a label added to it when we unstale it.

It's harder to do that for unstale-issue-has-label-removed without redesigning some of the code, which is more than I'd like to chew. I could add a test that removes a label on unstale and check if the array that contains "issues that had labels removed from them" contains two issues (_removeLabel was called twice and therefore added the same item twice), but it's not ideal.

Copy link
Collaborator

@luketomlinson luketomlinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work @benvillalobos! Let's get the README updated with the new options. I also left a comment about naming.

@@ -47,4 +47,6 @@ export interface IIssuesProcessorOptions {
exemptAllIssueAssignees: boolean | undefined;
exemptAllPrAssignees: boolean | undefined;
enableStatistics: boolean;
removeLabelsWhenUnstale: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to be too picky on naming, but I wonder if a name like labelsToRemoveWhenUnstale would read a little better and indicate that this option is a list of labels. To me removeLabelsWhenUnstale reads like it should be a boolean option. Not a huge deal either way, but curious what your thoughts are on that? @benvillalobos @C0ZEN

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reads like it should be a boolean option

I agree, and that makes sense considering I followed the naming scheme from removeIssueStaleWhenUpdated 😄 will rename.

@benvillalobos benvillalobos force-pushed the dev/add-remove-label-on-unstale branch from ca82ec2 to 6389ef2 Compare June 7, 2021 18:33
@benvillalobos
Copy link
Contributor Author

Just rebased off of main. When running npm run all I get some diffs that look to be mostly auto formatting. Should I push them up as a single commit or leave these out?

image

@C0ZEN
Copy link
Contributor

C0ZEN commented Jun 7, 2021

@benvillalobos you need to commit all those changes (probably due to a recent npm i which updated deps due to unpinned dependencies).

Copy link
Contributor

@C0ZEN C0ZEN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost done mate!

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
__tests__/main.spec.ts Outdated Show resolved Hide resolved
@@ -944,6 +959,65 @@ export class IssuesProcessor {
return this.options.removeStaleWhenUpdated;
}

private async _removeLabelsWhenUnstale(
issue: Issue,
removeLabels: Readonly<string>[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
removeLabels: Readonly<string>[]
labels: Readonly<string>[]


private async _addLabelsWhenUnstale(
issue: Issue,
addLabels: Readonly<string>[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
addLabels: Readonly<string>[]
labels: Readonly<string>[]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haven't been ignoring this suggestion. Trying to keep the var's name as labels would result in an error passing labels: labels when calling the API, presumably because the names were identical.

src/classes/issues-processor.ts Outdated Show resolved Hide resolved

this.addedLabelIssues.push(issue);

if (this.options.debugOnly) {
Copy link
Contributor

@C0ZEN C0ZEN Jun 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the merge of this one #463
This check can now be closer to the GitHub call.
Basically, you can move the if from here to just around "addLabels".
That way, we have a better debugger and we can test the operations per run.

Copy link
Contributor

@C0ZEN C0ZEN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@luketomlinson
Copy link
Collaborator

@benvillalobos some conflicts FYI

@benvillalobos benvillalobos force-pushed the dev/add-remove-label-on-unstale branch from 87fbc5e to 272bf5f Compare June 7, 2021 23:00
@@ -299,14 +300,16 @@ class IssuesProcessor {
else {
this._logger.info(`${logger_service_1.LoggerService.yellow('Processing the batch of issues')} ${logger_service_1.LoggerService.cyan(`#${page}`)} ${logger_service_1.LoggerService.yellow('containing')} ${logger_service_1.LoggerService.cyan(issues.length)} ${logger_service_1.LoggerService.yellow(`issue${issues.length > 1 ? 's' : ''}...`)}`);
}
const labelsToAddWhenUnstale = words_to_list_1.wordsToList(this.options.labelsToAddWhenUnstale);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it reasonable to add this var as a global and have it set once via the constructor? I'm not a fan of changing the args to processIssue to pass labels to add/remove when unstale. Not the most familiar with best practices in ts though.

Copy link
Collaborator

@luketomlinson luketomlinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @benvillalobos!

@luketomlinson luketomlinson merged commit b1da9e1 into actions:main Jun 8, 2021
@benvillalobos
Copy link
Contributor Author

Thanks @luketomlinson! And thanks for the help shepherding this along @C0ZEN !

@jezdez
Copy link

jezdez commented Aug 19, 2021

Seems like this wasn't enabled in the action.yml: #551

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Add label when stale issue gets a response
4 participants