Skip to content

Commit

Permalink
feat: close and reopen discussions
Browse files Browse the repository at this point in the history
Closes #24.
  • Loading branch information
dessant committed Jun 8, 2023
1 parent 5155811 commit 6fcd004
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 23 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,15 @@ pull requests or discussions by grouping them under the
- Remove labels, value must be either a label or a list of labels
- Optional, defaults to `''`
- **`close`**
- Close threads, value must be either `true` or `false`,
ignored for discussions
- Close threads, value must be either `true` or `false`
- Optional, defaults to `false`
- **`close-reason`**
- Reason for closing threads, value must be
either `completed` or `not planned`, ignored for discussions
- Optional, defaults to `completed`
- Reason for closing threads, value must be:
- `completed` or `not planned` for issues
- `duplicate`, `outdated` or `resolved` for discussions
- Optional, defaults to `''`
- **`reopen`**
- Reopen threads, value must be either `true` or `false`,
ignored for discussions
- Reopen threads, value must be either `true` or `false`
- Optional, defaults to `false`
- **`lock`**
- Lock threads, value must be either `true` or `false`
Expand Down Expand Up @@ -263,6 +262,10 @@ wip:
# The `solved` label is added to discussions
solved:
discussions:
# Close the discussion
close: true
# Set a close reason
close-reason: 'resolved'
# Lock the discussion
lock: true

Expand Down
22 changes: 22 additions & 0 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ mutation ($labelableId: ID!, $labelIds: [ID!]!) {
}
`;

const closeDiscussionQuery = `
mutation ($discussionId: ID!, $reason: DiscussionCloseReason) {
closeDiscussion(input: {discussionId: $discussionId, reason: $reason}) {
discussion {
closed
}
}
}
`;

const reopenDiscussionQuery = `
mutation ($discussionId: ID!) {
reopenDiscussion(input: {discussionId: $discussionId}) {
discussion {
closed
}
}
}
`;

const lockLockableQuery = `
mutation ($lockableId: ID!) {
lockLockable(input: {lockableId: $lockableId}) {
Expand Down Expand Up @@ -105,6 +125,8 @@ module.exports = {
getDiscussionLabelsQuery,
addLabelsToLabelableQuery,
removeLabelsFromLabelableQuery,
closeDiscussionQuery,
reopenDiscussionQuery,
lockLockableQuery,
unlockLockableQuery
};
43 changes: 29 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const {
getDiscussionLabelsQuery,
addLabelsToLabelableQuery,
removeLabelsFromLabelableQuery,
closeDiscussionQuery,
reopenDiscussionQuery,
lockLockableQuery,
unlockLockableQuery
} = require('./data');
Expand Down Expand Up @@ -205,25 +207,38 @@ class App {
}
}

if (threadType !== 'discussion') {
if (
actions.reopen &&
threadData.state === 'closed' &&
!threadData.merged
) {
core.debug('Reopening');
if (actions.reopen && threadData.state === 'closed' && !threadData.merged) {
core.debug('Reopening');

if (threadType === 'discussion') {
await this.client.graphql(reopenDiscussionQuery, {
discussionId: discussion.node_id
});
} else {
await this.client.rest.issues.update({...issue, state: 'open'});
}
}

if (actions.close && threadData.state === 'open') {
core.debug('Closing');
if (actions.close && threadData.state === 'open') {
core.debug('Closing');

await this.client.rest.issues.update({
...issue,
state: 'closed',
state_reason: actions['close-reason']
});
const closeReason = actions['close-reason'];

if (threadType === 'discussion') {
const params = {discussionId: discussion.node_id};
if (closeReason) {
params.reason = closeReason;
}

await this.client.graphql(closeDiscussionQuery, params);
} else {
const params = {...issue, state: 'closed'};

if (closeReason) {
params.state_reason = closeReason;
}

await this.client.rest.issues.update(params);
}
}

Expand Down
18 changes: 16 additions & 2 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const extendedJoi = Joi.extend(joi => {
value = value.trim();
if (value === 'not planned') {
value = 'not_planned';
} else if (['duplicate', 'outdated', 'resolved'].includes(value)) {
value = value.toUpperCase();
}

return {value};
Expand Down Expand Up @@ -66,7 +68,19 @@ const configSchema = Joi.object({
const actions = {
close: Joi.boolean(),

'close-reason': extendedJoi.closeReason().valid('completed', 'not_planned'),
'close-reason': Joi.alternatives().try(
Joi.boolean().only(false),
extendedJoi
.closeReason()
.valid(
'completed',
'not_planned',
'DUPLICATE',
'OUTDATED',
'RESOLVED',
''
)
),

reopen: Joi.boolean(),

Expand Down Expand Up @@ -113,7 +127,7 @@ const actionSchema = Joi.object()
Joi.string().trim().max(51),
Joi.object().keys({
close: actions.close.default(false),
'close-reason': actions['close-reason'].default('completed'),
'close-reason': actions['close-reason'].default(''),
reopen: actions.reopen.default(false),
lock: actions.lock.default(false),
unlock: actions.unlock.default(false),
Expand Down

0 comments on commit 6fcd004

Please sign in to comment.