-
Notifications
You must be signed in to change notification settings - Fork 12
118 lines (114 loc) · 4.95 KB
/
dependabot-automerge.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
name: Merge Dependabot PRs
on:
workflow_dispatch:
schedule:
- cron: 0 2 * * *
permissions:
contents: write
pull-requests: write
checks: write
statuses: read
jobs:
merge-pullrequests:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Harden runner
uses: step-security/harden-runner@c95a14d0e5bab51a9f56296a4eb0e416910cd350 # ratchet:step-security/harden-runner@v2
with:
disable-sudo: true
egress-policy: block
allowed-endpoints: >
api.github.com:443
- name: merge
uses: actions/github-script@v7
with:
script: |
const erFerie = () => {
const today = new Date();
const sommerStart = new Date(today.getFullYear(), 5, 20);
const sommerSlutt = new Date(today.getFullYear(), 6, 7);
const julStart = new Date(today.getFullYear(), 11, 20);
const julSlutt = new Date(today.getFullYear(), 11, 31);
const nyttaarStart = new Date(today.getFullYear(), 0, 1);
const nyttaarSlutt = new Date(today.getFullYear(), 0, 3);
return (today >= sommerStart && today <= sommerSlutt) || (today >= julStart && today <= julSlutt) || (today >= nyttaarStart && today <= nyttaarSlutt)
}
if (erFerie()) {
console.log('Skriptet kjører ikke i ferier');
return;
}
const pulls = await github.request('GET /repos/{owner}/{repo}/pulls', {
owner: context.repo.owner,
repo: context.repo.repo,
})
const toDager = 2 * 60 * 60 * 1000 // 2 days
const filtrert = pulls.data
.filter((it) => it.state == 'open')
.filter((it) => !it.head.repo.fork)
.filter((it) => it.user.login === 'dependabot[bot]')
.filter((it) => new Date(it.created_at).getTime() < (Date.now() - toDager))
.map((it) => {
return {
title: it.title,
number: it.number,
}
})
const beriket = filtrert.map(async (it) => {
const QUERY = `query($owner: String!, $repo: String!, $pull_number: Int!) {
repository(owner: $owner, name:$repo) {
pullRequest(number:$pull_number) {
commits(last: 1) {
nodes {
commit {
statusCheckRollup {
state
}
}
}
}
}
}
}`
const result = await github.graphql(QUERY, {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: it.number,
})
const [{ commit: lastCommit }] =
result.repository.pullRequest.commits.nodes
return {
title: it.title,
number: it.number,
checksOk: lastCommit.statusCheckRollup?.state === 'SUCCESS',
}
})
const allePrs = await Promise.all(beriket)
const mergeablePromise = allePrs
.filter((it) => it.checksOk)
.map(async (it) => {
const prdata = await github.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: it.number
})
return {
title: it.title,
number: it.number,
mergeable: it.checksOk
}
})
const mergeable = (await Promise.all(mergeablePromise))
.filter((it) => {
return it.mergeable;
})
console.log(`About to merge ${mergeable.length} PRs`)
mergeable.forEach(async (pr) => {
console.log('Merging PR: ', pr.title)
await github.request('PUT /repos/{owner}/{repo}/pulls/{pull_number}/merge', {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
merge_method: 'squash'
})
})