github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/web/src/store/modules/repository/actions.ts (about)

     1  import { ActionContext } from 'vuex';
     2  import Axios from 'axios';
     3  import { RepoState, Mutations, Actions } from '.';
     4  import { RootState } from '@/store';
     5  import { reasonToError } from '@/plugins/http';
     6  
     7  const errUndefinedCurrentRepo = new Error('current repository is undefined');
     8  
     9  function fetchRepositories(context: ActionContext<RepoState, RootState>): Promise<void> {
    10    return new Promise((resolve) => {
    11      Axios.get<Repository[]>(`${context.rootState.base}/api/v1/user/repos`)
    12        .then((response) => {
    13          context.commit(Mutations.UPDATE_REPOSITORY_LIST, response.data);
    14        }).catch((error) => {
    15          console.warn(error);
    16        }).finally(() => {
    17          resolve();
    18        });
    19    });
    20  }
    21  
    22  function fetchRepository(base: string, scm: string, namespace: string, name: string): Promise<Repository> {
    23    return new Promise((resolve, reject) => {
    24      let repository: Repository;
    25      Axios.get<Repository>(
    26        `${base}/api/v1/repos/${scm}/${namespace}/${name}`
    27      ).then((response) => {
    28        repository = response.data;
    29      }).catch((reason) => {
    30        let error: Error;
    31        if (reason.response) {
    32          error = new Error(reason.response.data);
    33        } else if (reason.message) {
    34          error = new Error(reason.message);
    35        } else {
    36          error = new Error('Unknown Error');
    37        }
    38        if (!repository) {
    39          reject(error);
    40        }
    41      }).finally(() => {
    42        resolve(repository);
    43      });
    44    });
    45  }
    46  
    47  export function fetchRepositoryList<S extends RepoState, R extends RootState>(context: ActionContext<S, R>): Promise<void> {
    48    context.commit(Mutations.START_REPOSITORY_LOADING);
    49    return fetchRepositories(context).then(() => {
    50      context.commit(Mutations.STOP_REPOSITORY_LOADING);
    51    });
    52  }
    53  
    54  export function synchronizeRepository(context: ActionContext<RepoState, RootState>): Promise<void> {
    55    return new Promise(resolve => {
    56      Axios.patch(`${context.rootState.base}/api/v1/user/repos`)
    57        .then(() => {
    58          context.dispatch(Actions.FETCH_REPOSITORY_LIST);
    59        }).catch(reason => {
    60          console.warn(reasonToError(reason));
    61        }).finally(() => {
    62          resolve();
    63        });
    64    });
    65  }
    66  
    67  export function updateRepositoryCurrent<S extends RepoState, R extends RootState>(
    68    context: ActionContext<S, R>
    69  ): Promise<void> {
    70    return new Promise<void>((resolve, reject) => {
    71      context.commit(Mutations.START_REPOSITORY_LOADING);
    72      if (context.state.current === undefined) {
    73        context.commit(Mutations.STOP_REPOSITORY_LOADING);
    74        reject(errUndefinedCurrentRepo);
    75      }
    76      const repo = context.state.current;
    77      const { SCM, Name, NameSpace } = (repo as Repository);
    78      fetchRepository(context.rootState.base, SCM, NameSpace, Name)
    79        .then((response) => {
    80          context.commit(Mutations.SET_REPOSITORY_CURRENT, response);
    81        }).catch(error => {
    82          reject(error);
    83        }).finally(() => {
    84          context.commit(Mutations.STOP_REPOSITORY_LOADING);
    85          resolve();
    86        });
    87    });
    88  }
    89  
    90  export function updateRepositoryReportID<S extends RepoState, R extends RootState>(
    91    context: ActionContext<S, R>
    92  ): Promise<void> {
    93    return new Promise<void>((resolve, reject) => {
    94      context.commit(Mutations.START_REPOSITORY_LOADING);
    95      const repo = context.state.current;
    96      if (repo === undefined) {
    97        context.commit(Mutations.STOP_REPOSITORY_LOADING);
    98        reject(errUndefinedCurrentRepo);
    99      }
   100      Axios.patch<Repository>(
   101        `${context.rootState.base}/api/v1/${repo?.SCM}/${repo?.NameSpace}/${repo?.Name}`
   102      ).then((response) => {
   103        context.commit(Mutations.SET_REPOSITORY_CURRENT, response.data);
   104      }).catch(reason => {
   105        const error = reasonToError(reason);
   106        context.commit(Mutations.SET_REPOSITORY_ERROR, error);
   107        reject(error);
   108      }).finally(() => {
   109        context.commit(Mutations.STOP_REPOSITORY_LOADING);
   110        resolve();
   111      });
   112    });
   113  }
   114  
   115  export function changeCurrentRepository<S extends RepoState, R extends RootState>(
   116    context: ActionContext<S, R>, params: { scm: string; namespace: string; name: string }
   117  ): Promise<void> {
   118    return new Promise((resolve) => {
   119      context.commit(Mutations.START_REPOSITORY_LOADING);
   120      context.commit(Mutations.SET_REPOSITORY_ERROR);
   121      fetchRepository(context.rootState.base, params.scm, params.namespace, params.name)
   122        .then((response) => {
   123          context.commit(Mutations.SET_REPOSITORY_CURRENT, response);
   124        }).catch(error => {
   125          context.commit(Mutations.SET_REPOSITORY_ERROR, error);
   126        }).finally(() => {
   127          context.commit(Mutations.STOP_REPOSITORY_LOADING);
   128          resolve();
   129        });
   130    });
   131  }
   132  
   133  export function fetchRepositorySetting<S extends RepoState, R extends RootState>(
   134    context: ActionContext<S, R>
   135  ): Promise<void> {
   136    return new Promise((resolve) => {
   137      const base = context.rootState.base;
   138      const repo = context.state.current;
   139      if (repo === undefined) {
   140        context.commit(Mutations.SET_REPOSITORY_SETTING, undefined);
   141        resolve();
   142        return;
   143      }
   144      context.commit(Mutations.START_REPOSITORY_LOADING);
   145      const { SCM, Name, NameSpace } = (repo as Repository);
   146      Axios.get<RepositorySetting>(`${base}/api/v1/repos/${SCM}/${NameSpace}/${Name}/setting`)
   147        .then(response => {
   148          context.commit(Mutations.SET_REPOSITORY_SETTING, response.data);
   149        })
   150        .catch(() => {
   151          context.commit(Mutations.SET_REPOSITORY_SETTING, undefined);
   152        }).finally(() => {
   153          context.commit(Mutations.STOP_REPOSITORY_LOADING);
   154          resolve();
   155        });
   156    });
   157  }
   158  
   159  export function fetchRepositoryOwner<S extends RepoState, R extends RootState>(
   160    context: ActionContext<S, R>
   161  ): Promise<void> {
   162    return new Promise((resolve) => {
   163      const base = context.rootState.base;
   164      const repo = context.state.current;
   165      if (repo === undefined) {
   166        context.commit(Mutations.SET_REPOSITORY_OWNER, false);
   167        resolve();
   168        return;
   169      }
   170      const { SCM, Name, NameSpace } = (repo as Repository);
   171      Axios.get<RepositorySetting>(`${base}/api/v1/user/owner/${SCM}/${NameSpace}/${Name}`)
   172        .then(() => {
   173          context.commit(Mutations.SET_REPOSITORY_OWNER, true);
   174        })
   175        .catch(() => {
   176          context.commit(Mutations.SET_REPOSITORY_OWNER, false);
   177        }).finally(() => {
   178          resolve();
   179        });
   180    });
   181  }
   182  
   183  export function fetchRepositoryCommits<S extends RepoState, R extends RootState>(
   184    context: ActionContext<S, R>, ref = ''
   185  ): Promise<void> {
   186    return new Promise(resolve => {
   187      const base = context.rootState.base;
   188      const repo = context.state.current;
   189      if (repo === undefined) {
   190        context.commit(Mutations.SET_REPOSITORY_COMMITS, []);
   191        resolve();
   192        return;
   193      }
   194      const { SCM, Name, NameSpace } = (repo as Repository);
   195      Axios.get<Commit[]>(
   196        `${base}/api/v1/repos/${SCM}/${NameSpace}/${Name}/commits`,
   197        { params: { ref: ref } })
   198        .then(response => {
   199          context.commit(Mutations.SET_REPOSITORY_COMMITS, response.data);
   200        })
   201        .catch(() => {
   202          context.commit(Mutations.SET_REPOSITORY_COMMITS, []);
   203        })
   204        .finally(() => {
   205          resolve();
   206        });
   207    });
   208  }
   209  
   210  export function fetchRepositoryBranches<S extends RepoState, R extends RootState>(
   211    context: ActionContext<S, R>
   212  ): Promise<void> {
   213    return new Promise(resolve => {
   214      const base = context.rootState.base;
   215      const repo = context.state.current;
   216      if (repo === undefined) {
   217        context.commit(Mutations.SET_REPOSITORY_BRANCHES, []);
   218        resolve();
   219        return;
   220      }
   221      const { SCM: scm, Name: name, NameSpace: nameSpace } = (repo as Repository);
   222      Axios.get<string[]>(`${base}/api/v1/repos/${scm}/${nameSpace}/${name}/branches`)
   223        .then(response => {
   224          context.commit(Mutations.SET_REPOSITORY_BRANCHES, response.data);
   225        })
   226        .catch(() => {
   227          context.commit(Mutations.SET_REPOSITORY_BRANCHES, []);
   228        })
   229        .finally(() => {
   230          resolve();
   231        });
   232    });
   233  }