Skip to content

Commit

Permalink
Comment unmergeable PRs (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
tibdex committed Apr 5, 2021
1 parent f321429 commit 09a5dc7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "auto-update",
"version": "2.0.0",
"version": "2.1.0",
"license": "MIT",
"files": [
"action.yml",
Expand Down
82 changes: 78 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,83 @@ const handleError = (
handle(error);
};

const unmergeablePullRequestCommentBody =
"Cannot auto-update because of conflicts.";

const handleUnmergeablePullRequest = async (
pullRequest: components["schemas"]["pull-request"],
{
octokit,
}: Readonly<{
octokit: InstanceType<typeof GitHub>;
}>,
): Promise<void> => {
await group(
`Handling unmergeable pull request #${pullRequest.number}`,
async () => {
try {
const {
head: {
repo: {
name: repo,
owner: { login: owner },
},
sha,
},
number,
} = pullRequest;

const {
data: { commit: lastCommit },
} = await octokit.request("GET /repos/{owner}/{repo}/commits/{ref}", {
owner,
ref: sha,
repo,
});

const lastCommitDate = lastCommit.committer?.date;

if (!lastCommit) {
throw new Error(`Missing date on last commit ${sha}`);
}

const comments = await octokit.paginate(
"GET /repos/{owner}/{repo}/issues/{issue_number}/comments",
{
...context.repo,
issue_number: number,
since: lastCommitDate,
},
);

const existingUnmergeablePullRequestComment = comments.find(
({ body }) => body === unmergeablePullRequestCommentBody,
);

if (existingUnmergeablePullRequestComment) {
info(
`Already commented since last commit: ${existingUnmergeablePullRequestComment.html_url}`,
);
return;
}

const { data: newComment } = await octokit.request(
"POST /repos/{owner}/{repo}/issues/{issue_number}/comments",
{
...context.repo,
body: unmergeablePullRequestCommentBody,
issue_number: number,
},
);

info(`Commented: ${newComment.html_url}`);
} catch (error: unknown) {
handleError(error, { handle: warning });
}
},
);
};

const handlePullRequest = async (
pullRequest: components["schemas"]["pull-request-simple"],
{
Expand Down Expand Up @@ -55,10 +132,7 @@ const handlePullRequest = async (
);

if (!detailedPullRequest.mergeable) {
info(
`Pull request #${pullRequest.number} is not mergeable (it probably has conflicts)`,
);
return;
return handleUnmergeablePullRequest(detailedPullRequest, { octokit });
}

await group(
Expand Down

0 comments on commit 09a5dc7

Please sign in to comment.