Use a custom message for Cypress Dashboard run
Advent day 25: How to overwrite the dreadful "Merge SHA1 into SHA2" Dashboard run title
Merry Christmas 🎄
When recording to Cypress Dashboard, some CI providers create a merge commit, which means the run title is a cryptic “Merge … into …” like below:
I think this is one of the most common recurring questions asked by Cypress users. Ok, here is how you put your own message there. First, let’s trace where this Git information is read inside Cypress code: in the file record.js
const commitInfo = require('@cypress/commit-info')
const debugCiInfo =
require('debug')('cypress:server:record:ci-info')
...
// at some point when creating a Dashboard run
commitInfo.commitInfo(projectRoot)
.then((git) => {
debugCiInfo('found the following git information')
debugCiInfo(git)
We can look at the cypress-io/commit-info to see how it gets the commit message, the author, etc
const debug = require('debug')('commit-info')
// common git commands for getting basic info
// https://git-scm.com/docs/git-show
const gitCommands = {
branch: 'git rev-parse --abbrev-ref HEAD',
message: 'git show -s --pretty=%B',
subject: 'git show -s --pretty=%s',
body: 'git show -s --pretty=%b',
email: 'git show -s --pretty=%ae',
author: 'git show -s --pretty=%an',
sha: 'git show -s --pretty=%H',
timestamp: 'git show -s --pretty=%ct',
remoteOriginUrl: 'git config --get remote.origin.url'
}
Good, this info is read using “git …” commands. But there are fallbacks - if the git info is unavailable, the Commit-info module will try reading certain environment variables.
branch: COMMIT_INFO_BRANCH
message: COMMIT_INFO_MESSAGE
email: COMMIT_INFO_EMAIL
author: COMMIT_INFO_AUTHOR
sha: COMMIT_INFO_SHA
timestamp: COMMIT_INFO_TIMESTAMP
remote: COMMIT_INFO_REMOTE
So there is our answer: remove the Git info we do not want, and set COMMIT_INFO_
variables with the values we want to display. Here is my pull request CI file from the example repo run_message. I read the current Git info into the environment variables and then delete the .git folder.
- name: Checkout
uses: actions/checkout@v2
- name: Save Git values
# pass env variables from this step to other steps
# using GitHub Actions environment file
# https://docs.github.com/en/actions/learn-github-actions/workflow-commands-for-github-actions#environment-files
run: |
echo COMMIT_INFO_BRANCH=$(git rev-parse --abbrev-ref HEAD) >> $GITHUB_ENV
echo COMMIT_INFO_MESSAGE=My custom message >> $GITHUB_ENV
echo COMMIT_INFO_EMAIL=$(git show -s --pretty=%ae) >> $GITHUB_ENV
echo COMMIT_INFO_AUTHOR=$(git show -s --pretty=%an) >> $GITHUB_ENV
echo COMMIT_INFO_SHA=$(git show -s --pretty=%H) >> $GITHUB_ENV
echo COMMIT_INFO_TIMESTAMP=$(git show -s --pretty=%ct) >> $GITHUB_ENV
echo COMMIT_INFO_REMOTE=$(git config --get remote.origin.url) >> $GITHUB_ENV
# delete the .git folder afterwords to use the environment values
rm -rf .git
Of course, it is up to you how you want to set the COMMIT_INFO_MESSAGE
value - it could be the head branch commit subject, or something else. If you are using run-cy-on-ci you could pass the message text from the command line. In my case, it was simply the “My custom message” string.
Tip: notice how both record.js and commit-info modules use the debug module to output verbose logs. We can see the Git info during Cypress run by enabling both log sources by defining an environment variable DEBUG
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
# see verbose debug logs with commit info
DEBUG: cypress:server:record:ci-info,commit-info