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

Skip author/assignee automention #5

Merged
merged 3 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
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
23 changes: 14 additions & 9 deletions automention.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const { isAutomention, formatAutomention } = require('./comments');

function getOwners(issueLabels, labelToOwners, log) {
log.info('Getting unique owners', issueLabels, labelToOwners);
const uniqueOwners = new Set();
function usersToNotify(issueLabels, labelToUsers, skipNotify, log) {
log.info('Getting users to notify', issueLabels, labelToUsers, skipNotify);
const uniqueUsers = new Set();
issueLabels.forEach(label => {
const owners = labelToOwners[label];
if (owners) {
owners.forEach(owner => uniqueOwners.add(owner));
const users = labelToUsers[label];
if (users) {
users.forEach(
user => !skipNotify.includes(user) && uniqueUsers.add(user)
);
}
});
return Array.from(uniqueOwners).sort();
return Array.from(uniqueUsers).sort();
}

async function automention({
Expand All @@ -30,9 +32,12 @@ async function automention({

const fullIssue = (await issuesApi.get(issue)).data;
log.debug(`Full issue state: ${fullIssue.state}`);
const skipNotify = [fullIssue.user, ...fullIssue.assignees].map(
user => user.login
);

const owners = getOwners(labels, config, log);
const body = owners.length ? formatAutomention(owners) : null;
const users = usersToNotify(labels, config, skipNotify, log);
const body = users.length ? formatAutomention(users) : null;

if (!automentionComments.length) {
log.debug('No automention comments');
Expand Down
40 changes: 36 additions & 4 deletions automention.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ const issue = {
number: 'number'
};

const FULL_ISSUE = {
OPEN: {
state: 'open',
user: { login: 'random' },
assignees: []
},
CLOSED: {
state: 'closed',
user: { login: 'random' },
assignees: []
}
};

describe('automention', () => {
beforeEach(() => {
input = {
Expand All @@ -20,7 +33,7 @@ describe('automention', () => {
issuesApi: {
get: jest.fn().mockReturnValue(
Promise.resolve({
data: { state: 'open' }
data: FULL_ISSUE.OPEN
})
),
createComment: jest.fn(),
Expand All @@ -36,7 +49,7 @@ describe('automention', () => {
};
});

fit('handles multiple labels, uniquifying users and sorting them', async () => {
it('handles multiple labels, uniquifying users and sorting them', async () => {
input.labels = ['bug', 'whatever', 'feature'];
input.existingComments = [];
await automention(input);
Expand All @@ -46,6 +59,25 @@ describe('automention', () => {
});
});

it('excludes issue author and assignees from automention', async () => {
input.labels = ['bug', 'whatever', 'feature'];
input.existingComments = [];
input.issuesApi.get.mockReturnValue(
Promise.resolve({
data: {
state: 'open',
user: { login: 'shilman' },
assignees: [{ login: 'igor-dv' }]
}
})
);
await automention(input);
expect(input.issuesApi.createComment).toHaveBeenCalledWith({
...issue,
body: `Automention: Hey @ndelangen @tmeasday, you've been tagged! Can you give a hand here?`
});
});

describe('no automention comments', () => {
beforeEach(() => {
input.existingComments = [
Expand Down Expand Up @@ -77,7 +109,7 @@ describe('automention', () => {
input.labels = ['bug'];
input.issuesApi.get.mockReturnValue(
Promise.resolve({
data: { state: 'closed' }
data: FULL_ISSUE.CLOSED
})
);
await automention(input);
Expand Down Expand Up @@ -132,7 +164,7 @@ describe('automention', () => {
input.labels = ['bug'];
input.issuesApi.get.mockReturnValue(
Promise.resolve({
data: { state: 'closed' }
data: FULL_ISSUE.CLOSED
})
);
await automention(input);
Expand Down