Skip to content

Commit

Permalink
Release 1.0.1 (#6)
Browse files Browse the repository at this point in the history
* 1.0.1

Co-authored-by: Erick Massa Sprengel <[email protected]>
  • Loading branch information
GustavoDinizMonteiro and ericksprengel authored Jan 13, 2023
1 parent bdc3580 commit c382772
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 51 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
# acton
Aqui rodamos tudo, o que não falta é Action


```
git branch -D merge-push-on
git checkout -b merge-push-on origin/pull-requests-base-branch
git fetch prs-to-pull-requests-base-branch
for pr in prs
git merge pr on merge-push-on
if conflict
return error
git push -f origin HEAD:merge-push-on
```

```
exemplo com:
merge-push-on = sdx
pull-requests-base-branch = master
git checkout -b sdx-${timestamp} origin/master
git fetch prs-to-master
for pr in prs
git merge pr on sdx-${timestamp}
if conflict
git branch -d sdx-${timestamp}
return error
git branch -d sdx
git checkout -b sdx
git branch -d sdx-${timestamp}
```
12 changes: 5 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ description: 'Native Github action to make merge and deploy from repo.'
inputs:
token:
description: 'Github API token'
owner:
description: 'Organization'
default: 'stone-ton'
repo:
description: 'Repository name'
sdx_branch_name:
pull_requests_base_branch:
default: 'master'
description: 'Name of base branch'
merge_push_on:
default: 'sdx'
description: 'Name of default sandbox branch'
description: 'Name of branch will be created from base branch to merge PRs'


runs:
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gitmerge-action",
"version": "1.0.0",
"version": "1.0.1",
"description": "Native Github action to make merge and deploy from repo",
"main": "index.js",
"scripts": {
Expand Down
14 changes: 7 additions & 7 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const deployBranchName = 'sdx-prs-deploy'
const deployBranchName = process.env.INPUT_MERGE_PUSH_ON
const deployRefName = `heads/${deployBranchName}`
const deployRefHead = `refs/${deployRefName}`

const token = process.env.INPUT_TOKEN
const sdx = process.env.INPUT_SDX_BRANCH_NAME || 'sdx'
const ref = `heads/${sdx}`
const target = process.env.INPUT_PULL_REQUESTS_BASE_BRANCH
const ref = `heads/${target}`

const repoInfo = {
owner: process.env.INPUT_OWNER,
repo: process.env.INPUT_REPO,
}
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')

const repoInfo = { owner, repo }

module.exports = {
deployBranchName,
deployRefName,
deployRefHead,
repoInfo,
token,
target,
ref,
}
69 changes: 49 additions & 20 deletions src/helper.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,57 @@
const github = require('@actions/github')

const { deployBranchName, deployRefHead, deployRefName, repoInfo, token, ref } = require('./constants')
const { deployRefHead, deployRefName, repoInfo, token, target, ref } = require('./constants')
const octokit = github.getOctokit(token)

async function getLastCommit() {
const { data: sdxLastCommit } = await octokit.rest.repos.getCommit({
const timestamp = new Date().getTime()
const auxBranchName = `${deployRefHead}-${timestamp}`
const auxBranchRef = `${deployRefName}-${timestamp}`

async function getLastCommitSha() {
console.log(`Getting last commit from branch ${ref}`);

const { data } = await octokit.rest.repos.getCommit({
...repoInfo,
ref: ref
})
return sdxLastCommit

console.log(`Successful get commit.
Commit message: ${data.commit.message}`);

return data.sha
}

async function createBranch(commitSha) {
await octokit.rest.git.createRef({
...repoInfo,
ref: deployRefHead,
sha: commitSha
})
console.log('Deploy branch created')
async function createAuxBranch(commitSha) {
console.log(`Creating branch ${auxBranchName}`)
await createBranch(auxBranchName, commitSha)
console.log(`Successful create branch`)

return auxBranchRef
}

async function deleteBranch() {
async function deleteBranch(branchName) {
console.log(`Deleting branch ${branchName}`)

await octokit.rest.git.deleteRef({
...repoInfo,
ref: deployRefName
ref: branchName
})
console.log('Deploy branch deleted')

console.log('Successful delete branch')
}

async function mergeBranchs(pullHeadRef) {
await octokit.rest.repos.merge({
return await octokit.rest.repos.merge({
...repoInfo,
base: deployBranchName,
head: pullHeadRef,
base: auxBranchName,
head: pullHeadRef
})
}

async function getPrs() {
const { data } = await octokit.rest.pulls.list({
...repoInfo,
base: 'sdx'
base: target,
})

const prs = data.filter(pr => !pr.draft)
Expand All @@ -48,9 +60,26 @@ async function getPrs() {
return prs
}

async function recreateDeployBranch(commitSha) {
console.log(`Recreating branch ${deployRefHead}`)
await deleteBranch(deployRefName)
await createBranch(deployRefHead, commitSha)
console.log(`Successful create branch`)
}

async function createBranch(branchName, commitSha) {
await octokit.rest.git.createRef({
...repoInfo,
ref: branchName,
sha: commitSha
})
}


module.exports = {
getLastCommit,
createBranch,
recreateDeployBranch,
getLastCommitSha,
createAuxBranch,
deleteBranch,
mergeBranchs,
getPrs,
Expand Down
31 changes: 17 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
const { getLastCommit, createBranch, deleteBranch, getPrs, mergeBranchs } = require('./helper')
const { getLastCommitSha, createAuxBranch, deleteBranch, getPrs, mergeBranchs, recreateDeployBranch } = require('./helper')

async function run() {
const sdxLastCommit = await getLastCommit()
const baseLastCommit = await getLastCommitSha()

try {
await createBranch(sdxLastCommit.sha)
} catch (error) {
await deleteBranch()
await createBranch(sdxLastCommit.sha)
}
const workBranchName = await createAuxBranch(baseLastCommit)

const pullRequests = await getPrs()

// Check if all prs can be merged together in SDX based branch

let lastMergeCommitSha
for (let pull of pullRequests) {
try {
await mergeBranchs(pull.head.ref)
} catch (error) {
console.error(error.response.data.message)
throw new Error('Merge conflict')
console.log(`Merging PR ${pull.number}`)
const { data } = await mergeBranchs(pull.head.ref)

console.log(`Successful merge PR ${pull.number}`);

lastMergeCommitSha = data.sha
} finally {
await deleteBranch(workBranchName)
}
}
console.log('Merge Success!');

await recreateDeployBranch(lastMergeCommitSha)

await deleteBranch(workBranchName)
}

run()

0 comments on commit c382772

Please sign in to comment.