From 885726727385f24f8094fa42b9be3c69260efbd3 Mon Sep 17 00:00:00 2001 From: afrocandy Date: Mon, 15 May 2023 16:51:21 +0900 Subject: [PATCH] Build dist/index.js for #2 (#3) * Feature/add options (#1) * add 'add' action and ghBaseUrl * use env for base url * update nodejs ver 12 to 16 * update README.md --- README.md | 2 +- dist/index.js | 65 ++++++++++++++++++++++++--------------------------- 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index bdb623f..4952d89 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ This action will set / delete Branch Protection rules on specified branches of G ``` - name: Run Branch Protection - uses: venh/branch-protection@v1.0.0 + uses: venh/branch-protection@v1.1.0 with: token: '${{ secrets.GITHUB_ADMIN_ACCESS_TOKEN }}' org: Name-Of-Your-GitHub-Organization diff --git a/dist/index.js b/dist/index.js index 52331f8..e482a51 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3100,7 +3100,7 @@ var __webpack_exports__ = {}; // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. (() => { const core = __nccwpck_require__(186); -const { request } = __nccwpck_require__(234); +const { request: orgRequest } = __nccwpck_require__(234); const fs = __nccwpck_require__(747) var excludedReposPath = ''; @@ -3113,29 +3113,43 @@ async function run() { excludedReposPath = core.getInput("excludedReposPath"); includedReposPath = core.getInput("includedReposPath"); const action = core.getInput("action"); - const limit = 100; + const canDeleteProtection = action == 'set' || action == 'delete'; + const canSetProtection = action == 'set' || action == 'add'; + var rulesObj; var branches; try { if(!fs.existsSync(rulesPath)){ throw "Rules JSON is missing." } + + const request = orgRequest.defaults({ + baseUrl: process.env.GITHUB_API_URL || 'https://api.github.com', + headers: { + authorization: "token " + token, + }, + }) const rules = fs.readFileSync(rulesPath, {encoding:'utf8', flag:'r'}); rulesObj = JSON.parse(rules); keys = Object.keys(rulesObj); - var repos = await getFinalRepos(token, orgName); + var repos = await getFinalRepos(request, orgName); for (let i = 0; i < repos.length; i++) { - branches = await getBranches(token, repos[i], keys); + branches = await getBranches(request, repos[i], keys); for (let j = 0; j < branches.length; j++) { if(branches[j].protected){ + if (!canDeleteProtection) { + console.log("Skip Branch Protection for " + branches[j].name + " branch of " + repos[i]); + continue; + } + console.log("Deleting Branch Protection for " + branches[j].name + " branch of " + repos[i]); core.debug("Deleting Branch Protection for " + branches[j].name + " branch of " + repos[i]); - await deleteProtection(token, repos[i], branches[j].name); + await deleteProtection(request, repos[i], branches[j].name); } - if(action == "set"){ + if(canSetProtection){ console.log("Setting Branch Protection for " + branches[j].name + " branch of " + repos[i]); core.debug("Setting Branch Protection for " + branches[j].name + " branch of " + repos[i]); - await setProtection(token, repos[i], branches[j].name, rulesObj[branches[j].name] ) + await setProtection(request, repos[i], branches[j].name, rulesObj[branches[j].name] ) } } } @@ -3146,7 +3160,7 @@ async function run() { } } -async function setProtection(token, repoName, branchName, ruleData){ +async function setProtection(request, repoName, branchName, ruleData){ const url = "/repos/" + repoName + "/branches/" + branchName + "/protection" if(ruleData == ""){ ruleData = { @@ -3159,9 +3173,6 @@ async function setProtection(token, repoName, branchName, ruleData){ } try { const result = await request("PUT " + url, { - headers: { - authorization: "token " + token, - }, data: ruleData }); //console.log(result.data); @@ -3173,14 +3184,10 @@ async function setProtection(token, repoName, branchName, ruleData){ } } -async function deleteProtection(token, repoName, branchName){ +async function deleteProtection(request, repoName, branchName){ const url = "/repos/" + repoName + "/branches/" + branchName + "/protection" try{ - const result = await request("DELETE " + url, { - headers: { - authorization: "token " + token, - } - }); + const result = await request("DELETE " + url); if(result.status != 204){ throw "Exception occured during Delete Protection"; } @@ -3191,15 +3198,11 @@ async function deleteProtection(token, repoName, branchName){ } } -async function getBranches(token, repoName, branchNames){ +async function getBranches(request, repoName, branchNames){ branchInfoArr = []; const url = "/repos/" + repoName + "/branches" try { - const result = await request("GET " + url, { - headers: { - authorization: "token " + token, - } - }); + const result = await request("GET " + url); branchData = result.data; for (let j = 0; j < branchData.length; j++) { const element = branchData[j]; @@ -3215,13 +3218,10 @@ async function getBranches(token, repoName, branchNames){ return branchInfoArr; } -async function getRepoCount(token, orgName){ +async function getRepoCount(request, orgName){ repoCnt = 0; try { const result = await request("GET /orgs/{org}/repos", { - headers: { - authorization: "token " + token, - }, org: orgName, per_page:1, type: "all" @@ -3241,13 +3241,10 @@ function getPageCount(itemCount, limit){ return pageCount; } -async function getPagedRepos(token, orgName, pageNum, limit){ +async function getPagedRepos(request, orgName, pageNum, limit){ var repos = []; try { const result = await request("GET /orgs/{org}/repos", { - headers: { - authorization: "token " + token, - }, org: orgName, per_page:limit, type: "all", @@ -3264,7 +3261,7 @@ async function getPagedRepos(token, orgName, pageNum, limit){ return repos; } -async function getFinalRepos(token, orgName){ +async function getFinalRepos(request, orgName){ repos = []; includedRepos = []; limit = 100; @@ -3276,12 +3273,12 @@ async function getFinalRepos(token, orgName){ } return includedRepos; } - repoCount = await getRepoCount(token, orgName); + repoCount = await getRepoCount(request, orgName); pageCnt = getPageCount(repoCount, limit); excludedRepos = getReposFromFile(excludedReposPath); for (let i = 0; i < pageCnt; i++) { i = i + 1; - pagedRepos = await getPagedRepos(token, orgName, i, limit); + pagedRepos = await getPagedRepos(request, orgName, i, limit); for (let j = 0; j < pagedRepos.length; j++) { repoShortName = pagedRepos[j].replace(orgName + "/",""); if(!excludedRepos.includes(repoShortName)) {