-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
112 lines (100 loc) · 4.03 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
name: "Branch out-of-sync notifier"
description: "Comments on a given PR if the base branch is out of sync with the target one."
branding:
icon: git-branch
color: orange
inputs:
base-ref:
description: "The base git reference, used for comparison."
default: ${{ github.event.pull_request.base.ref }}
required: true
head-ref:
description: "The head git reference, used for comparison."
default: ${{ github.event.pull_request.head.ref }}
required: true
request-owner:
description: "The owner of the pull request"
default: ${{ github.event.pull_request.user.login }}
required: true
request-number:
descripton: "The pull request number"
default: ${{ github.event.pull_request.number }}
required: true
no-sync-label:
description: "The label value, used for tagging PRs that shouldn't be checked"
default: 'no-branch-sync'
required: false
runs:
using: "composite"
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: true
clean: false
- name: Check if no-sync label has been applied
uses: actions/github-script@v7
id: "check-for-no-sync-label"
with:
script: |
const labels = await github.paginate(github.rest.issues.listLabelsOnIssue, {
issue_number: ${{ inputs.request-number }},
owner: context.repo.owner,
repo: context.repo.repo,
});
const projectedLabels = labels?.map(l => l.name);
const isNoSyncLabelApplied = projectedLabels.includes('${{ inputs.no-sync-label }}');
return isNoSyncLabelApplied;
- name: Compare branches
id: "compare-branches"
if: steps.check-for-no-sync-label.outputs.result == 'false'
shell: bash
run: |
git fetch --all
echo "behind=$(git rev-list --count origin/${{ inputs.head-ref }}..origin/${{ inputs.base-ref }})" >> $GITHUB_OUTPUT
echo "ahead=$(git rev-list --count origin/${{ inputs.base-ref }}..origin/${{ inputs.head-ref }})" >> $GITHUB_OUTPUT
- name: Remove existing comments
uses: actions/github-script@v7
if: steps.check-for-no-sync-label.outputs.result == 'false'
env:
# Top level env. variables are not supported in composite actions :(
SYNC_BOT_TITLE: "Branch Sync Notifier"
BOT_LOGIN: "github-actions[bot]"
with:
script: |
const pullRequestComments = await github.paginate(github.rest.issues.listComments, {
issue_number: ${{ inputs.request-number }},
owner: context.repo.owner,
repo: context.repo.repo,
});
const botComments = pullRequestComments.filter(c => {
return c.user.login === '${{ env.BOT_LOGIN }}' && c.body.includes('${{ env.SYNC_BOT_TITLE }}');
});
botComments.forEach(async comment => {
await github.rest.issues.deleteComment({
comment_id: comment.id,
owner: context.repo.owner,
repo: context.repo.repo
});
});
- name: Comment if head branch is out of sync
uses: actions/github-script@v7
if: steps.compare-branches.outputs.behind > 0
env:
SYNC_BOT_TITLE: "Branch Sync Notifier"
with:
script: |
const body = `# ${{ env.SYNC_BOT_TITLE }} :bell:
### Hey, @${{ inputs.request-owner }}! :wave:
### Your branch is out of sync with the PR's target branch.
### Please rebase it on top of the latest changes from \`${{ inputs.base-ref }}\` or merge them. :sun_with_face:
**Run: [${{ github.run_number }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) :rabbit2:**
**Attempt: № \`${{ github.run_attempt }}\` :eyes:**
`;
github.rest.issues.createComment({
issue_number: ${{ inputs.request-number }},
owner: context.repo.owner,
repo: context.repo.repo,
body: body
})