Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: migrate gitlab store to pinia (#1024)

Yunwei Xiao84067e5d76e6
diff --git a/frontend/src/components/RepositorySelectionPanel.vue b/frontend/src/components/RepositorySelectionPanel.vue
index dc1eacef80..b902782128 100644
--- a/frontend/src/components/RepositorySelectionPanel.vue
+++ b/frontend/src/components/RepositorySelectionPanel.vue
@@ -40,14 +40,13 @@ export default { name: "RepositorySelectionPanel" };
</script>
<script setup lang="ts">
-import { useStore } from "vuex";
import { reactive, computed, onMounted } from "vue";
import {
ExternalRepositoryInfo,
OAuthToken,
ProjectRepositoryConfig,
} from "../types";
-import { useOAuthStore } from "@/store";
+import { useOAuthStore, useGitlabStore } from "@/store";
interface LocalState {
repositoryList: ExternalRepositoryInfo[];
@@ -62,7 +61,7 @@ const emit = defineEmits<{
(event: "set-repository", payload: ExternalRepositoryInfo): void;
}>();
-const store = useStore();
+const gitlabStore = useGitlabStore();
const state = reactive<LocalState>({
repositoryList: [],
searchText: "",
@@ -81,8 +80,8 @@ const prepareRepositoryList = () => {
})
.then((token: OAuthToken) => {
emit("set-token", token);
- store
- .dispatch("gitlab/fetchProjectList", {
+ gitlabStore
+ .fetchProjectList({
vcs: props.config.vcs,
token: token,
})
@@ -95,8 +94,8 @@ const prepareRepositoryList = () => {
const refreshRepositoryList = () => {
if (props.config.vcs.type == "GITLAB_SELF_HOST") {
- store
- .dispatch("gitlab/fetchProjectList", {
+ gitlabStore
+ .fetchProjectList({
vcs: props.config.vcs,
token: props.config.token,
})
diff --git a/frontend/src/store/pinia-modules/gitlab.ts b/frontend/src/store/pinia-modules/gitlab.ts
new file mode 100644
index 0000000000..af33cc5029
--- /dev/null
+++ b/frontend/src/store/pinia-modules/gitlab.ts
@@ -0,0 +1,66 @@
+import { defineStore } from "pinia";
+import axios from "axios";
+import {
+ ExternalRepositoryInfo,
+ VCS,
+ OAuthConfig,
+ OAuthToken,
+} from "../../types";
+
+function convertGitLabProject(project: any): ExternalRepositoryInfo {
+ const attributes = project.attributes;
+ return {
+ externalId: project.id.toString(),
+ name: attributes.name,
+ fullPath: attributes.fullPath,
+ webUrl: attributes.webUrl,
+ };
+}
+
+export const useGitlabStore = defineStore("gitlab", {
+ actions: {
+ // this actions is for initiating vcs ONLY
+ // after creation, the frontend should in no case access the secret.
+ async exchangeToken({
+ oAuthConfig,
+ code,
+ }: {
+ oAuthConfig: OAuthConfig;
+ code: string;
+ }): Promise<OAuthToken> {
+ const data = (
+ await axios.post(
+ `${oAuthConfig.endpoint}?client_id=${oAuthConfig.applicationId}&client_secret=${oAuthConfig.secret}&code=${code}&redirect_uri=${oAuthConfig.redirectUrl}&grant_type=authorization_code`
+ )
+ ).data;
+
+ const oAuthToken: OAuthToken = {
+ accessToken: data.access_token,
+ refreshToken: data.refresh_token,
+ // For GitLab, as of 13.12, the default config won't expire the access token, thus this field is 0.
+ // see https://gitlab.com/gitlab-org/gitlab/-/issues/21745.
+ expiresTs: data.expires_in == 0 ? 0 : data.created_at + data.expires_in,
+ };
+ return oAuthToken;
+ },
+
+ // TODO(zilong): here we still store the access token at the frontend, we may move this to the backend
+ async fetchProjectList({
+ vcs,
+ token,
+ }: {
+ vcs: VCS;
+ token: OAuthToken;
+ }): Promise<ExternalRepositoryInfo[]> {
+ const data = (
+ await axios.get(`/api/vcs/${vcs.id}/external-repository`, {
+ headers: {
+ accessToken: token.accessToken,
+ refreshToken: token.refreshToken,
+ },
+ })
+ ).data.data;
+ return data.map((item: any) => convertGitLabProject(item));
+ },
+ },
+});
diff --git a/frontend/src/store/pinia-modules/index.ts b/frontend/src/store/pinia-modules/index.ts
index 5a4091fb1f..3f464621eb 100644
--- a/frontend/src/store/pinia-modules/index.ts
+++ b/frontend/src/store/pinia-modules/index.ts
@@ -8,6 +8,7 @@ export * from "./dataSource";
export * from "./debug";
export * from "./deployment";
export * from "./environment";
+export * from "./gitlab";
export * from "./issueSubscriber";
export * from "./label";
export * from "./member";