Skip to content

Commit fe6442b

Browse files
feat(ci): Add pull request checks (#7884)
1 parent 281aa19 commit fe6442b

File tree

9 files changed

+144
-2
lines changed

9 files changed

+144
-2
lines changed

.github/workflows/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ _Action:_ Append the new release to the Cloud Foundry repository.
2020

2121
_Recovery:_ Manually edit and push the `index.yml` file from [the cloudfoundry branch](https://github.com/DataDog/dd-trace-java/tree/cloudfoundry).
2222

23+
### check-pull-requests [🔗](check-pull-requests.yaml)
24+
25+
_Trigger:_ When creating or updating a pull request.
26+
27+
_Action:_ Check the pull request complies with [the contribution guidelines](https://github.com/DataDog/dd-trace-java/blob/master/CONTRIBUTING.md).
28+
29+
_Recovery:_ Manually verify the guideline compliance.
30+
2331
### create-next-milestone [🔗](create-next-milestone.yaml)
2432

2533
_Trigger:_ When closing a milestone.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Check pull requests
2+
on:
3+
pull_request:
4+
types: [opened, reopened, edited, labeled, unlabeled]
5+
branches:
6+
- master
7+
- release/v*
8+
jobs:
9+
check_pull_requests:
10+
name: Check pull requests
11+
permissions:
12+
issues: write # Required to create a comment on the pull request
13+
pull-requests: write # Required to create a comment on the pull request
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Check pull requests
17+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # 7.0.1
18+
with:
19+
github-token: ${{secrets.GITHUB_TOKEN}}
20+
script: |
21+
// Skip draft pull requests
22+
if (context.payload.pull_request.draft) {
23+
return
24+
}
25+
// Check at least one type and (component or instrumentation) label is set
26+
const labels = context.payload.pull_request.labels.map(label => label.name)
27+
const ignoreReleaseNotes = labels.filter(label => label == 'tag: no release notes').length > 0
28+
const hasTypeLabel = labels.filter(label => label.startsWith('type:')).length > 0
29+
const hasComponentLabel = labels.filter(label => label.startsWith('comp:')).length > 0
30+
const hasInstrumentationLabel = labels.filter(label => label.startsWith('instr:')).length > 0
31+
const labelsCheckFailed = !ignoreReleaseNotes && (!hasTypeLabel || (!hasComponentLabel && !hasInstrumentationLabel));
32+
if (labelsCheckFailed) {
33+
core.setFailed('Please add at least one type, and one component or instrumentation label to the pull request.')
34+
}
35+
// Check title does not contain tag
36+
const title = context.payload.pull_request.title
37+
const titleCheckFailed = title.match(/\[.*\]/)
38+
if (titleCheckFailed) {
39+
core.setFailed('Please remove the tag from the pull request title.')
40+
}
41+
// Add comment to the pull request
42+
if (labelsCheckFailed || titleCheckFailed) {
43+
await github.rest.issues.createComment({
44+
issue_number: context.payload.pull_request.number,
45+
owner: context.repo.owner,
46+
repo: context.repo.repo,
47+
body: 'Hi! 👋 Thanks for your pull request! 🎉\n\n' +
48+
'To help us review it, please make sure to:\n\n' +
49+
(labelsCheckFailed ? '* Add at least one type, and one component or instrumentation label to the pull request\n' : '') +
50+
(titleCheckFailed ? '* Remove the tag from the pull request title\n' : '') +
51+
'\nIf you need help, please check our [contributing guidelines](https://github.com/DataDog/dd-trace-java/blob/master/CONTRIBUTING.md).'
52+
})
53+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"pull_request": {
3+
"number": 7884,
4+
"draft": true,
5+
"labels": [],
6+
"title": "Adding some new features"
7+
}
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"pull_request": {
3+
"number": 7884,
4+
"draft": false,
5+
"labels": [
6+
{
7+
"name": "comp: api"
8+
}
9+
],
10+
"title": "Adding some new features"
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"pull_request": {
3+
"number": 7884,
4+
"draft": false,
5+
"labels": [
6+
{
7+
"name": "tag: no release notes"
8+
}
9+
],
10+
"title": "Adding some new features"
11+
}
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"pull_request": {
3+
"number": 7884,
4+
"draft": false,
5+
"labels": [
6+
{
7+
"name": "comp: api"
8+
},
9+
{
10+
"name": "type: enhancement"
11+
}
12+
],
13+
"title": "[API] Adding some new features"
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"pull_request": {
3+
"number": 7884,
4+
"draft": false,
5+
"labels": [
6+
{
7+
"name": "comp: api"
8+
},
9+
{
10+
"name": "type: enhancement"
11+
}
12+
],
13+
"title": "Adding some new features"
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
source "$(dirname "$0")/../env.sh"
3+
testworkflow pull_request && \
4+
testworkflow pull_request draft && \
5+
testworkflow pull_request no-release-notes && \
6+
! testworkflow pull_request missing-label && \
7+
! testworkflow pull_request title-tag

.github/workflows/tests/env.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@
22

33
function testworkflow() {
44
local EVENT_TYPE=$1
5+
local SCENARIO=$2
56
# Get workflow name
67
local TEST_PATH
78
TEST_PATH=$(dirname "$(readlink -f "${BASH_SOURCE[1]}")")
89
local WORKFLOW_NAME
910
WORKFLOW_NAME=$(basename "$TEST_PATH")
1011
local WORKFLOW_FILE=.github/workflows/${WORKFLOW_NAME}.yaml
11-
local PAYLOAD_FILE=${TEST_PATH}/payload-${EVENT_TYPE//_/-}.json
12+
local PAYLOAD_FILE
13+
PAYLOAD_FILE=${TEST_PATH}/payload-${EVENT_TYPE//_/-}
14+
if [ "$SCENARIO" != "" ]; then
15+
PAYLOAD_FILE=${PAYLOAD_FILE}-${SCENARIO}
16+
fi
17+
PAYLOAD_FILE=${PAYLOAD_FILE}.json
1218
# Move to project root directory
1319
local FILE_PATH
1420
FILE_PATH=$(dirname "$0")
15-
cd "$FILE_PATH/../../../../" || exit 1
21+
pushd "$FILE_PATH/../../../../" || exit 1
1622
# Check if workflow file and payload file exist
1723
if [ ! -f "$WORKFLOW_FILE" ]; then
1824
echo "Workflow file not found: $WORKFLOW_FILE"
@@ -29,4 +35,10 @@ function testworkflow() {
2935
--container-architecture linux/amd64 \
3036
--secret GITHUB_TOKEN="$(gh auth token)" \
3137
--verbose
38+
# Capture the exit code
39+
local EXIT_CODE=$?
40+
# Move back to initial directory
41+
popd || exit 1
42+
# Return the test exit code
43+
return $EXIT_CODE
3244
}

0 commit comments

Comments
 (0)