Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/dispatch/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,19 @@ export const dispatchControllerFactory: () => Promise<RequestHandler> =
// If it's not present, we need to resolve the default branch.
let enrichedTargetRef = body.target.ref;
if (!enrichedTargetRef) {
enrichedTargetRef = await getRepositoryDefaultBranch({
owner: body.target.owner,
repo: body.target.repo,
});
try {
enrichedTargetRef = await getRepositoryDefaultBranch({
owner: body.target.owner,
repo: body.target.repo,
});
} catch (e) {
_reqLogger.warn({ error: e }, "Failed to resolve default branch");

return res
.status(400)
.header("content-type", responseContentType)
.json({ error: "Failed to resolve default branch" });
}
}

// Map the body to the policy input.
Expand Down
17 changes: 14 additions & 3 deletions src/dispatch/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,21 @@ async function resolveAccessToken(id: RepositoryIdentity): Promise<string> {
export async function getRepositoryDefaultBranch(
id: RepositoryIdentity,
): Promise<string> {
// We need to fetch the metadata of the repository, which might be private.
// Therefore, we also need the access token.
const token = await resolveAccessToken({
owner: id.owner,
repo: id.repo,
});

try {
const { data } = await _baseOctokit.rest.repos.get({
owner: id.owner,
repo: id.repo,
const data = await runOctokit(token, async (octokit) => {
const { data } = await octokit.rest.repos.get({
owner: id.owner,
repo: id.repo,
});

return data;
});

return data.default_branch;
Expand Down