Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic github functions #11

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ module.exports = {
},
rules: {
'linebreak-style': 0,
'max-len': [
'error',
{
'code': 120,
'ignoreComments': true,
}
]
},
}
],
Expand Down
16 changes: 15 additions & 1 deletion src/components/UserInfo.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import useStore from '@/stores/store';
import { initClient, getAuthenticatedUser } from '@/util/github';
import { initClient, getAuthenticatedUser, triggerWorkflowRun, listWorkflows, enablePages, enableWorkflowPermissions, rerunWorkflow } from '@/util/github';

const store = useStore();
const loading = ref(true);
Expand All @@ -15,9 +15,20 @@ onMounted(async () => {
const response = await getAuthenticatedUser(client);
userInfo.value.avatarUrl = response.data.avatar_url;
userInfo.value.username = response.data.login;
store.setUsername(response.data.login);
loading.value = false;
})

async function test() {
const client = initClient(store.accessToken);
//const data = await enableWorkflowPermissions(client, 'vvidday', 'test');
//const data = await enablePages(client, 'vvidday', 'test');
//const data = await listWorkflows(client, 'vvidday', 'publish-RepoSense');
const data = await rerunWorkflow(client, 'vvidday', 'test', 'main.yml');
//const data = await triggerWorkflowRun(client, 'vvidday', 'test', 'main.yml', 'master');
console.log(data);
}

</script>

<template lang="pug">
Expand All @@ -33,4 +44,7 @@ onMounted(async () => {
)
.user-info-username
p {{ userInfo.username }}
button(
v-on:click="test()"
) Test
</template>
6 changes: 6 additions & 0 deletions src/stores/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ 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;
}
function setAccessToken(value: string) {
accessToken.value = value;
}
function setUsername(value: string) {
username.value = value;
}

return {
isLoggedIn,
setLogIn,
accessToken,
setAccessToken,
username,
setUsername,
};
});

Expand Down
94 changes: 93 additions & 1 deletion src/util/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}