Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

fix: respect github token #235

Merged
merged 1 commit into from
Apr 15, 2021
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
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ async function main(req) {
const repo = searchParams.get('repo');
const ref = searchParams.get('ref');
const path = searchParams.get('path');
const githubToken = req.headers.get('x-github-token');
if (!(owner && repo && ref)) {
return new Response('missing owner, repo, or ref', {
status: 400,
});
}

const opts = {};
if (githubToken) {
opts.headers = {
authorization: `token ${githubToken}`,
};
}

const config = await new RedirectConfig()
.withRepo(owner, repo, ref)
.withRepo(owner, repo, ref, opts)
.init();

const match = await config.match(path);
Expand Down
46 changes: 46 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ redirects:
assert.equal(result.statusCode, 204);
});

it('204 for non existing redirect.yaml', async function test() {
const { server } = this.polly;

server
.get('https://raw.githubusercontent.com/adobe/theblog/branch2/helix-redirects.yaml')
.intercept((req, res) => {
res.status(404).send();
});

const result = await index({
owner: 'adobe',
repo: 'theblog',
ref: 'branch2',
});
assert.equal(result.statusCode, 204);
});

it('204 when non-matching path provided', async function test() {
const { server } = this.polly;

Expand All @@ -101,6 +118,35 @@ redirects:
assert.equal(result.statusCode, 204);
});

it('301 for PHP on private repo', async function test() {
const { server } = this.polly;

server
.get('https://raw.githubusercontent.com/adobe/theblog/branch3p/helix-redirects.yaml')
.intercept((req, res) => {
if (req.headers.authorization !== 'token foobar') {
res.status(404).send();
return;
}
res.status(200).send(`
redirects:
- from: (.*).php
to: $1.html
`);
});

const result = await index({
owner: 'adobe',
repo: 'theblog',
ref: 'branch3p',
path: '/test.php',
__ow_headers: {
'x-github-token': 'foobar',
},
});
assert.equal(result.statusCode, 301);
});

it('301 for PHP', async function test() {
const { server } = this.polly;

Expand Down