Pass auth token to got request

This commit is contained in:
Michal Dorner 2021-02-15 16:59:31 +01:00
parent 52024f70c3
commit 8819b4b3d4
No known key found for this signature in database
GPG key ID: 9EEE04B48DA36786
5 changed files with 36 additions and 21 deletions

23
dist/index.js generated vendored
View file

@ -37,13 +37,14 @@ const adm_zip_1 = __importDefault(__nccwpck_require__(6761));
const picomatch_1 = __importDefault(__nccwpck_require__(8569)); const picomatch_1 = __importDefault(__nccwpck_require__(8569));
const github_utils_1 = __nccwpck_require__(3522); const github_utils_1 = __nccwpck_require__(3522);
class ArtifactProvider { class ArtifactProvider {
constructor(octokit, artifact, name, pattern, sha, runId) { constructor(octokit, artifact, name, pattern, sha, runId, token) {
this.octokit = octokit; this.octokit = octokit;
this.artifact = artifact; this.artifact = artifact;
this.name = name; this.name = name;
this.pattern = pattern; this.pattern = pattern;
this.sha = sha; this.sha = sha;
this.runId = runId; this.runId = runId;
this.token = token;
if (this.artifact.startsWith('/')) { if (this.artifact.startsWith('/')) {
const endIndex = this.artifact.lastIndexOf('/'); const endIndex = this.artifact.lastIndexOf('/');
const rePattern = this.artifact.substring(1, endIndex); const rePattern = this.artifact.substring(1, endIndex);
@ -84,7 +85,7 @@ class ArtifactProvider {
return {}; return {};
} }
for (const art of artifacts) { for (const art of artifacts) {
await github_utils_1.downloadArtifact(this.octokit, art.id, art.name); await github_utils_1.downloadArtifact(this.octokit, art.id, art.name, this.token);
const reportName = this.getReportName(art.name); const reportName = this.getReportName(art.name);
const files = []; const files = [];
const zip = new adm_zip_1.default(art.name); const zip = new adm_zip_1.default(art.name);
@ -250,7 +251,7 @@ class TestReporter {
} }
const pattern = this.path.split(','); const pattern = this.path.split(',');
const inputProvider = this.artifact const inputProvider = this.artifact
? new artifact_provider_1.ArtifactProvider(this.octokit, this.artifact, this.name, pattern, this.context.sha, this.context.runId) ? new artifact_provider_1.ArtifactProvider(this.octokit, this.artifact, this.name, pattern, this.context.sha, this.context.runId, this.token)
: new local_file_provider_1.LocalFileProvider(this.name, pattern); : new local_file_provider_1.LocalFileProvider(this.name, pattern);
const parseErrors = this.maxAnnotations > 0; const parseErrors = this.maxAnnotations > 0;
const trackedFiles = await inputProvider.listTrackedFiles(); const trackedFiles = await inputProvider.listTrackedFiles();
@ -1369,7 +1370,7 @@ function getCheckRunContext() {
return { sha: github.context.sha, runId }; return { sha: github.context.sha, runId };
} }
exports.getCheckRunContext = getCheckRunContext; exports.getCheckRunContext = getCheckRunContext;
async function downloadArtifact(octokit, artifactId, fileName) { async function downloadArtifact(octokit, artifactId, fileName, token) {
core.startGroup(`Downloading artifact ${fileName}`); core.startGroup(`Downloading artifact ${fileName}`);
try { try {
core.info(`Artifact ID: ${artifactId}`); core.info(`Artifact ID: ${artifactId}`);
@ -1378,9 +1379,11 @@ async function downloadArtifact(octokit, artifactId, fileName) {
artifact_id: artifactId, artifact_id: artifactId,
archive_format: 'zip' archive_format: 'zip'
}); });
const resp = await got_1.default({ const headers = {
url: req.url, Authorization: `Bearer ${token}`
headers: req.headers, };
const resp = await got_1.default(req.url, {
headers,
followRedirect: false followRedirect: false
}); });
core.info(`Fetch artifact URL: ${resp.statusCode} ${resp.statusMessage}`); core.info(`Fetch artifact URL: ${resp.statusCode} ${resp.statusMessage}`);
@ -1389,14 +1392,14 @@ async function downloadArtifact(octokit, artifactId, fileName) {
} }
const url = resp.headers.location; const url = resp.headers.location;
if (url === undefined) { if (url === undefined) {
const headers = Object.keys(resp.headers); const receivedHeaders = Object.keys(resp.headers);
core.info(`Received headers: ${headers.join(', ')}`); core.info(`Received headers: ${receivedHeaders.join(', ')}`);
throw new Error('Location header was not found in API response'); throw new Error('Location header was not found in API response');
} }
if (typeof url !== 'string') { if (typeof url !== 'string') {
throw new Error(`Location header has unexpected value: ${url}`); throw new Error(`Location header has unexpected value: ${url}`);
} }
const downloadStream = got_1.default.stream(url); const downloadStream = got_1.default.stream(url, { headers });
const fileWriterStream = fs_1.createWriteStream(fileName); const fileWriterStream = fs_1.createWriteStream(fileName);
core.info(`Downloading ${url}`); core.info(`Downloading ${url}`);
downloadStream.on('downloadProgress', ({ transferred, total, percent }) => { downloadStream.on('downloadProgress', ({ transferred, total, percent }) => {

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View file

@ -19,7 +19,8 @@ export class ArtifactProvider implements InputProvider {
readonly name: string, readonly name: string,
readonly pattern: string[], readonly pattern: string[],
readonly sha: string, readonly sha: string,
readonly runId: number readonly runId: number,
readonly token: string
) { ) {
if (this.artifact.startsWith('/')) { if (this.artifact.startsWith('/')) {
const endIndex = this.artifact.lastIndexOf('/') const endIndex = this.artifact.lastIndexOf('/')
@ -66,7 +67,7 @@ export class ArtifactProvider implements InputProvider {
} }
for (const art of artifacts) { for (const art of artifacts) {
await downloadArtifact(this.octokit, art.id, art.name) await downloadArtifact(this.octokit, art.id, art.name, this.token)
const reportName = this.getReportName(art.name) const reportName = this.getReportName(art.name)
const files: FileContent[] = [] const files: FileContent[] = []
const zip = new Zip(art.name) const zip = new Zip(art.name)

View file

@ -69,7 +69,15 @@ class TestReporter {
const pattern = this.path.split(',') const pattern = this.path.split(',')
const inputProvider = this.artifact const inputProvider = this.artifact
? new ArtifactProvider(this.octokit, this.artifact, this.name, pattern, this.context.sha, this.context.runId) ? new ArtifactProvider(
this.octokit,
this.artifact,
this.name,
pattern,
this.context.sha,
this.context.runId,
this.token
)
: new LocalFileProvider(this.name, pattern) : new LocalFileProvider(this.name, pattern)
const parseErrors = this.maxAnnotations > 0 const parseErrors = this.maxAnnotations > 0

View file

@ -32,7 +32,8 @@ export function getCheckRunContext(): {sha: string; runId: number} {
export async function downloadArtifact( export async function downloadArtifact(
octokit: InstanceType<typeof GitHub>, octokit: InstanceType<typeof GitHub>,
artifactId: number, artifactId: number,
fileName: string fileName: string,
token: string
): Promise<void> { ): Promise<void> {
core.startGroup(`Downloading artifact ${fileName}`) core.startGroup(`Downloading artifact ${fileName}`)
try { try {
@ -44,9 +45,11 @@ export async function downloadArtifact(
archive_format: 'zip' archive_format: 'zip'
}) })
const resp = await got({ const headers = {
url: req.url, Authorization: `Bearer ${token}`
headers: req.headers as {[header: string]: string}, }
const resp = await got(req.url, {
headers,
followRedirect: false followRedirect: false
}) })
@ -57,15 +60,15 @@ export async function downloadArtifact(
const url = resp.headers.location const url = resp.headers.location
if (url === undefined) { if (url === undefined) {
const headers = Object.keys(resp.headers) const receivedHeaders = Object.keys(resp.headers)
core.info(`Received headers: ${headers.join(', ')}`) core.info(`Received headers: ${receivedHeaders.join(', ')}`)
throw new Error('Location header was not found in API response') throw new Error('Location header was not found in API response')
} }
if (typeof url !== 'string') { if (typeof url !== 'string') {
throw new Error(`Location header has unexpected value: ${url}`) throw new Error(`Location header has unexpected value: ${url}`)
} }
const downloadStream = got.stream(url) const downloadStream = got.stream(url, {headers})
const fileWriterStream = createWriteStream(fileName) const fileWriterStream = createWriteStream(fileName)
core.info(`Downloading ${url}`) core.info(`Downloading ${url}`)