Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move auth to http module #6502

Merged
merged 1 commit into from Jun 13, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 0 additions & 34 deletions lib/util/got/auth.ts

This file was deleted.

17 changes: 9 additions & 8 deletions lib/util/got/index.ts
@@ -1,14 +1,15 @@
import got from 'got';
import auth from './auth';
import { mergeInstances } from './util';
import { create, mergeInstances } from './util';

export * from './common';

/*
* This is the default got instance for Renovate.
* - Cache all GET requests for the lifetime of the repo
*
*/
export const api = mergeInstances(got, auth);
const dummy = create({
options: {},
handler: (options, next) => {
return next(options);
},
});

export const api = mergeInstances(got, dummy);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when we simply directly use got? Again lots of failing tests?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Actually lots of tests mock util/got so they immediately fail. I tried changing them over but it was dozens of test suites failures either way


export default api;
26 changes: 26 additions & 0 deletions lib/util/http/auth.ts
@@ -0,0 +1,26 @@
import {
PLATFORM_TYPE_GITEA,
PLATFORM_TYPE_GITHUB,
PLATFORM_TYPE_GITLAB,
} from '../../constants/platforms';

export function applyAuthorization(inOptions: any): any {
const options = { ...inOptions };
if (options.auth || options.headers?.authorization) {
return options;
}
if (options.token) {
if (
options.hostType === PLATFORM_TYPE_GITHUB ||
options.hostType === PLATFORM_TYPE_GITEA
) {
options.headers.authorization = `token ${options.token}`;
} else if (options.hostType === PLATFORM_TYPE_GITLAB) {
options.headers['Private-token'] = options.token;
} else {
options.headers.authorization = `Bearer ${options.token}`;
}
delete options.token;
}
return options;
}
2 changes: 2 additions & 0 deletions lib/util/http/index.ts
Expand Up @@ -4,6 +4,7 @@ import { GotPromise } from 'got';
import * as runCache from '../cache/run';
import { clone } from '../clone';
import got from '../got';
import { applyAuthorization } from './auth';
import { applyHostRules } from './host-rules';

interface OutgoingHttpHeaders {
Expand Down Expand Up @@ -90,6 +91,7 @@ export class Http<GetOptions = HttpOptions, PostOptions = HttpPostOptions> {
};

options = applyHostRules(url, options);
options = applyAuthorization(options);

// Cache GET requests unless useCache=false
let promisedRes: GotPromise<any>;
Expand Down