Skip to content

Commit

Permalink
Revert "Allow custom interval checks and labels"
Browse files Browse the repository at this point in the history
This reverts commit a35623d.
  • Loading branch information
alexleventer committed Oct 3, 2020
1 parent 6552efe commit 866c38d
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 105 deletions.
28 changes: 2 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# :calendar: GitHub Issue Due Dates Action
Add due dates to GitHub issues - issues are automatically tagged with labels when they pass certain date thresholds, as defined by you.
Add due dates to GitHub issues - issues are automatically tagged with `Overdue` and `Due in 1 week` labels.

## How it works:
1. Add the following snippet to the top of issues you'd like to assign due dates to:
Expand All @@ -9,7 +9,7 @@ due: 2019-09-19
---
```
2. Create a `.github/workflows/workflow.yml` file with the following contents:
```yaml
```
name: Main Workflow
on:
schedule:
Expand All @@ -23,30 +23,6 @@ jobs:
uses: alexleventer/[email protected]
with:
GH_TOKEN: "${{ secrets.GH_TOKEN }}"
OVERDUE_LABEL: OVERDUE!
INTERVALS: >-
- days: 30
label: Due in 1 month
- days: 14
label: Due in 2 weeks
- days: 7
label: Due in 1 week
- days: 1
label: DUE TOMORROW
```
3. Generate a [personal access GitHub token](https://github.com/settings/tokens).
4. Add the following environment variable to your repository secrets: `GH_TOKEN={{your personal access token}}`.

## Defining intervals

Intervals are defined as a sequence (list) with a number of `days` and
a `label` to be set.

You can define intervals and labels to meet your requirements; the
above is simply a guide.

### Note the block syntax

The value of the `INTERVALS` key must be interpreted as a string,
and must be a valid YAML block. Your action will fail if you do not
set the block indicator.
8 changes: 4 additions & 4 deletions __tests__/Octokit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ describe('Octokit', () => {
await gh.addLabelToIssue(TEST_REPO_AUTHOR, TEST_REPO_NAME, issues[1].number, ['Test']);
const updatedIssue = await gh.get(TEST_REPO_AUTHOR, TEST_REPO_NAME, issues[1].number);
expect(updatedIssue.labels.map(label => label.name).join(', ')).toContain('Test');
await gh.removeLabelsFromIssue(TEST_REPO_AUTHOR, TEST_REPO_NAME, issues[1].number, ['Test']);
await gh.removeLabelFromIssue(TEST_REPO_AUTHOR, TEST_REPO_NAME, 'Test', issues[1].number);
});

it('should remove labels from issue without label', async () => {
it('should remove label from issue without label', async () => {
const issues = await gh.listAllOpenIssues(TEST_REPO_AUTHOR, TEST_REPO_NAME);
const results = await gh.removeLabelsFromIssue(TEST_REPO_AUTHOR, TEST_REPO_NAME, issues[0].number, ['Test']);
const results = await gh.removeLabelFromIssue(TEST_REPO_AUTHOR, TEST_REPO_NAME, 'Test', issues[0].number);
expect(results).toHaveLength(0);
});

it('should remove label from issue with label', async () => {
const issues = await gh.listAllOpenIssues(TEST_REPO_AUTHOR, TEST_REPO_NAME);
await gh.addLabelToIssue(TEST_REPO_AUTHOR, TEST_REPO_NAME, issues[1].number, ['Test']);
const results = await gh.removeLabelsFromIssue(TEST_REPO_AUTHOR, TEST_REPO_NAME, issues[1].number, ['Test']);
const results = await gh.removeLabelFromIssue(TEST_REPO_AUTHOR, TEST_REPO_NAME, 'Test', issues[1].number);
expect(results).toHaveLength(1);
});

Expand Down
8 changes: 0 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ inputs:
GH_TOKEN:
description: GitHub token used to make API requests
required: true
INTERVALS:
description: >-
A list containing a `label` to set when
the ticket is due in the specified `days`
required: true
OVERDUE_LABEL:
description: The label to set when an issue is overdue
required: false
branding:
icon: 'calendar'
color: 'purple'
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"main": "dist/app.js",
"version": "1.0.13",
"version": "1.0.12",
"license": "MIT",
"scripts": {
"build": "tsc",
Expand All @@ -19,8 +19,7 @@
"@actions/core": "^1.2.6",
"@actions/github": "^4.0.0",
"@slack/client": "^5.0.2",
"front-matter": "^4.0.2",
"moment": "^2.29.0",
"yaml": "^1.10.0"
"front-matter": "^3.1.0",
"moment": "^2.25.3"
}
}
43 changes: 8 additions & 35 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,33 @@
import * as core from '@actions/core';
import {GitHub, context} from '@actions/github';
import {context} from '@actions/github';
import Octokit from './integrations/Octokit';
import {datesToDue, byDays} from './utils/dateUtils';
import YAML from 'yaml';
import {datesToDue} from './utils/dateUtils';
import {OVERDUE_TAG_NAME, NEXT_WEEK_TAG_NAME} from './constants';

export const run = async () => {
try {
const githubToken = core.getInput('GH_TOKEN');
const inputIntervals = core.getInput('INTERVALS');
const overdueLabel = core.getInput('OVERDUE_LABEL') || 'OVERDUE';

if (!githubToken) {
throw new Error('Missing GH_TOKEN environment variable');
}

if (!inputIntervals) {
throw new Error('Missing INTERVALS environment variable');
}

const ok = new Octokit(githubToken);

const issues = await ok.listAllOpenIssues(context.repo.owner, context.repo.repo);
console.log(`Found ${issues.length} open issue(s)`);

const results = await ok.getIssuesWithDueDate(issues);
console.log(`Found ${results.length} issue(s) with due dates`);

const intervals = YAML.parse(inputIntervals).sort(byDays);
const intervalLabels = intervals.map(interval => interval.label);
console.log(`Found ${intervals.length} defined intervals`);

results.forEach(async issue => {
console.log(`Processing issue #${issue.number} with due date of ${issue.due}`);
const daysUtilDueDate = await datesToDue(issue.due);

if (daysUtilDueDate <= 0) {
await ok.removeLabelsFromIssue(context.repo.owner, context.repo.repo, issue.number, intervalLabels);
await ok.addLabelToIssue(context.repo.owner, context.repo.repo, issue.number, [overdueLabel]);
console.log(`Marked issue #${issue.number} as overdue`);
} else {
for (const interval of intervals) {
if (daysUtilDueDate <= interval.days) {
await ok.removeLabelsFromIssue(context.repo.owner, context.repo.repo, issue.number, intervalLabels);
await ok.addLabelToIssue(context.repo.owner, context.repo.repo, issue.number, [interval.label]);
console.log(`Marked issue #${issue.number} with label: ${interval.label}`);
break; // don't process any more intervals
}
}
if (daysUtilDueDate <= 7 && daysUtilDueDate > 0) {
await ok.addLabelToIssue(context.repo.owner, context.repo.repo, issue.number, [NEXT_WEEK_TAG_NAME]);
} else if (daysUtilDueDate <= 0) {
await ok.removeLabelFromIssue(context.repo.owner, context.repo.repo, NEXT_WEEK_TAG_NAME, issue.number);
await ok.addLabelToIssue(context.repo.owner, context.repo.repo, issue.number, [OVERDUE_TAG_NAME]);
}
});

return {
ok: true,
issuesProcessed: results.length,
}

} catch (e) {
core.setFailed(e.message);
throw e;
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const OVERDUE_TAG_NAME = 'Overdue';
export const NEXT_WEEK_TAG_NAME = 'Due in next week';
32 changes: 15 additions & 17 deletions src/integrations/Octokit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,29 @@ export default class Octokit {
return data;
}

async addLabelToIssue(owner: string, repo: string, issue_number: number, labels: string[]) {
async addLabelToIssue(owner: string, repo: string, issueNumber: number, labels: string[]) {
const {data} = await this.client.issues.addLabels({
owner,
repo,
issue_number,
issue_number: issueNumber,
labels,
});
return data;
}

async removeLabelsFromIssue(owner: string, repo: string, issue_number: number, labels: string[]) {
labels.forEach(async label => {
try {
const {data} = await this.client.issues.removeLabel({
owner,
repo,
issue_number,
label,
});
return data;
} catch (e) {
// Do not throw error
return [];
}
});
async removeLabelFromIssue(owner: string, repo: string, name: string, issue_number: number) {
try {
const {data} = await this.client.issues.removeLabel({
owner,
repo,
name,
issue_number,
});
return data;
} catch (e) {
// Do not throw error
return [];
}
}

async getIssuesWithDueDate(rawIssues: any[]) {
Expand Down
11 changes: 0 additions & 11 deletions src/utils/dateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,3 @@ export const datesToDue = (date: string) => {
const today = moment();
return eventDate.diff(today, 'days');
};

export const byDays = (a: any, b: any) => {
switch(true) {
case a.days < b.days:
return -1;
case a.days > b.days:
return 1;
default:
return 0;
}
};

0 comments on commit 866c38d

Please sign in to comment.