|
1 | 1 | name: Merge Dependabot PRs |
| 2 | + |
2 | 3 | on: |
3 | 4 | schedule: |
4 | 5 | - cron: "0 9 * * 1" # Run this workflow every Monday at 9:00 |
|
7 | 8 | jobs: |
8 | 9 | merge: |
9 | 10 | runs-on: ubuntu-latest |
10 | | - steps: |
11 | | - - name: Check out code |
12 | | - uses: actions/checkout@v3 |
13 | | - with: |
14 | | - ref: master |
15 | 11 |
|
16 | | - - name: Authenticate GitHub CLI |
17 | | - run: echo "${{ secrets.DEPENDABOT_MERGER_PAT }}" | gh auth login --with-token |
18 | | - |
19 | | - - name: Set Git user identity |
| 12 | + steps: |
| 13 | + - name: Get current on-call |
| 14 | + id: on-call |
20 | 15 | run: | |
21 | | - git config user.email "[email protected]" |
22 | | - git config user.name "Dependabot Merger Bot" |
| 16 | + now=$(date -u +%Y-%m-%dT%H:%M:%SZ) |
| 17 | + end_time=$(date -u -d '+24 hour' +%Y-%m-%dT%H:%M:%SZ) |
23 | 18 |
|
24 | | - - name: Get current date and time |
25 | | - id: datetime |
26 | | - run: echo "date=$(date +'%m-%d-%Y-%H-%M')" >> $GITHUB_OUTPUT |
| 19 | + oncall=$(curl --request GET \ |
| 20 | + --url "https://api.pagerduty.com/oncalls?since=$now&until=$end_time&schedule_ids[]=PQLHTOP" \ |
| 21 | + --header 'Accept: application/vnd.pagerduty+json;version=2' \ |
| 22 | + --header "Authorization: Token token=${{ secrets.PAGERDUTY_TOKEN }}" \ |
| 23 | + --header 'Content-Type: application/json' ) |
27 | 24 |
|
28 | | - - name: Create new branch based on date and time |
29 | | - run: | |
30 | | - NEW_BRANCH="dependabot-test-${{ steps.datetime.outputs.date }}" |
31 | | - git checkout -b $NEW_BRANCH |
32 | | - git push origin $NEW_BRANCH |
33 | | -
|
34 | | - - name: Get list of PRs from dependabot |
35 | | - id: pr_list |
36 | | - run: | |
37 | | - PR_LIST=$(gh pr list --json number,author,headRefName --jq '.[] | select( .author.is_bot == true and .author.login == "app/dependabot" ) | "\(.number) \(.headRefName)"') |
38 | | - PR_LIST=$(echo "$PR_LIST" | tr -d '\r') |
39 | | - if [ -z "$PR_LIST" ]; then |
40 | | - echo "No PRs from dependabot found." |
41 | | - exit 0 |
42 | | - fi |
| 25 | + engineer_name=$(echo "$oncall" | jq -r '.oncalls[0].user.summary') |
43 | 26 |
|
44 | | - PR_COUNT=$(echo "$PR_LIST" | wc -l) |
45 | | - echo "$PR_COUNT PR's to be merged." |
| 27 | + declare -A engineer_to_slackid |
| 28 | + engineer_to_slackid=( |
| 29 | + ["Nipun Singh"]="U02AMC70R6E" |
| 30 | + ["Jagadeesh Karicherla"]="U038BDE0XUZ" |
| 31 | + ["Gabe De Luna"]="U02MDA0PHK5" |
| 32 | + ["Ernest Cho"]="UCV77QDSL" |
| 33 | + ["Nidhi Dixit"]="U02GDFBP88N" |
| 34 | + ) |
46 | 35 |
|
47 | | - EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) |
48 | | - echo "prs<<$EOF" >> $GITHUB_OUTPUT |
49 | | - echo "$PR_LIST" >> $GITHUB_OUTPUT |
50 | | - echo "$EOF" >> $GITHUB_OUTPUT |
| 36 | + slack_id=${engineer_to_slackid["$engineer_name"]} |
| 37 | + echo "oncall_slack_id=$slack_id" >> $GITHUB_OUTPUT |
51 | 38 |
|
52 | | - - name: Merge PRs into new branch |
53 | | - run: | |
54 | | - NEW_BRANCH="dependabot-test-${{ steps.datetime.outputs.date }}" |
55 | | - git checkout $NEW_BRANCH |
56 | | - PR_LIST="${{ steps.pr_list.outputs.prs }}" |
57 | | - while IFS= read -r line; do |
58 | | - IFS=' ' read -r PR_NUMBER BRANCH_NAME <<< "$line" |
59 | | - echo "Merging PR #$PR_NUMBER from branch $BRANCH_NAME into $NEW_BRANCH..." |
60 | | - git fetch origin $BRANCH_NAME |
61 | | - git merge --no-commit --allow-unrelated-histories --strategy-option=theirs origin/$BRANCH_NAME |
62 | | - echo "Pushing changes to $NEW_BRANCH..." |
63 | | - git commit -m "Merged PR #$PR_NUMBER into $NEW_BRANCH" |
64 | | - git push origin $NEW_BRANCH |
65 | | - done <<< "$PR_LIST" |
| 39 | + - name: Create PR |
| 40 | + uses: actions/github-script@v7 |
| 41 | + id: create-pr |
| 42 | + with: |
| 43 | + script: | |
| 44 | + const uniqueBranchName = 'dependabot-combined-prs-' + Date.now().toString(); |
| 45 | + const pulls = await github.paginate('GET /repos/:owner/:repo/pulls', { |
| 46 | + owner: context.repo.owner, |
| 47 | + repo: context.repo.repo |
| 48 | + }); |
| 49 | + let branchesAndPRStrings = []; |
| 50 | + let baseBranch = null; |
| 51 | + let baseBranchSHA = null; |
| 52 | + for (const pull of pulls) { |
| 53 | + const branch = pull['head']['ref']; |
| 54 | + if (branch.startsWith('dependabot/')) { |
| 55 | + console.log('Branch matched prefix. Adding to array: ' + branch); |
| 56 | + const prString = '#' + pull['number'] + ' ' + pull['title']; |
| 57 | + branchesAndPRStrings.push({ branch, prString }); |
| 58 | + baseBranch = pull['base']['ref']; |
| 59 | + baseBranchSHA = pull['base']['sha']; |
| 60 | + } |
| 61 | + } |
| 62 | + if (branchesAndPRStrings.length == 0) { |
| 63 | + core.setFailed('There are no open dependabot PRs.'); |
| 64 | + return; |
| 65 | + } |
| 66 | + try { |
| 67 | + await github.rest.git.createRef({ |
| 68 | + owner: context.repo.owner, |
| 69 | + repo: context.repo.repo, |
| 70 | + ref: 'refs/heads/' + uniqueBranchName, |
| 71 | + sha: baseBranchSHA |
| 72 | + }); |
| 73 | + } catch (error) { |
| 74 | + console.log(error); |
| 75 | + core.setFailed('Failed to create combined branch'); |
| 76 | + return; |
| 77 | + } |
66 | 78 |
|
67 | | - - name: Merge process status |
68 | | - run: | |
69 | | - echo "Merging process completed successfully!" |
70 | | - echo "New branch name: dependabot-test-${{ steps.datetime.outputs.date }}" |
| 79 | + let combinedPRs = []; |
| 80 | + let mergeFailedPRs = []; |
| 81 | + for(const { branch, prString } of branchesAndPRStrings) { |
| 82 | + try { |
| 83 | + await github.rest.repos.merge({ |
| 84 | + owner: context.repo.owner, |
| 85 | + repo: context.repo.repo, |
| 86 | + base: uniqueBranchName, |
| 87 | + head: branch, |
| 88 | + }); |
| 89 | + console.log('Merged branch ' + branch); |
| 90 | + combinedPRs.push(prString); |
| 91 | + } catch (error) { |
| 92 | + console.log('Failed to merge branch ' + branch); |
| 93 | + mergeFailedPRs.push(prString); |
| 94 | + } |
| 95 | + } |
71 | 96 |
|
72 | | - - name: Generate PR links |
73 | | - id: pr_links |
74 | | - run: | |
75 | | - PR_LIST="${{ steps.pr_list.outputs.prs }}" |
76 | | - PR_LINKS="" |
77 | | - while IFS= read -r line; do |
78 | | - IFS=' ' read -r PR_NUMBER BRANCH_NAME <<< "$line" |
79 | | - PR_URL="https://github.com/${GITHUB_REPOSITORY}/pull/$PR_NUMBER" |
80 | | - PR_LINKS+="\n• <$PR_URL|#${PR_NUMBER}: ${BRANCH_NAME}>" |
81 | | - done <<< "$PR_LIST" |
82 | | - EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) |
83 | | - echo "pr_links<<$EOF" >> $GITHUB_OUTPUT |
84 | | - echo "$PR_LINKS" >> $GITHUB_OUTPUT |
85 | | - echo "$EOF" >> $GITHUB_OUTPUT |
| 97 | + console.log('Creating combined PR'); |
| 98 | + const combinedPRsString = combinedPRs.join('\n'); |
| 99 | + let body = '✅ This PR was created by the Merge Dependabot PRs action by combining the following dependabot PRs:\n' + combinedPRsString; |
| 100 | + if(mergeFailedPRs.length > 0) { |
| 101 | + const mergeFailedPRsString = mergeFailedPRs.join('\n'); |
| 102 | + body += '\n\n⚠️ The following dependabot PRs were left out due to merge conflicts:\n' + mergeFailedPRsString |
| 103 | + } |
| 104 | + let response = await github.rest.pulls.create({ |
| 105 | + owner: context.repo.owner, |
| 106 | + repo: context.repo.repo, |
| 107 | + title: 'Combined Dependabot PR', |
| 108 | + head: uniqueBranchName, |
| 109 | + base: baseBranch, |
| 110 | + body: body |
| 111 | + }); |
| 112 | + console.log('Created combined PR: ' + response.data.html_url); |
| 113 | + core.setOutput('pr_url', response.data.html_url); |
| 114 | + core.setOutput('pr_list', combinedPRsString); |
86 | 115 |
|
87 | 116 | - name: Post to a Slack channel |
88 | 117 | |
| 118 | + id: slack |
89 | 119 | with: |
90 | 120 | channel-id: "C03RTLRKJQP" |
91 | 121 | payload: | |
92 | 122 | { |
93 | | - "blocks": [ |
| 123 | + "text": "iOS: New Dependabot PR Awaiting Review", |
| 124 | + "blocks": [ |
94 | 125 | { |
95 | | - "type": "header", |
96 | | - "text": { |
| 126 | + "type": "header", |
| 127 | + "text": { |
97 | 128 | "type": "plain_text", |
98 | | - "text": "⚡️ New iOS Dependabot Testing Branch", |
| 129 | + "text":"📱🔧 iOS: New Dependabot PR Awaiting Review", |
99 | 130 | "emoji": true |
100 | | - } |
| 131 | + } |
101 | 132 | }, |
102 | 133 | { |
103 | | - "type": "section", |
104 | | - "text": { |
| 134 | + "type": "section", |
| 135 | + "text": { |
105 | 136 | "type": "mrkdwn", |
106 | | - "text": "*Included PRs:*${{ steps.pr_links.outputs.pr_links }}" |
107 | | - } |
| 137 | + "text": "*Included PRs:*\n${{ toJSON(steps.create-pr.outputs.pr_list) }}\n\n\nCurrent On-Call: *<${{ steps.on-call.outputs.oncall_slack_id }}>*" |
| 138 | + } |
108 | 139 | }, |
109 | 140 | { |
110 | | - "type": "actions", |
111 | | - "elements": [ |
112 | | - { |
113 | | - "type": "button", |
114 | | - "text": { |
115 | | - "type": "plain_text", |
116 | | - "text": "Checkout Test Branch", |
117 | | - "emoji": true |
118 | | - }, |
119 | | - "value": "branch-button", |
120 | | - "url": "https://github.com/${{ github.repository }}/tree/dependabot-test-${{ steps.datetime.outputs.date }}", |
121 | | - "action_id": "link-action" |
122 | | - } |
123 | | - ] |
| 141 | + "type": "actions", |
| 142 | + "elements": [ |
| 143 | + { |
| 144 | + "type": "button", |
| 145 | + "text": { |
| 146 | + "type": "plain_text", |
| 147 | + "text": ":github-pull-request-open: View Combined PR", |
| 148 | + "emoji": true |
| 149 | + }, |
| 150 | + "value": "pr-button", |
| 151 | + "url": "${{ steps.create-pr.outputs.pr_url }}", |
| 152 | + "action_id": "link-action", |
| 153 | + "style": "primary" |
| 154 | + } |
| 155 | + ] |
124 | 156 | } |
125 | 157 | ] |
126 | 158 | } |
|
0 commit comments