Skip to content

Commit

Permalink
add: code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
shqear93 committed Apr 30, 2024
1 parent 48e04c1 commit 57ea3ad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
25 changes: 14 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33746,19 +33746,11 @@ async function run() {
try {
const checkNames = core.getInput('check-names').split(', ');
const { owner, repo } = github.context.repo;
let branch = null;

if (core.getInput('target-branch')) {
branch = core.getInput('target-branch');
} else if (github.context.payload.pull_request) {
branch = github.context.payload.pull_request.head.ref;
} else {
const repoInfo = await octokit.rest.repos.get({ owner, repo });
branch = repoInfo.data.default_branch; // default branch
}

const token = core.getInput('github-token');
const octokit = github.getOctokit(token);
const branch = await getBranchName(octokit, owner, repo);

core.info(`Branch: ${branch}`);

const checksResult = await octokit.rest.checks.listForRef({ owner, repo, ref: branch });

Expand Down Expand Up @@ -33788,6 +33780,17 @@ async function run() {
}
}

async function getBranchName(octokit, owner, repo) {
return core.getInput('target-branch') ||
github.context?.payload?.pull_request?.head?.ref ||
getDefaultBranch(octokit, owner, repo);
}

async function getDefaultBranch(octokit, owner, repo) {
const repoInfo = await octokit.rest.repos.get({ owner, repo });
return repoInfo.data.default_branch; // default branch
}

run();
})();

Expand Down
25 changes: 14 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ async function run() {
try {
const checkNames = core.getInput('check-names').split(', ');
const { owner, repo } = github.context.repo;
let branch = null;

if (core.getInput('target-branch')) {
branch = core.getInput('target-branch');
} else if (github.context.payload.pull_request) {
branch = github.context.payload.pull_request.head.ref;
} else {
const repoInfo = await octokit.rest.repos.get({ owner, repo });
branch = repoInfo.data.default_branch; // default branch
}

const token = core.getInput('github-token');
const octokit = github.getOctokit(token);
const branch = await getBranchName(octokit, owner, repo);

core.info(`Branch: ${branch}`);

const checksResult = await octokit.rest.checks.listForRef({ owner, repo, ref: branch });

Expand Down Expand Up @@ -47,4 +39,15 @@ async function run() {
}
}

async function getBranchName(octokit, owner, repo) {
return core.getInput('target-branch') ||
github.context?.payload?.pull_request?.head?.ref ||
getDefaultBranch(octokit, owner, repo);
}

async function getDefaultBranch(octokit, owner, repo) {
const repoInfo = await octokit.rest.repos.get({ owner, repo });
return repoInfo.data.default_branch; // default branch
}

run();

0 comments on commit 57ea3ad

Please sign in to comment.