Skip to content

Commit 42ee1bb

Browse files
authored
fix: add authentication to repo.get request (#98)
1 parent 0d63c8f commit 42ee1bb

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/dispatch/controller.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,19 @@ export const dispatchControllerFactory: () => Promise<RequestHandler> =
8888
// If it's not present, we need to resolve the default branch.
8989
let enrichedTargetRef = body.target.ref;
9090
if (!enrichedTargetRef) {
91-
enrichedTargetRef = await getRepositoryDefaultBranch({
92-
owner: body.target.owner,
93-
repo: body.target.repo,
94-
});
91+
try {
92+
enrichedTargetRef = await getRepositoryDefaultBranch({
93+
owner: body.target.owner,
94+
repo: body.target.repo,
95+
});
96+
} catch (e) {
97+
_reqLogger.warn({ error: e }, "Failed to resolve default branch");
98+
99+
return res
100+
.status(400)
101+
.header("content-type", responseContentType)
102+
.json({ error: "Failed to resolve default branch" });
103+
}
95104
}
96105

97106
// Map the body to the policy input.

src/dispatch/github.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,21 @@ async function resolveAccessToken(id: RepositoryIdentity): Promise<string> {
116116
export async function getRepositoryDefaultBranch(
117117
id: RepositoryIdentity,
118118
): Promise<string> {
119+
// We need to fetch the metadata of the repository, which might be private.
120+
// Therefore, we also need the access token.
121+
const token = await resolveAccessToken({
122+
owner: id.owner,
123+
repo: id.repo,
124+
});
125+
119126
try {
120-
const { data } = await _baseOctokit.rest.repos.get({
121-
owner: id.owner,
122-
repo: id.repo,
127+
const data = await runOctokit(token, async (octokit) => {
128+
const { data } = await octokit.rest.repos.get({
129+
owner: id.owner,
130+
repo: id.repo,
131+
});
132+
133+
return data;
123134
});
124135

125136
return data.default_branch;

0 commit comments

Comments
 (0)