-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.yml
94 lines (83 loc) · 3.25 KB
/
action.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
name: "Dependabot auto-merge 2.0"
description: "Approves and merges Dependabot PRs"
inputs:
VAULT_URL:
description: "Vault URL"
required: true
runs:
using: "composite"
steps:
- name: Retrieve Github Token
id: vault
uses: hashicorp/[email protected]
with:
url: ${{ inputs.VAULT_URL }}
role: ${{ github.event.repository.name }}-github-action
method: jwt
path: github-actions
exportEnv: false
secrets: |
github/token/${{ github.event.repository.name }}-dependabot token | GITHUB_MERGE_TOKEN ;
- name: approve PR
uses: actions/[email protected]
with:
github-token: ${{ steps.vault.outputs.GITHUB_MERGE_TOKEN }}
script: |
const opts = github.rest.pulls.listReviews.endpoint.merge({
pull_number: context.payload.pull_request.number,
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
});
const reviews = await github.paginate(opts);
const ourReview = reviews.find(
(review) =>
review.state === "APPROVED" && review.user && review.user.login === "contentful-automation[bot]"
);
if (ourReview) {
console.log(
`The user "${ourReview.user.login}" has already approved and requested this PR is merged, exiting`
);
} else {
github.rest.pulls.createReview({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
pull_number: context.payload.pull_request.number,
event: 'APPROVE',
body: ''
})
}
- name: Get merge type
id: merge-type
uses: actions/[email protected]
with:
github-token: ${{ steps.vault.outputs.GITHUB_MERGE_TOKEN }}
script: |
const repo = await github.rest.repos.get({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
});
const methods = new Map([
['squash', repo.data.allow_squash_merge],
['merge', repo.data.allow_merge_commit],
['rebase', repo.data.allow_rebase_merge],
]);
console.log(methods)
const allowedMethods = [...methods.entries()]
.filter(([_, allowed]) => allowed)
.map(([method]) => method);
// just pick the first one
const mergeMethod = allowedMethods[0];
if (!mergeMethod) {
throw new Error("No allowed merge method found");
}
return mergeMethod;
- name: Enable auto merge
shell: bash
run: |
merge_method="$(echo ${{ steps.merge-type.outputs.result }} | tr -d '"')"
echo "Auto merging PR using method $merge_method"
gh pr merge --delete-branch "--$merge_method" --auto ${{ github.event.pull_request.html_url }}
env:
# Note: The GH_TOKEN env var is required for now, even though it contradicts GH's documentation. If not present we
# were seeing an error message like this one: https://github.com/github/docs/issues/21930#issuecomment-1310122605
GH_TOKEN: ${{ github.token }}