Skip to content

Commit

Permalink
Merge pull request #3531 from bcgov/NDT-465-automate-merge-conflict-w…
Browse files Browse the repository at this point in the history
…orkflow

feat: automate move to merge conflict
  • Loading branch information
AntBush committed Sep 13, 2024
2 parents 550ae17 + a66e920 commit ff86b64
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ Add detailed description of the changes if the PR title isn't enough
-->

- [ ] Check to trigger automatic release process

- [ ] Check for automatic rebasing
44 changes: 44 additions & 0 deletions .github/workflows/detect-conflict.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Detect Merge Conflict

on:
pull_request_target:
types: [edited, synchronize]

jobs:
move-to-merge-conflict:
environment:
name: development
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Does PR have conflic
env:
HEAD_REF: ${{ github.event.pull_request.head.ref }}
id: check_conflict
run: |
git config user.name "CCBC Service Account"
git config user.email "116113628+ccbc-service-account@users.noreply.github.com"
git fetch origin
git checkout main
git pull origin main
git checkout "$HEAD_REF"
git pull origin "$HEAD_REF"
set -e
git merge main --no-commit --no-ff
MERGE_STATUS=$?
echo $MERGE_STATUS
echo "MERGE_STATUS=$MERGE_STATUS" >> $GITHUB_OUTPUT
- name: Save PR number
if: steps.check_conflict.outputs.MERGE_STATUS != 0
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
mkdir -p ./pr
echo $PR_NUMBER > ./pr/pr_number
- uses: actions/upload-artifact@v4
with:
name: pr_number
path: pr/
4 changes: 3 additions & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jobs:
const { owner, repo } = context.repo
const listOfBranches = [];
const prs = await github.rest.pulls.list({ owner, repo, state: 'open' });
const checkboxText = "[x] Check for automatic rebasing";
for (const pr of prs.data) {
// check if PR is rebaseable, not draft, and mergable before adding to list
const baseBranch = pr.base.ref;
Expand All @@ -91,7 +92,8 @@ jobs:
head: headBranch
});
if(comparison.data.behind_by > 0 && !pr.draft && pr.requested_reviewers.length > 0){
const checkboxChecked = pr.body.includes(checkboxText);
if(comparison.data.behind_by > 0 && !pr.draft && checkboxChecked){
listOfBranches.push(pr.head.ref);
}
}
Expand Down
82 changes: 82 additions & 0 deletions .github/workflows/move-on-merge-conlifct.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Move Merge Conflict Tickets

on:
workflow_run:
workflows: ['Detect Merge Conflict']
types:
- completed

jobs:
get-feature-name:
environment:
name: development
runs-on: ubuntu-latest
steps:
- name: Download Artifact
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo } = context.repo
const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner,
repo,
run_id: context.payload.workflow_run.id
})
const matchArtifact = allArtifacts.data.filter(artifact => artifact.name === 'feature-name')[0]
const download = await github.rest.actions.downloadArtifact({
owner,
repo,
artifact_id: matchArtifact.id,
archive_format: 'zip'
})
const fs = require('fs')
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr_number.zip`, Buffer.from(download.data))
- run: unzip pr_number.zip
- name: Check PR details and get feature name
id: get_feature_name
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
result-encoding: string
script: |
const fs = require('fs')
const prNumber = Number(fs.readFileSync('pr/pr_number'))
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
})
if (pr.head.repo.fork){
return
}
const featureName = pr.head.ref
const regex = /NDT-\d+/g;
const match = featureName.match(regex)
return match
- name: Check Status of issue
if: startsWith(steps.get_feature_name.outputs.result, 'NDT-')
id: get_status
run: |
response=$(curl -s -X GET \
-H "Authorization: Basic ${{ secrets.JIRA_AUTH }}" \
-H "Content-Type: application/json" \
"https://connectivitydivision.atlassian.net/rest/api/3/issue/${{ steps.get_feature_name.outputs.result }}")
echo "Jira Key: $JIRA_KEY"
echo "Response: $response"
status=$(echo "$response" | jq -r '.fields.status.name')
echo "Issue status: $status"
echo "::set-output name=status::$status"
- name: Move to Merge Conflict Column
if: steps.get_status.outputs.status == 'PO REVIEW'
run: |
curl -X POST \
-H "Authorization: Basic ${{ secrets.JIRA_AUTH }}" \
-H "Content-Type: application/json" \
-d '{
"transition": {
"id": "9"
}
}' \
"https://connectivitydivision.atlassian.net/rest/api/3/issue/$JIRA_KEY/transitions"

0 comments on commit ff86b64

Please sign in to comment.