Skip to content

Commit

Permalink
Stop proactively detecting potential conflicts (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
tibdex committed Apr 12, 2021
1 parent eb061e6 commit 33438f9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 73 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.1.1",
"version": "2.1.2",
"license": "MIT",
"files": [
"action.yml",
Expand Down
126 changes: 54 additions & 72 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,81 +24,75 @@ const handleError = (
handle(error);
};

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

const handleUnmergeablePullRequest = async (
pullRequest: components["schemas"]["pull-request"],
const handleUnupdatablePullRequest = async (
pullRequest: components["schemas"]["pull-request-simple"],
{
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;
try {
const {
head: {
repo: { full_name },
sha,
},
number,
} = pullRequest;

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

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

if (!lastCommitter) {
throw new Error(`Missing committer on last commit ${sha}`);
}
const lastCommitter = lastCommit.committer;

const comments = await octokit.paginate(
"GET /repos/{owner}/{repo}/issues/{issue_number}/comments",
{
...context.repo,
issue_number: number,
since: lastCommitter.date,
},
);
if (!lastCommitter) {
throw new Error(`Missing committer on last commit ${sha}`);
}

const existingUnmergeablePullRequestComment = comments.find(
({ body }) => body === unmergeablePullRequestCommentBody,
);
const comments = await octokit.paginate(
"GET /repos/{owner}/{repo}/issues/{issue_number}/comments",
{
...context.repo,
issue_number: number,
since: lastCommitter.date,
},
);

if (existingUnmergeablePullRequestComment) {
info(
`Already commented since last commit: ${existingUnmergeablePullRequestComment.html_url}`,
);
return;
}
const existingUnupdatablePullRequestComment = comments.find(
({ body }) => body === unupdatablePullRequestCommentBody,
);

const { data: newComment } = await octokit.request(
"POST /repos/{owner}/{repo}/issues/{issue_number}/comments",
{
...context.repo,
body: unmergeablePullRequestCommentBody,
issue_number: number,
},
);
if (existingUnupdatablePullRequestComment) {
info(
`Already commented since last commit: ${existingUnupdatablePullRequestComment.html_url}`,
);
return;
}

info(`Commented: ${newComment.html_url}`);
} catch (error: unknown) {
handleError(error, { handle: warning });
}
},
);
const { data: newComment } = await octokit.request(
"POST /repos/{owner}/{repo}/issues/{issue_number}/comments",
{
...context.repo,
body: unupdatablePullRequestCommentBody,
issue_number: number,
},
);

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

const handlePullRequest = async (
Expand All @@ -123,18 +117,6 @@ const handlePullRequest = async (
return;
}

const { data: detailedPullRequest } = await octokit.request(
"GET /repos/{owner}/{repo}/pulls/{pull_number}",
{
...context.repo,
pull_number: pullRequest.number,
},
);

if (!detailedPullRequest.mergeable) {
return handleUnmergeablePullRequest(detailedPullRequest, { octokit });
}

await group(
`Attempting to update pull request #${pullRequest.number}`,
async () => {
Expand All @@ -153,14 +135,14 @@ const handlePullRequest = async (
info("Updated!");
} catch (error: unknown) {
handleError(error, { handle: warning });
await handleUnupdatablePullRequest(pullRequest, { octokit });
}
},
);
};

const run = async () => {
try {
const label = getInput("label") || undefined;
const token = getInput("github_token", { required: true });
const octokit = getOctokit(token);

Expand Down

0 comments on commit 33438f9

Please sign in to comment.