In Concourse we are using teliaoss/github-pr-resource
to run pull request checks when a pull request has been created in Github. One of the checks we do is npm run prettier:fix
which ensures all code is formatted to standards. If the repository shows changes then the task fails and the user has to run the command locally and push changes. This is fine, but we'd love to run npm run prettier:fix
and then commit the changes to the pull request branch and avoid requiring the user to make another commit.
The git resource is available and allows you to push to a repository, but you have to specify the branch in your yaml, I'm not aware of a way to make that dynamic. Here's a simplified example using the git resource
resource_types:
- name: pull-request
type: registry-image
source:
repository: teliaoss/github-pr-resource
resources:
- name: deployment-plan
type: git
source:
branch: main # this needs to be the pull-request branch
uri: ((git-base-uri))/myproject
- task: run-prettier
config:
platform: linux
image_resource:
type: registry-image
source:
repository: alpine/git
inputs:
- name: deployment-plan
outputs:
- name: deployment-plan-git-update
run:
path: sh
args:
- -exc
- |
git clone deployment-plan deployment-plan-git-update
cd deployment-plan-git-update
npm run prettier:fix
git add .
if [[ ! -z "$(git status --porcelain)" ]]; then
git commit -m "run prettier"
fi
- put: deployment-plan
params:
repository: deployment-plan-git-update
rebase: true
You still need to explicitly set your branch, but the answer from @Busches got me on the right path
- task: prettier
config:
platform: linux
image_resource:
type: docker-image
source:
repository: image-with-node-git-and-jq
tag: 14
inputs:
- name: pull-request
outputs:
- name: pull-request
params:
USERNAME: ((access-token.username))
ACCESS_TOKEN: ((access-token.git-access-token))
run:
path: sh
dir: pull-request
args:
- -exc
- |
export NG_CLI_ANALYTICS=false
prNumber=$(cat .git/resource/metadata.json | jq -r '.[] | select(.name=="pr") | .value')
branchName=$(curl 'https://'${USERNAME}:${ACCESS_TOKEN}'@github.nwie.net/api/v3/repos/Nationwide/roundup/pulls/'${prNumber} | jq -r '.head.ref')
git fetch
git checkout ${branchName}
# run any commands that change files here
npm install -g prettier
npm run prettier:fix
git add .
# only commit if there are changes otherwise the step fails
if [[ ! -z "$(git status --porcelain)" ]]; then
git commit -m "prettier fix"
git push --set-upstream origin ${branchName}
else
echo "no changes found"
fi