-
Notifications
You must be signed in to change notification settings - Fork 917
106 lines (88 loc) · 3.74 KB
/
validate-structure.yml
File metadata and controls
106 lines (88 loc) · 3.74 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
name: Validate Folder Structure
on:
pull_request_target:
branches:
- main
permissions:
contents: read
pull-requests: write
concurrency:
group: folder-structure-${{ github.event.pull_request.number || github.run_id }}
cancel-in-progress: true
jobs:
structure:
runs-on: ubuntu-latest
steps:
- name: Checkout base repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Cache validation script
run: cp .github/scripts/validate-structure.js "$RUNNER_TEMP/validate-structure.js"
- name: Fetch pull request head
id: fetch_head
env:
PR_REMOTE_URL: https://github.com/${{ github.event.pull_request.head.repo.full_name }}.git
PR_HEAD_REF: ${{ github.event.pull_request.head.ref }}
run: |
git remote remove pr >/dev/null 2>&1 || true
git remote add pr "$PR_REMOTE_URL"
if git fetch pr "$PR_HEAD_REF":pr-head --no-tags; then
git checkout pr-head
git fetch origin "${{ github.event.pull_request.base.ref }}"
echo "fetched=true" >> "$GITHUB_OUTPUT"
else
echo "::warning::Unable to fetch fork repository. Skipping structure validation."
echo "fetched=false" >> "$GITHUB_OUTPUT"
fi
- name: Use Node.js 18
uses: actions/setup-node@v4
with:
node-version: 18
- name: Validate folder layout
if: ${{ steps.fetch_head.outputs.fetched == 'true' }}
id: validate
run: |
set -euo pipefail
tmp_output=$(mktemp)
tmp_error=$(mktemp)
set +e
node "$RUNNER_TEMP/validate-structure.js" origin/${{ github.event.pull_request.base.ref }}...HEAD >"$tmp_output" 2>"$tmp_error"
status=$?
set -e
cat "$tmp_output"
cat "$tmp_error" >&2
if grep -q 'Folder structure violations found' "$tmp_output" "$tmp_error"; then
echo "status=failed" >> "$GITHUB_OUTPUT"
exit 0
fi
if [ $status -ne 0 ]; then
echo "::warning::Structure validation skipped because the diff could not be evaluated (exit code $status)."
echo "status=skipped" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "status=passed" >> "$GITHUB_OUTPUT"
- name: Close pull request on failure
if: ${{ steps.validate.outputs.status == 'failed' }}
uses: actions/github-script@v6
with:
github-token: ${{ github.token }}
script: |
const pullNumber = context.payload.pull_request.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
await github.rest.issues.createComment({
owner,
repo,
issue_number: pullNumber,
body: `Thank you for your contribution. However, it doesn't comply with our contributing guidelines. As a reminder, the general requirements (as outlined in the [CONTRIBUTING.md file](https://github.com/ServiceNowDevProgram/code-snippets/blob/main/CONTRIBUTING.md)) are the following: follow the folder+subfolder guidelines and include a README.md file explaining what the code snippet does. Review your contribution against the guidelines and make the necessary adjustments. Closing this for now. Once you make additional changes, feel free to re-open this Pull Request or create a new one.`.trim()
});
await github.rest.pulls.update({
owner,
repo,
pull_number: pullNumber,
state: 'closed'
});
- name: Mark job as failed if validation failed
if: ${{ steps.validate.outputs.status == 'failed' }}
run: exit 1