-
Notifications
You must be signed in to change notification settings - Fork 230
160 lines (149 loc) · 6.06 KB
/
dependabot-merger.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
name: Merge Dependabot PRs
on:
schedule:
- cron: "0 9 * * 1" # Run this workflow every Monday at 9:00
workflow_dispatch:
jobs:
merge:
runs-on: ubuntu-latest
steps:
- name: Get current on-call
id: on-call
run: |
now=$(date -u +%Y-%m-%dT%H:%M:%SZ)
end_time=$(date -u -d '+24 hour' +%Y-%m-%dT%H:%M:%SZ)
oncall=$(curl --request GET \
--url "https://api.pagerduty.com/oncalls?since=$now&until=$end_time&schedule_ids[]=PQLHTOP" \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header "Authorization: Token token=${{ secrets.PAGERDUTY_TOKEN }}" \
--header 'Content-Type: application/json' )
engineer_name=$(echo "$oncall" | jq -r '.oncalls[0].user.summary')
declare -A engineer_to_slackid
engineer_to_slackid=(
["Nipun Singh"]="U02AMC70R6E"
["Jagadeesh Karicherla"]="U038BDE0XUZ"
["Gabe De Luna"]="U02MDA0PHK5"
["Ernest Cho"]="UCV77QDSL"
["Nidhi Dixit"]="U02GDFBP88N"
)
slack_id=${engineer_to_slackid["$engineer_name"]}
echo "oncall_slack_id=$slack_id" >> $GITHUB_OUTPUT
- name: Create PR
uses: actions/github-script@v7
id: create-pr
with:
script: |
const uniqueBranchName = 'dependabot-combined-prs-' + Date.now().toString();
const pulls = await github.paginate('GET /repos/:owner/:repo/pulls', {
owner: context.repo.owner,
repo: context.repo.repo
});
let branchesAndPRStrings = [];
let baseBranch = null;
let baseBranchSHA = null;
for (const pull of pulls) {
const branch = pull['head']['ref'];
if (branch.startsWith('dependabot/')) {
console.log('Branch matched prefix. Adding to array: ' + branch);
const prString = '#' + pull['number'] + ' ' + pull['title'];
branchesAndPRStrings.push({ branch, prString });
baseBranch = pull['base']['ref'];
baseBranchSHA = pull['base']['sha'];
}
}
if (branchesAndPRStrings.length == 0) {
core.setFailed('There are no open dependabot PRs.');
return;
}
try {
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: 'refs/heads/' + uniqueBranchName,
sha: baseBranchSHA
});
} catch (error) {
console.log(error);
core.setFailed('Failed to create combined branch');
return;
}
let combinedPRs = [];
let mergeFailedPRs = [];
for(const { branch, prString } of branchesAndPRStrings) {
try {
await github.rest.repos.merge({
owner: context.repo.owner,
repo: context.repo.repo,
base: uniqueBranchName,
head: branch,
});
console.log('Merged branch ' + branch);
combinedPRs.push(prString);
} catch (error) {
console.log('Failed to merge branch ' + branch);
mergeFailedPRs.push(prString);
}
}
console.log('Creating combined PR');
const combinedPRsString = combinedPRs.join('\n');
let body = '✅ This PR was created by the Merge Dependabot PRs action by combining the following dependabot PRs:\n' + combinedPRsString;
if(mergeFailedPRs.length > 0) {
const mergeFailedPRsString = mergeFailedPRs.join('\n');
body += '\n\n⚠️ The following dependabot PRs were left out due to merge conflicts:\n' + mergeFailedPRsString
}
let response = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: 'Combined Dependabot PR',
head: uniqueBranchName,
base: baseBranch,
body: body
});
console.log('Created combined PR: ' + response.data.html_url);
core.setOutput('pr_url', response.data.html_url);
core.setOutput('pr_list', combinedPRsString);
- name: Post to a Slack channel
uses: slackapi/slack-github-action@v1.24.0
id: slack
with:
channel-id: "C03RTLRKJQP"
payload: |
{
"text": "iOS: New Dependabot PR Awaiting Review",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text":"📱🔧 iOS: New Dependabot PR Awaiting Review",
"emoji": true
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Included PRs:*\n${{ toJSON(steps.create-pr.outputs.pr_list) }}\n\n\nCurrent On-Call: *<${{ steps.on-call.outputs.oncall_slack_id }}>*"
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": ":github-pull-request-open: View Combined PR",
"emoji": true
},
"value": "pr-button",
"url": "${{ steps.create-pr.outputs.pr_url }}",
"action_id": "link-action",
"style": "primary"
}
]
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_SDK_BOT_TOKEN }}