MCPcopy Create free account
hub / github.com/github/gh-aw / findPullRequestForCurrentBranch

Function findPullRequestForCurrentBranch

actions/setup/js/handle_agent_failure.cjs:124–181  ·  view source on GitHub ↗

* Attempt to find a pull request for the current branch * @returns {Promise<{number: number, html_url: string, head_sha: string, mergeable: boolean | null, mergeable_state: string, updated_at: string} | null>} PR info or null if not found

()

Source from the content-addressed store, hash-verified

122 * @returns {Promise<{number: number, html_url: string, head_sha: string, mergeable: boolean | null, mergeable_state: string, updated_at: string} | null>} PR info or null if not found
123 */
124async function findPullRequestForCurrentBranch() {
125 try {
126 const { owner, repo } = context.repo;
127 const currentBranch = getCurrentBranch();
128
129 core.info(`Searching for pull request from branch: ${currentBranch}`);
130
131 // Search for open PRs with the current branch as head
132 const searchQuery = `repo:${owner}/${repo} is:pr is:open head:${currentBranch}`;
133
134 const searchResult = await github.rest.search.issuesAndPullRequests({
135 q: searchQuery,
136 per_page: 1,
137 });
138
139 if (searchResult.data.total_count > 0) {
140 const pr = searchResult.data.items[0];
141 core.info(`Found pull request #${pr.number}: ${pr.html_url}`);
142
143 // Fetch detailed PR info to get mergeable state and head SHA
144 try {
145 const detailedPR = await github.rest.pulls.get({
146 owner,
147 repo,
148 pull_number: pr.number,
149 });
150
151 core.info(`PR #${pr.number} details - head_sha: ${detailedPR.data.head.sha}, mergeable: ${detailedPR.data.mergeable}, mergeable_state: ${detailedPR.data.mergeable_state}`);
152
153 return {
154 number: pr.number,
155 html_url: pr.html_url,
156 head_sha: detailedPR.data.head.sha,
157 mergeable: detailedPR.data.mergeable,
158 mergeable_state: detailedPR.data.mergeable_state || "unknown",
159 updated_at: detailedPR.data.updated_at,
160 };
161 } catch (detailsError) {
162 core.warning(`Failed to fetch detailed PR info: ${getErrorMessage(detailsError)}`);
163 // Fall back to basic info
164 return {
165 number: pr.number,
166 html_url: pr.html_url,
167 head_sha: "",
168 mergeable: null,
169 mergeable_state: "unknown",
170 updated_at: "",
171 };
172 }
173 }
174
175 core.info(`No pull request found for branch: ${currentBranch}`);
176 return null;
177 } catch (error) {
178 core.warning(`Failed to find pull request for current branch: ${getErrorMessage(error)}`);
179 return null;
180 }
181}

Callers 1

mainFunction · 0.85

Calls 3

getMethod · 0.80
getCurrentBranchFunction · 0.70
getErrorMessageFunction · 0.70

Tested by

no test coverage detected