Skip to content

Commit

Permalink
fix: always apply close and lock reason
Browse files Browse the repository at this point in the history
  • Loading branch information
dessant committed Nov 15, 2023
1 parent cb26bc2 commit 7e8888e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 deletions.
81 changes: 44 additions & 37 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,48 +38,58 @@ class App {
const comment = this.config['issue-comment'];
if (comment) {
core.debug(`Commenting (issue: ${issue.issue_number})`);

const commentBody = comment.replace(
/{issue-author}/,
issueData.user.login
);
await this.ensureUnlock(
issue,
{active: issueData.locked, reason: issueData.active_lock_reason},
{
active: issueData.locked,
reason: issueData.active_lock_reason,
restoreLock: !this.config['lock-issue']
},
() =>
this.client.rest.issues
.createComment({...issue, body: commentBody})
.catch(err => core.warning(err.toString()))
);
}

if (this.config['close-issue'] && issueData.state === 'open') {
const closeReason = this.config['issue-close-reason'];
if (
this.config['close-issue'] &&
(issueData.state === 'open' || issueData.state_reason !== closeReason)
) {
core.debug(`Closing (issue: ${issue.issue_number})`);

const params = {...issue, state: 'closed'};

const closeReason = this.config['issue-close-reason'];
if (closeReason) {
params.state_reason = closeReason;
}

await this.client.rest.issues.update(params);
await this.client.rest.issues.update({
...issue,
state: 'closed',
state_reason: closeReason
});
}

if (this.config['lock-issue'] && !issueData.locked) {
const lockReason = this.config['issue-lock-reason'] || null;
if (
this.config['lock-issue'] &&
(!issueData.locked || issueData.active_lock_reason !== lockReason)
) {
core.debug(`Locking (issue: ${issue.issue_number})`);
let params;
const lockReason = this.config['issue-lock-reason'];

const params = {...issue};

if (lockReason) {
params = {
...issue,
lock_reason: lockReason,
headers: {
accept: 'application/vnd.github.sailor-v-preview+json'
}
};
} else {
params = issue;
params.lock_reason = lockReason;
}

// Lock reason is not updated when issue is locked
// Issue is unlocked before posting comment
if (issueData.active_lock_reason !== lockReason && !comment) {
await this.client.rest.issues.unlock(issue);
}

await this.client.rest.issues.lock(params);
}
}
Expand All @@ -94,11 +104,13 @@ class App {

if (this.config['close-issue'] && issueData.state === 'closed') {
core.debug(`Reopening (issue: ${issue.issue_number})`);

await this.client.rest.issues.update({...issue, state: 'open'});
}

if (this.config['lock-issue'] && issueData.locked) {
core.debug(`Unlocking (issue: ${issue.issue_number})`);

await this.client.rest.issues.unlock(issue);
}
}
Expand All @@ -114,25 +126,23 @@ class App {
const issue = {...github.context.repo, issue_number: issueData.number};

core.debug(`Unlabeling (issue: ${issue.issue_number})`);

await this.client.rest.issues.removeLabel({...issue, name: supportLabel});

if (this.config['lock-issue'] && issueData.locked) {
core.debug(`Unlocking (issue: ${issue.issue_number})`);

await this.client.rest.issues.unlock(issue);
}
}

async ensureUnlock(issue, lock, action) {
if (lock.active) {
if (!lock.hasOwnProperty('reason')) {
const {data: issueData} = await this.client.rest.issues.get({
...issue,
headers: {
Accept: 'application/vnd.github.sailor-v-preview+json'
}
});
const {data: issueData} = await this.client.rest.issues.get(issue);
lock.reason = issueData.active_lock_reason;
}

await this.client.rest.issues.unlock(issue);

let actionError;
Expand All @@ -142,16 +152,13 @@ class App {
actionError = err;
}

if (lock.reason) {
issue = {
...issue,
lock_reason: lock.reason,
headers: {
Accept: 'application/vnd.github.sailor-v-preview+json'
}
};
if (lock.restoreLock) {
if (lock.reason) {
issue = {...issue, lock_reason: lock.reason};
}

await this.client.rest.issues.lock(issue);
}
await this.client.rest.issues.lock(issue);

if (actionError) {
throw actionError;
Expand Down
2 changes: 1 addition & 1 deletion src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const schema = Joi.object({

'issue-close-reason': extendedJoi
.closeReason()
.valid('completed', 'not_planned', '')
.valid('completed', 'not_planned')
.default('not planned'),

'lock-issue': Joi.boolean().default(true),
Expand Down

0 comments on commit 7e8888e

Please sign in to comment.