This is an automated email from the ASF dual-hosted git repository.

marat pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-karavan.git


The following commit(s) were added to refs/heads/main by this push:
     new 121a075  get userinfo for commit automatically
121a075 is described below

commit 121a075d91f77f3356b6c590d4d64da47eed253b
Author: Marat Gubaidullin <marat.gubaidul...@gmail.com>
AuthorDate: Wed Nov 16 13:13:50 2022 -0500

    get userinfo for commit automatically
---
 karavan-space/src/GithubModal.tsx   | 36 +++++++++++++++++++++++++++++++++---
 karavan-space/src/api/GithubApi.tsx | 16 +++++++++++++---
 2 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/karavan-space/src/GithubModal.tsx 
b/karavan-space/src/GithubModal.tsx
index c66fad1..911bbbb 100644
--- a/karavan-space/src/GithubModal.tsx
+++ b/karavan-space/src/GithubModal.tsx
@@ -11,6 +11,8 @@ import './designer/karavan.css';
 import {GithubApi, GithubParams} from "./api/GithubApi";
 import GithubImageIcon from 
"@patternfly/react-icons/dist/esm/icons/github-icon";
 import {StorageApi} from "./api/StorageApi";
+import {KameletApi} from "../../karavan-core/lib/api/KameletApi";
+import {ComponentApi} from "../../karavan-core/lib/api/ComponentApi";
 
 interface Props {
     yaml: string,
@@ -35,6 +37,13 @@ interface State {
 export class GithubModal extends React.Component<Props, State> {
 
     public state: State = {
+        token: '',
+        owner: '',
+        repo: '',
+        branch: '',
+        name: '',
+        email: '',
+        message: 'Add a new Camel integration',
         save: false,
         pushing: false,
         path: this.props.filename
@@ -60,13 +69,34 @@ export class GithubModal extends React.Component<Props, 
State> {
     githubAuth = () => {
         GithubApi.auth(
             (result: any) => {
-                this.setState({token: result.token})
+                const onlyToken =  StorageApi.getGithubParameters() != 
undefined;
+                if (onlyToken){
+                    this.setState({token: result.token})
+                } else {
+                    this.githubData(result.token);
+                }
             },
             reason => {
                 this.props.onClose?.call(this, false, true, false, 
reason?.toString());
             });
     }
 
+    githubData = (token: string) => {
+        Promise.all([
+            GithubApi.getUserInfo(token),
+            GithubApi.getUserEmails(token),
+        ]).then(responses =>
+            Promise.all(responses.map(response => response.data))
+        ).then(data => {
+            const name: string =( data[0] as any).name || '';
+            const login: string =( data[0] as any).login || '';
+            const email: string = (Array.isArray(data[1]) ? 
Array.from(data[1]).filter(d => d.primary === true)?.at(0)?.email : '') || '';
+            this.setState({token: token, name: name, email:email, owner: 
login})
+        }).catch(err =>
+            this.props.onClose?.call(this, false, true, false, err?.toString())
+        );
+    }
+
     closeModal = () => {
         this.props.onClose?.call(this, true, false, true, '');
     }
@@ -135,12 +165,12 @@ export class GithubModal extends React.Component<Props, 
State> {
                             </FlexItem>
                         </Flex>
                     </FormGroup>
-                    <FormGroup label="User" fieldId="user" isRequired>
+                    <FormGroup label="Commit user" fieldId="user" isRequired>
                         <Flex direction={{default: "row"}} 
justifyContent={{default: "justifyContentSpaceBetween"}} alignItems={{default: 
"alignItemsStretch"}}>
                             <FlexItem>
                                 <TextInput id="username" 
placeholder="Username" value={name} onChange={value => this.setState({name: 
value})}/>
                             </FlexItem>
-                            <FlexItem flex={{default: "flex_2"}}>
+                            <FlexItem flex={{default: "flex_3"}}>
                                 <TextInput id="email" placeholder="Email" 
value={email} onChange={value => this.setState({email: value})}/>
                             </FlexItem>
                         </Flex>
diff --git a/karavan-space/src/api/GithubApi.tsx 
b/karavan-space/src/api/GithubApi.tsx
index 7cd34b7..4d9d0e8 100644
--- a/karavan-space/src/api/GithubApi.tsx
+++ b/karavan-space/src/api/GithubApi.tsx
@@ -14,8 +14,18 @@ export interface GithubParams {
 
 export class GithubApi {
 
+    static async getUserInfo(token: string) {
+        const octokit = new Octokit({auth: token});
+        return octokit.rest.users.getAuthenticated();
+    }
+
+    static async getUserEmails(token: string) {
+        const octokit = new Octokit({auth: token});
+        return octokit.rest.users.listEmailsForAuthenticatedUser();
+    }
+
     static async getFile(octokit: Octokit, param: GithubParams) {
-        return octokit.request('GET 
/repos/{owner}/{repo}/contents/{path}{?ref}', {
+        return octokit.rest.repos.getContent({
             owner: param.owner,
             repo: param.repo,
             path: param.path,
@@ -57,9 +67,9 @@ export class GithubApi {
     }
 
     static auth(onSuccess: (result: {}) => void, onError: (reason: {}) => 
void) {
-        const authenticator = new Authenticator({});
+        const authenticator = new Authenticator({site_id: 
'8dacd004-90d6-441f-93cf-592efd2d4196'});
         authenticator.authenticate(
-            { provider: "github", scope: "public_repo,repo" },
+            { provider: "github", scope: 
"public_repo,repo,read:user,user:email" },
             async function (error, data) {
                 if (error) {
                     onError(error);

Reply via email to