Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Register GitHub Authentication provider for the vscode Github PR plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vinokurig committed Sep 23, 2020
1 parent 2f28ffb commit 9cc1fd4
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { CheGithubMain } from '../common/che-protocol';
import { interfaces } from 'inversify';
import axios, { AxiosInstance } from 'axios';
import { OauthUtils } from './oauth-utils';
import { GitHubUser } from '@eclipse-che/plugin';

export class CheGithubMainImpl implements CheGithubMain {
private axiosInstance: AxiosInstance = axios;
Expand Down Expand Up @@ -62,4 +63,10 @@ export class CheGithubMainImpl implements CheGithubMain {
}
}
}

async $getUser(): Promise<GitHubUser> {
await this.fetchToken();
const result = await this.axiosInstance.get<GitHubUser>('https://api.github.com/user?access_token=' + this.token);
return result.data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ export interface CheOpenshiftMain {
export interface CheGithub {
uploadPublicSshKey(publicKey: string): Promise<void>;
getToken(): Promise<string>;
getUser(): Promise<che.GitHubUser>;
}

export interface CheGithubMain {
$uploadPublicSshKey(publicKey: string): Promise<void>;
$getToken(): Promise<string>;
$getUser(): Promise<che.GitHubUser>;
}

export interface CheOauth {
Expand Down
3 changes: 3 additions & 0 deletions extensions/eclipse-che-theia-plugin-ext/src/plugin/che-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ export function createAPIFactory(rpc: RPCProtocol): CheApiFactory {
},
getToken(): Promise<string> {
return cheGithubImpl.getToken();
},
getUser(): Promise<che.GitHubUser> {
return cheGithubImpl.getUser();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import { RPCProtocol } from '@theia/plugin-ext/lib/common/rpc-protocol';
import { PLUGIN_RPC_CONTEXT, CheGithub, CheGithubMain } from '../common/che-protocol';
import { GitHubUser } from '@eclipse-che/plugin';

export class CheGithubImpl implements CheGithub {

Expand All @@ -26,4 +27,8 @@ export class CheGithubImpl implements CheGithub {
getToken(): Promise<string> {
return this.githubMain.$getToken();
}

getUser(): Promise<GitHubUser> {
return this.githubMain.$getUser();
}
}
8 changes: 8 additions & 0 deletions extensions/eclipse-che-theia-plugin/src/che-proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ declare module '@eclipse-che/plugin' {
export function createWorkspace(devfilePath: string): Promise<void>;
}

export interface GitHubUser {
login: string,
id: number,
name: string,
email: string
}

export namespace github {
export function uploadPublicSshKey(publicKey: string): Promise<void>;
export function getToken(): Promise<string>;
export function getUser(): Promise<GitHubUser>;
}

export namespace openshift {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,4 @@
"path": "../examples/assembly/compile.tsconfig.json"
}
]
}
}
51 changes: 36 additions & 15 deletions plugins/github-auth-plugin/src/github-auth-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,43 @@
import * as theia from '@theia/plugin';
import * as che from '@eclipse-che/plugin';

export function start(context: theia.PluginContext): void {
export async function start(context: theia.PluginContext): Promise<void> {
let session: theia.AuthenticationSession | undefined = context.workspaceState.get('session');
if (theia.plugins.getPlugin('github.vscode-pull-request-github')) {
const command = {
id: 'github-plugin-authenticate',
label: 'GitHub authenticate'
};
context.subscriptions.push(theia.commands.registerCommand(command, async () => {
const token = await che.github.getToken();
const conf = theia.workspace.getConfiguration();
await conf.update('githubPullRequests.hosts', [{
host: 'github.com',
token
}], theia.ConfigurationTarget.Global);
theia.window.showWarningMessage('GitHub token has been set to preferences. ' +
'Refresh the page to reinitialise the vscode GitHub pull-request plugin with the token');
}));
const onDidChangeSessions = new theia.EventEmitter<theia.AuthenticationProviderAuthenticationSessionsChangeEvent>();
theia.authentication.onDidChangeSessions(async () => {
await theia.authentication.getSession('github', ['read:user', 'user:email', 'repo']);
});
theia.authentication.registerAuthenticationProvider({
id: 'github',
label: 'GitHub',
supportsMultipleAccounts: false,
onDidChangeSessions: onDidChangeSessions.event,
getSessions: async () => {
if (session) {
return [session];
} else {
return [];
}
},
login: async (scopeList: string[]) => {
const githubUser = await che.github.getUser();
session = {
id: 'github-session',
accessToken: await che.github.getToken(),
account: {label: githubUser.login, id: githubUser.id.toString()},
scopes: scopeList
};
context.workspaceState.update('session', session);
onDidChangeSessions.fire({added: [session.id], removed: [], changed: []});
return session;
},
logout: async (id: string) => {
session = undefined;
onDidChangeSessions.fire({added: [], removed: [id], changed: []});
}
}
);
}
}

Expand Down

0 comments on commit 9cc1fd4

Please sign in to comment.