-
Notifications
You must be signed in to change notification settings - Fork 306
132 lines (126 loc) · 5.82 KB
/
configure-pr.yml
File metadata and controls
132 lines (126 loc) · 5.82 KB
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
# This workflow labels PRs based on the files that were changed and also assign a reviewer based on team. It uses a custom script to this
# instead of actions/labeler as few of the tags are more than just file changes.
name: Configure PR
on:
pull_request_target:
types: [opened, synchronize, reopened, ready_for_review]
jobs:
configure-pr:
runs-on: ubuntu-22.04
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Compute Labels
id: compute-labels
uses: actions/github-script@v7
with:
# Required for the script to access team membership information.
# Scope: members:read and contentes:read permission on the organization.
github-token: ${{ secrets.GH_PAT_MEMBER_AND_PULL_REQUEST_READONLY }}
script: |
const script = require('./scripts/github-action/compute-labels.js')
await script({github, context, core})
# Separating apply labels to separate step to avoid using PAT token auth.
- name: Apply Labels
uses: actions/github-script@v7
env:
labelsToAdd: '${{ steps.compute-labels.outputs.add }}'
labelsToRemove: '${{ steps.compute-labels.outputs.remove }}'
with:
script: |
const { labelsToAdd, labelsToRemove, DRY_RUN } = process.env
if(Boolean(DRY_RUN)){
core.info(`Would have added labels: ${labelsToAdd}`)
core.info(`Would have removed labels: ${labelsToRemove}`)
return
}
if(labelsToAdd.length > 0) {
await github.rest.issues.addLabels({
issue_number: context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: labelsToAdd.split(',')
});
}
if(labelsToRemove.length > 0) {
const requests = labelsToRemove.split(',').map(label => {
return github.rest.issues.removeLabel({
issue_number: context.payload.pull_request.number,
name: label,
owner: context.repo.owner,
repo: context.repo.repo
});
});
await Promise.all(requests);
}
- name: Comment for mapping-kit changes
uses: actions/github-script@v7
env:
labelsToAdd: '${{ steps.compute-labels.outputs.add }}'
labelsToRemove: '${{ steps.compute-labels.outputs.remove }}'
with:
script: |
const { labelsToAdd, labelsToRemove, DRY_RUN } = process.env
const shouldAddComment = labelsToAdd.length > 0 && labelsToAdd.split(",").some(x=>x.includes("mappingkit"))
const shouldRemoveComment = labelsToRemove.length > 0 && labelsToRemove.split(",").some(x=>x.includes("mappingkit"))
// Get the list of comments on the PR
const response = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo
})
const mappingKitComment = response.data.find(comment => comment.body.includes('mapping-kit go'))
if(shouldAddComment){
if (mappingKitComment) {
console.log('Already commented on this PR')
return
}
// Add comment to the PR
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `This PR makes changes to mapping-kit. Please ensure that the changes are reflected in the [mapping-kit go](https://github.com/segmentio/mapping-kit) library as well and link the PR in description.`
})
}
if(shouldRemoveComment) {
if (!mappingKitComment) {
console.log('No mapping-kit comment to remove')
return
}
// Remove comment from the PR
await github.rest.issues.deleteComment({
comment_id: mappingKitComment.id,
owner: context.repo.owner,
repo: context.repo.repo
})
}
- name: Get Reviewers
if: github.event.action == 'ready_for_review' || (github.event.action == 'opened' && !github.event.pull_request.draft)
id: get-reviewers
uses: actions/github-script@v7
env:
labelsToAdd: '${{ steps.compute-labels.outputs.add }}'
with:
github-token: ${{ secrets.GH_PAT_MEMBER_AND_PULL_REQUEST_READONLY }}
script: |
const script = require('./scripts/github-action/get-reviewers.js')
await script({github, context, core})
- name: Assign Reviewers
if: (github.event.action == 'ready_for_review' || (github.event.action == 'opened' && !github.event.pull_request.draft)) && steps.get-reviewers.outputs.skip != 'true'
uses: actions/github-script@v7
env:
REVIEWERS: '${{ steps.get-reviewers.outputs.reviewers }}'
TEAM: '${{ steps.get-reviewers.outputs.team }}'
with:
script: |
const script = require('./scripts/github-action/assign-reviewer.js')
await script({github, context, core})
- name: Log Skip Reviewer Assignment Reason
if: (github.event.action == 'ready_for_review' || (github.event.action == 'opened' && !github.event.pull_request.draft)) && steps.get-reviewers.outputs.skip == 'true'
run: echo "Skipping reviewer assignment - ${{ steps.get-reviewers.outputs.reason }}"