-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👷 use devflow to merge main into staging (#2917)
- Loading branch information
1 parent
92908d7
commit 6b867f7
Showing
1 changed file
with
24 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,31 @@ | ||
'use strict' | ||
|
||
const { printLog, printError, runMain } = require('../lib/execution-utils') | ||
const { printLog, runMain, fetchHandlingError } = require('../lib/execution-utils') | ||
const { command } = require('../lib/command') | ||
const { initGitConfig } = require('../lib/git-utils') | ||
|
||
const REPOSITORY = process.env.GIT_REPOSITORY | ||
const CURRENT_STAGING_BRANCH = process.env.CURRENT_STAGING | ||
const CI_COMMIT_SHA = process.env.CI_COMMIT_SHA | ||
const CI_COMMIT_SHORT_SHA = process.env.CI_COMMIT_SHORT_SHA | ||
const CI_COMMIT_REF_NAME = process.env.CI_COMMIT_REF_NAME | ||
|
||
runMain(() => { | ||
initGitConfig(REPOSITORY) | ||
command`git fetch --no-tags origin ${CURRENT_STAGING_BRANCH}`.run() | ||
command`git checkout ${CURRENT_STAGING_BRANCH} -f`.run() | ||
command`git pull origin ${CURRENT_STAGING_BRANCH}`.run() | ||
|
||
// Commit can already be merged if someone merge main into his branch and | ||
// run to-staging before the end of this scripts | ||
if (isCommitAlreadyMerged()) { | ||
printLog('Already merged.') | ||
return | ||
} | ||
|
||
printLog(`Merging branch '${CI_COMMIT_REF_NAME}' (${CI_COMMIT_SHORT_SHA}) into ${CURRENT_STAGING_BRANCH}...`) | ||
try { | ||
command`git merge --no-ff ${CI_COMMIT_SHA}`.run() | ||
} catch (error) { | ||
const diff = command`git diff`.run() | ||
printError( | ||
`Conflicts:\n${diff}\n` + | ||
'See "How to fix staging" in Confluence for help: https://datadoghq.atlassian.net/wiki/spaces/FRON/pages/2548269306/How+to+fix+staging+conflicts' | ||
) | ||
throw error | ||
const REPOSITORY = process.env.APP | ||
const CURRENT_STAGING = process.env.CURRENT_STAGING | ||
const DEVFLOW_AUTH_TOKEN = command`authanywhere --audience sdm`.run().split(' ')[2].trim() | ||
const DEVFLOW_API_URL = 'https://devflow-api.us1.ddbuild.io/internal/api/v2/devflow/execute/' | ||
const SUCESS_FEEDBACK_LEVEL = 'FEEDBACK_LEVEL_INFO' | ||
|
||
runMain(async () => { | ||
const rawResponse = await fetchHandlingError( | ||
`${DEVFLOW_API_URL}/update-branch?repository=${REPOSITORY}&branch=${CURRENT_STAGING}`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${DEVFLOW_AUTH_TOKEN}`, | ||
}, | ||
} | ||
) | ||
const jsonResponse = await rawResponse.json() | ||
|
||
const isSuccess = jsonResponse.state.feedbacks[0].level === SUCESS_FEEDBACK_LEVEL | ||
const message = jsonResponse.state.feedbacks[0].message | ||
|
||
if (!isSuccess) { | ||
throw new Error(message) | ||
} | ||
|
||
const commitMessage = command`git show -s --format=%B`.run() | ||
const newSummary = `Merge branch '${CI_COMMIT_REF_NAME}' (${CI_COMMIT_SHORT_SHA}) with ${CURRENT_STAGING_BRANCH}` | ||
const message = `${newSummary}\n\n${commitMessage}` | ||
|
||
command`git commit --amend -m ${message}`.run() | ||
command`git push origin ${CURRENT_STAGING_BRANCH}`.run() | ||
|
||
printLog('Merge done.') | ||
printLog(message) | ||
}) | ||
|
||
function isCommitAlreadyMerged() { | ||
try { | ||
command`git merge-base --is-ancestor ${CI_COMMIT_SHA} ${CURRENT_STAGING_BRANCH}`.run() | ||
return true | ||
} catch { | ||
return false | ||
} | ||
} |