From 761d01bd4e9006d645e0ef88180395c3bfa4c467 Mon Sep 17 00:00:00 2001 From: vvidday Date: Mon, 13 Feb 2023 09:40:29 +0800 Subject: [PATCH] Add basic github functions and testing component --- .eslintrc.cjs | 7 +++ src/components/UserInfo.vue | 16 ++++++- src/stores/store.ts | 6 +++ src/util/github.ts | 94 ++++++++++++++++++++++++++++++++++++- 4 files changed, 121 insertions(+), 2 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 4e8fb02..f15ade2 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -45,6 +45,13 @@ module.exports = { }, rules: { 'linebreak-style': 0, + 'max-len': [ + 'error', + { + 'code': 120, + 'ignoreComments': true, + } + ] }, } ], diff --git a/src/components/UserInfo.vue b/src/components/UserInfo.vue index b34c567..de62380 100644 --- a/src/components/UserInfo.vue +++ b/src/components/UserInfo.vue @@ -1,7 +1,7 @@ \ No newline at end of file diff --git a/src/stores/store.ts b/src/stores/store.ts index 666e0dc..142f741 100644 --- a/src/stores/store.ts +++ b/src/stores/store.ts @@ -4,6 +4,7 @@ import { ref } from 'vue'; const useStore = defineStore('store', () => { const isLoggedIn = ref(false); const accessToken = ref(''); + const username = ref(''); function setLogIn(value: boolean) { isLoggedIn.value = value; @@ -11,12 +12,17 @@ const useStore = defineStore('store', () => { function setAccessToken(value: string) { accessToken.value = value; } + function setUsername(value: string) { + username.value = value; + } return { isLoggedIn, setLogIn, accessToken, setAccessToken, + username, + setUsername, }; }); diff --git a/src/util/github.ts b/src/util/github.ts index 14035ed..1958513 100644 --- a/src/util/github.ts +++ b/src/util/github.ts @@ -9,7 +9,7 @@ function dec2hex(dec: number) { } // Generates a random string for use as the 'state' when starting oAuth -// See https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#1-request-a-users-github-identity +// https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#1-request-a-users-github-identity function generateId(len?: number) { const arr = new Uint8Array((len || 40) / 2); window.crypto.getRandomValues(arr); @@ -40,3 +40,95 @@ export async function getAuthenticatedUser(client: Octokit) { const data = await client.request('GET /user'); return data; } + +export async function forkRepoSense(client: Octokit) { + const data = await client.request('POST /repos/reposense/publish-RepoSense/forks'); + return data; +} + +export async function enableActions(client: Octokit, owner: string, repo: string) { + const data = await client.request('PUT /repos/{owner}/{repo}/actions/permissions', { + owner, + repo, + enabled: true, + allowed_actions: 'all', + }); + return data; +} + +export async function listWorkflows(client: Octokit, owner: string, repo: string) { + const data = await client.request('GET /repos/{owner}/{repo}/actions/workflows', { + owner, + repo, + }); + return data; +} + +// First call needed to get specific run_id of workflow to rerun. +// To verify order of runs (so we can always get latest), consider separating into 2 methods or at least helper +export async function rerunWorkflow(client: Octokit, owner: string, repo: string, workflowId: string) { + const data = await client.request('GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs', { + owner, + repo, + workflow_id: workflowId, + }); + console.log(data); + if (data.data.workflow_runs.length > 0) { + const runId = data.data.workflow_runs[0].id; + const response = await client.request('POST /repos/{owner}/{repo}/actions/runs/{run_id}/rerun', { + owner, + repo, + run_id: runId, + }); + return response; + } + return null; +} + +export async function enableWorkflowPermissions(client: Octokit, owner: string, repo: string) { + const data = await client.request('PUT /repos/{owner}/{repo}/actions/permissions/workflow', { + owner, + repo, + default_workflow_permissions: 'write', + }); + return data; +} + +// Need to update actions to use workflow_dispatch, better to not use if possible. Use rerunWorkflow instead +// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_dispatch +export async function triggerWorkflowRun( + client: Octokit, + owner: string, + repo: string, + workflowId: string, + ref: string, +) { + const data = await client.request('POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches', { + owner, + repo, + workflow_id: workflowId, + ref, + }); + return data; +} + +export async function createRepoFromTemplate(client: Octokit, name: string) { + const data = await client.request('POST /repos/{template_owner}/{template_repo}/generate', { + template_owner: 'vvidday', + template_repo: 'publish-RepoSense', + name, + include_all_branches: true, + }); + return data; +} + +export async function enablePages(client: Octokit, owner: string, repo: string) { + const data = await client.request('POST /repos/{owner}/{repo}/pages', { + owner, + repo, + source: { + branch: 'gh-pages', + }, + }); + return data; +}