Skip to content

Commit

Permalink
项目创建
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjj1024 committed Sep 19, 2024
1 parent f42ea4b commit 129ed9e
Show file tree
Hide file tree
Showing 4 changed files with 685 additions and 22 deletions.
27 changes: 25 additions & 2 deletions src/apis/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,42 @@ export default {
})
},
// get github user info
gitUserInfo() {
gitUserInfo(token: string) {
return http('/user', {
method: 'get',
headers: {
Authorization: `Bearer ${token}`,
'User-Agent': 'PostmanRuntime/7.41.2',
},
responseType: ResponseType.JSON,
})
},
// start repository
startProgect() {
return http(`/user/starred/Sjj1024/PakePlus`, {
method: 'put',
})
},
// creat project repository
creatProgect(body: any) {
forkProgect(body: any) {
return http(`/repos/Sjj1024/PakePlus/forks`, {
method: 'post',
body,
})
},
// get commit sha
getCommitSha(user: string, repo: string) {
return http(`/repos/${user}/${repo}/commits`, {
method: 'get',
})
},
// creat branch
createBranch(user: string, repo: string, body: any) {
return http(`/repos/${user}/${repo}/git/refs`, {
method: 'post',
body,
})
},
// creat file contents
updateConfigFile(body: any) {
return http(`/repos/{owner}/{repo}/contents/{path}`, {
Expand Down
39 changes: 27 additions & 12 deletions src/pages/edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ import { invoke } from '@tauri-apps/api/tauri'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import github from '@/apis/github'
import { ElMessage } from 'element-plus'
import { usePakeStore } from '@/store'
const router = useRouter()
const store = usePakeStore()
const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
Expand Down Expand Up @@ -138,20 +141,32 @@ const preview = () => {
})
}
// 创建仓库
// 创建分支
const createRepo = async () => {
const res: any = await github.creatProgect({
name: 'PakePlus',
default_branch_only: false,
})
console.log('createRepo', res)
// 202 is ok
if (res.status === 202) {
localStorage.setItem('repoInfo', JSON.stringify(res.data))
ElMessage.success('Fork成功')
// 创建分支:需要有上一次提交的commit sha
const res: any = await github.createBranch(
store.userInfo.login,
'PakePlus',
{
ref: `refs/heads/${ruleForm.rename}`,
sha: store.commit.sha,
}
)
console.log('createBranch', res)
// 201 is ok
if (res.status === 201) {
const branchInfo = {
name: ruleForm.rename,
...res.data,
}
console.log('branchInfo success', branchInfo)
store.setCurrentProject(branchInfo)
ElMessage.success('项目创建成功')
} else if (res.status === 422) {
ElMessage.success('项目已经存在')
} else {
console.log('createRepo', res)
ElMessage.success('Fork失败')
console.log('branchInfo error', res)
ElMessage.success(`项目创建失败: ${res.data.message}`)
}
}
Expand Down
53 changes: 51 additions & 2 deletions src/pages/home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</div>
<!-- 设置按钮 -->
<div class="setting" @click="dialogVisible = true">
<span class="userName">{{ store.userInfo.login }}</span>
<el-icon :size="26"><Setting /></el-icon>
</div>
</div>
Expand Down Expand Up @@ -71,9 +72,12 @@ import { useRouter } from 'vue-router'
import { appWindow } from '@tauri-apps/api/window'
import githubApi from '@/apis/github'
import { ElMessage } from 'element-plus'
import { usePakeStore } from '@/store'
const router = useRouter()
const store = usePakeStore()
const token = ref(localStorage.getItem('token') || '')
const dialogVisible = ref(false)
Expand All @@ -93,6 +97,8 @@ const appList = ref([
// 跳转到新建页面
const pushEdit = () => {
// 先获取一次sha,为后续工作做准备
getCommitSha()
router.push('/edit')
}
Expand All @@ -104,17 +110,50 @@ const saveToken = () => {
// 测试token是否可用
const testToken = async () => {
const res: any = await githubApi.gitRatelimit(token.value)
// const res: any = getApiLimit()
const res: any = await githubApi.gitUserInfo(token.value)
console.log('testToken', res)
if (res.status === 200) {
ElMessage.success('Token可用')
// 本地存储并且fork仓库
localStorage.setItem('token', token.value)
store.setUser(res.data)
forkProgect()
} else {
ElMessage.error('Token不可用.')
}
}
// fork仓库以及准备工作
const forkProgect = async () => {
// fork仓库
const forkRes = await githubApi.forkProgect({
name: 'PakePlus',
default_branch_only: false,
})
console.log('forkRes', forkRes)
if (forkRes.status === 202) {
store.setRepository(forkRes.data)
}
// start仓库
const startRes = await githubApi.startProgect()
console.log('startRes', startRes)
if (startRes.status === 204) {
console.log('start仓库成功')
}
}
// 获取最后一次提交的commit sha
const getCommitSha = async () => {
const res: any = await githubApi.getCommitSha(
store.userInfo.login,
'PakePlus'
)
console.log('getCommitSha', res.data[0])
if (res.status === 200) {
store.setCommitSha(res.data[0])
}
}
onMounted(() => {
appWindow.setTitle('PakePlus')
})
Expand Down Expand Up @@ -146,6 +185,16 @@ onMounted(() => {
}
}
.setting {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
.userName {
margin-right: 6px;
}
}
.headerTool {
display: flex;
flex-direction: row;
Expand Down
Loading

0 comments on commit 129ed9e

Please sign in to comment.