From 03a43f35c75744b9051017b5aee049610b71c038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Bokvad=20Engar=C3=A5s?= Date: Thu, 7 Sep 2023 14:29:51 +0200 Subject: [PATCH] added branchprotection task --- .../BranchProtectionService.ts | 30 ++++++++++++++++++ src/index.ts | 31 ++----------------- 2 files changed, 33 insertions(+), 28 deletions(-) create mode 100644 src/branchprotection/BranchProtectionService.ts diff --git a/src/branchprotection/BranchProtectionService.ts b/src/branchprotection/BranchProtectionService.ts new file mode 100644 index 00000000..671f07e5 --- /dev/null +++ b/src/branchprotection/BranchProtectionService.ts @@ -0,0 +1,30 @@ +import * as core from '@actions/core'; +import * as github from '@actions/github'; +export class BranchProtectionService{ + + public static async getStateOfBranchProtection(): Promise { + console.log('Custom task is working!'); + const token = core.getInput('repo-token'); + console.log("Got the token"); + + const octokit = github.getOctokit(token); + console.log("octoKit authenticated"); + + const { owner, repo } = github.context.repo; + console.log(`Owner: ${owner}`); + console.log(`Repo: ${repo}`); + + console.log("Going to get branch protection"); + await octokit.rest.repos.getBranchProtection({ + owner: owner, + repo: repo, + branch: 'main', + }).then((response) => { + console.log("Got the branch protection"); + console.log(response.data); + }).catch((error) => { + core.warning("Error getting branch protection!"); + core.warning("Error: ", error.message); + }); + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 3ac1ce6c..9e302c83 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,41 +1,16 @@ import * as core from '@actions/core'; import * as github from '@actions/github'; +import { BranchProtectionService } from './branchprotection/BranchProtectionService'; /** * The main function for the action. * @returns {Promise} Resolves when the action is complete. */ export async function run(): Promise { try { - console.log('Custom task is working!'); - const token = core.getInput('repo-token'); - console.log("Got the token"); - const octokit = github.getOctokit(token); - console.log("octoKit authenticated"); - console.log("Going to get branch protection"); - const { owner, repo } = github.context.repo; - console.log(`Owner: ${owner}`); - console.log(`Repo: ${repo}`); - - const branchProtection = await octokit.rest.repos.getBranchProtection({ - owner: owner, - repo: repo, - branch: 'main', - }).then((response) => { - console.log("Got the branch protection"); - console.log(response.data); - }).catch((error) => { - core.warning("Error getting branch protection"); - core.warning(error.message); - }); - // if(!branchProtection){ - // console.log("Branch protection not found"); - // } - // else{ - // console.log("Logging branch protection"); - // console.log(branchProtection.data); - // } + await BranchProtectionService.getStateOfBranchProtection(); + } catch (error) { // Fail the workflow run if an error occurs if (error instanceof Error) core.setFailed(error.message);