diff --git a/.github/workflows/detect-conflict.yaml b/.github/workflows/detect-conflict.yaml deleted file mode 100644 index fdc59728a..000000000 --- a/.github/workflows/detect-conflict.yaml +++ /dev/null @@ -1,44 +0,0 @@ -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/ diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 215f818e7..df8554523 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -9,6 +9,10 @@ on: branches: [main] jobs: + has-merge-conflict: + uses: ./.github/workflows/move-on-merge-conflict.yaml + secrets: inherit + build: uses: ./.github/workflows/build.yaml secrets: diff --git a/.github/workflows/move-on-merge-conflict.yaml b/.github/workflows/move-on-merge-conflict.yaml new file mode 100644 index 000000000..9004e9557 --- /dev/null +++ b/.github/workflows/move-on-merge-conflict.yaml @@ -0,0 +1,102 @@ +# File: .github/workflows/check-merge-conflicts.yml +name: Check Merge Conflicts and Update Jira + +on: + workflow_call: + secrets: + JIRA_AUTH: { required: true } + +jobs: + check_merge_conflicts: + environment: + name: development + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Get branches to process + id: get_branches + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const {owner, repo } = context.repo; + const { data: pullRequests } = await github.rest.pulls.list({ + owner, + repo, + state: 'open', + }); + const branches = [] + const filteredPrs = pullRequests.filter(pr => !pr.head.repo.fork && pr.head.ref.startsWith('NDT-') && !pr.draft); + for (const pr of filteredPrs) { + const prReviews = await github.rest.pulls.listReviews({ + owner, + repo, + pull_number: pr.number, + }); + const approved = prReviews.data.some(review => review.state === 'APPROVED'); + if(approved) { + branches.push(pr.head.ref); + } + } + + return branches; + - name: Process branches + env: + BRANCHES: ${{ steps.get_branches.outputs.result }} + JIRA_AUTH: ${{ secrets.JIRA_AUTH }} + run: | + set -e + branches=$(echo $BRANCHES | jq -r '.[]') + if [ -z "$branches" ]; then + echo "No branches to process." + exit 0 + fi + git config user.name "CCBC Service Account" + git config user.email "116113628+ccbc-service-account@users.noreply.github.com" + for BRANCH in $branches; do + echo "Processing branch: $BRANCH" + git fetch origin $BRANCH + git checkout $BRANCH + git merge origin/main --no-commit --no-ff || MERGE_CONFLICT=1 + if [ "$MERGE_CONFLICT" -eq 1 ]; then + echo "Merge conflict detected in branch $BRANCH" + # Extract Jira issue key from branch name + JIRA_KEY=$(echo $BRANCH | grep -o 'NDT-[0-9]\+') + if [ -z "$JIRA_KEY" ]; then + echo "No Jira key found in branch name $BRANCH" + continue + fi + echo "Jira Key: $JIRA_KEY" + # Check issue status in Jira + response=$(curl -s -X GET \ + -H "Authorization: Basic $JIRA_AUTH" \ + -H "Content-Type: application/json" \ + "https://connectivitydivision.atlassian.net/rest/api/3/issue/$JIRA_KEY") + status=$(echo "$response" | jq -r '.fields.status.name') + echo "Issue status: $status" + if [ "$status" == "PO REVIEW" ]; then + echo "Moving issue $JIRA_KEY to Merge Conflict column" + curl -X POST \ + -H "Authorization: Basic $JIRA_AUTH" \ + -H "Content-Type: application/json" \ + -d '{ + "transition": { + "id": "9" + } + }' \ + "https://connectivitydivision.atlassian.net/rest/api/3/issue/$JIRA_KEY/transitions" + else + echo "Issue $JIRA_KEY is not in PO REVIEW status" + fi + else + echo "No merge conflict in branch $BRANCH" + fi + # Clean up for next iteration + git reset --hard + git checkout main + MERGE_CONFLICT=0 + done diff --git a/.github/workflows/move-on-merge-conlifct.yaml b/.github/workflows/move-on-merge-conlifct.yaml deleted file mode 100644 index 58a6a6762..000000000 --- a/.github/workflows/move-on-merge-conlifct.yaml +++ /dev/null @@ -1,82 +0,0 @@ -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"