Skip to content

Commit

Permalink
Merge pull request #417 from yanamura/avoid-merge-same-branch
Browse files Browse the repository at this point in the history
Avoid merge same branch
  • Loading branch information
yanamura authored Apr 11, 2023
2 parents ee1c166 + f114a45 commit 63862b5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
30 changes: 26 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1138,10 +1138,32 @@ const octokit = (0, github_1.getOctokit)(core.getInput('github_token'));
function merge(branch, to) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`merge branch:${branch} to: ${to}`);
const response = yield octokit.rest.repos.merge(Object.assign(Object.assign({}, github_1.context.repo), { base: to, head: branch }));
const newMasterSha = response.data.sha;
core.info(`sha = ${newMasterSha}`);
return newMasterSha;
// check already merged
const branchResponse = yield octokit.rest.repos.getBranch(Object.assign(Object.assign({}, github_1.context.repo), { branch: branch }));
const branchSha = branchResponse.data.commit.sha;
core.info(`branch_sha = ${branchSha}`);
const toResponse = yield octokit.rest.repos.getBranch(Object.assign(Object.assign({}, github_1.context.repo), { branch: to }));
const toSha = toResponse.data.commit.sha;
core.info(`to_sha = ${toSha}`);
const commits = yield octokit.rest.repos.listCommits(Object.assign(Object.assign({}, github_1.context.repo), { sha: toSha }));
let isMerged = false;
for (let i = 0; i < commits.data.length; i++) {
const commit = commits.data[i];
if (commit.sha == branchSha) {
isMerged = true;
break;
}
}
if (isMerged) {
core.info(`sha = ${toSha}`);
return toSha;
}
else {
const mergeResponse = yield octokit.rest.repos.merge(Object.assign(Object.assign({}, github_1.context.repo), { base: to, head: branch }));
const newMasterSha = mergeResponse.data.sha;
core.info(`sha = ${newMasterSha}`);
return newMasterSha;
}
});
}
function addTag(tag, sha) {
Expand Down
46 changes: 40 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,48 @@ const octokit = getOctokit(core.getInput('github_token'))

async function merge(branch: string, to: string): Promise<string> {
core.info(`merge branch:${branch} to: ${to}`)
const response = await octokit.rest.repos.merge({

// check already merged
const branchResponse = await octokit.rest.repos.getBranch({
...context.repo,
branch: branch
})
const branchSha = branchResponse.data.commit.sha
core.info(`branch_sha = ${branchSha}`)

const toResponse = await octokit.rest.repos.getBranch({
...context.repo,
base: to,
head: branch
branch: to
})
const newMasterSha = response.data.sha
core.info(`sha = ${newMasterSha}`)
return newMasterSha
const toSha = toResponse.data.commit.sha
core.info(`to_sha = ${toSha}`)

const commits = await octokit.rest.repos.listCommits({
...context.repo,
sha: toSha
})
let isMerged = false
for (let i = 0; i < commits.data.length; i++) {
const commit = commits.data[i]
if (commit.sha == branchSha) {
isMerged = true
break
}
}

if (isMerged) {
core.info(`sha = ${toSha}`)
return toSha
} else {
const mergeResponse = await octokit.rest.repos.merge({
...context.repo,
base: to,
head: branch
})
const newMasterSha = mergeResponse.data.sha
core.info(`sha = ${newMasterSha}`)
return newMasterSha
}
}

async function addTag(tag: string, sha: string): Promise<void> {
Expand Down

0 comments on commit 63862b5

Please sign in to comment.