Skip to content

Commit 9c131d9

Browse files
committed
fix: Include PR in the 'fixings' output, not an accidental repeat of the issue
1 parent 78a81e0 commit 9c131d9

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Octokit } from '@octokit/core';
22
import { Issue } from './Issue.js';
33

44
// https://docs.github.com/en/enterprise-cloud@latest/copilot/how-tos/use-copilot-agents/coding-agent/assign-copilot-to-an-issue#assigning-an-existing-issue
5-
export async function fixIssue(octokit: Octokit, { owner, repository, issueNumber, nodeId }: Issue) {
5+
export async function assignIssue(octokit: Octokit, { owner, repository, issueNumber, nodeId }: Issue) {
66
// Check whether issues can be assigned to Copilot
77
const suggestedActorsResponse = await octokit.graphql<{
88
repository: {
@@ -54,7 +54,7 @@ export async function fixIssue(octokit: Octokit, { owner, repository, issueNumbe
5454
return;
5555
}
5656
// Assign issue to Copilot
57-
const response = await octokit.graphql<{
57+
await octokit.graphql<{
5858
replaceActorsForAssignable: {
5959
assignable: {
6060
id: string;
@@ -84,9 +84,4 @@ export async function fixIssue(octokit: Octokit, { owner, repository, issueNumbe
8484
}`,
8585
{ issueId, assigneeId: suggestedActorsResponse?.repository?.suggestedActors?.nodes[0]?.id }
8686
);
87-
return {
88-
nodeId: response.replaceActorsForAssignable.assignable.id,
89-
url: response.replaceActorsForAssignable.assignable.url,
90-
title: response.replaceActorsForAssignable.assignable.title,
91-
}
9287
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { Octokit } from "@octokit/core";
2+
import { Issue } from "./Issue.js";
3+
4+
export async function getLinkedPR(
5+
octokit: Octokit,
6+
{ owner, repository, issueNumber, nodeId }: Issue
7+
) {
8+
// Check whether issues can be assigned to Copilot
9+
const response = await octokit.graphql<{
10+
repository?: {
11+
issue?: {
12+
timelineItems?: {
13+
nodes: (
14+
| { source: { id: string; url: string; title: string } }
15+
| { subject: { id: string; url: string; title: string } }
16+
)[];
17+
};
18+
};
19+
};
20+
}>(
21+
`query($owner: String!, $repo: String!, $issueNumber: Int!) {
22+
repository(owner: $owner, name: $repo) {
23+
issue(number: $issueNumber) {
24+
timelineItems(first: 100, itemTypes: [CONNECTED_EVENT, CROSS_REFERENCED_EVENT]) {
25+
nodes {
26+
... on CrossReferencedEvent { source { ... on PullRequest { id url title } } }
27+
... on ConnectedEvent { subject { ... on PullRequest { id url title } } }
28+
}
29+
}
30+
}
31+
}
32+
}`,
33+
{ owner, repository, issueNumber }
34+
);
35+
const timelineNodes = response?.repository?.issue?.timelineItems?.nodes || [];
36+
const pullRequest: { id: string; url: string; title: string } | undefined =
37+
timelineNodes
38+
.map((node) => {
39+
if ("source" in node && node.source?.url) return node.source;
40+
if ("subject" in node && node.subject?.url) return node.subject;
41+
return undefined;
42+
})
43+
.find((pr) => !!pr);
44+
return pullRequest;
45+
}

.github/actions/fix/src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import process from "node:process";
33
import core from "@actions/core";
44
import { Octokit } from "@octokit/core";
55
import { throttling } from "@octokit/plugin-throttling";
6-
import { fixIssue } from "./fixIssue.js";
6+
import { assignIssue } from "./assignIssue.js";
7+
import { getLinkedPR } from "./getLinkedPR.js";
8+
import { sleep } from "./sleep.js";
79
import { Issue } from "./Issue.js";
810
const OctokitWithThrottling = Octokit.plugin(throttling);
911

@@ -45,9 +47,11 @@ export default async function () {
4547
for (const fixing of fixings) {
4648
try {
4749
const issue = new Issue(fixing.issue);
48-
const response = await fixIssue(octokit, issue);
49-
if (response) {
50-
fixing.pullRequest = response;
50+
await assignIssue(octokit, issue);
51+
await sleep(1000); // Wait for Copilot to open a PR
52+
const pullRequest = await getLinkedPR(octokit, issue);
53+
if (pullRequest) {
54+
fixing.pullRequest = pullRequest;
5155
}
5256
core.info(
5357
`Assigned ${issue.owner}/${issue.repository}#${issue.issueNumber} to Copilot!`

.github/actions/fix/src/sleep.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Sleep for a given number of milliseconds.
3+
* @param ms Time to sleep, in milliseconds.
4+
* @returns
5+
*/
6+
export async function sleep(ms: number): Promise<void> {
7+
return new Promise((resolve) => setTimeout(() => resolve(), ms));
8+
}

0 commit comments

Comments
 (0)