forked from segmentio/action-destinations
-
Notifications
You must be signed in to change notification settings - Fork 0
143 lines (126 loc) · 5.89 KB
/
required-field-check.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
name: Required Field Check
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
required-field-check:
runs-on: ubuntu-20.04
timeout-minutes: 5
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
fetch-depth: 0
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
registry-url: 'https://registry.npmjs.org'
cache: yarn
- name: Install Dependencies
run: yarn install --frozen-lockfile
- name: Get files changed in the PR
id: get_files
uses: actions/github-script@v7
with:
script: |
const response = await github.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/files', {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
})
const files = response.data.map(file => file.filename)
core.setOutput('files', files.join(' '))
if (files.length === 0) {
core.setOutput('no_files_changed', true)
}
- name: Find required fields on current branch
id: required-fields-curr-branch
if: steps.get_files.outputs.no_files_changed != 'true'
run: |
REQUIRED_FIELDS_CURR_BRANCH=$(./bin/run list-required-fields -p ${{ steps.get_files.outputs.files }} | jq -c .)
echo "REQUIRED_FIELDS_CURR_BRANCH=$REQUIRED_FIELDS_CURR_BRANCH" >> $GITHUB_ENV
- name: Check for required fields on main branch
id: required-fields-main-branch
if: steps.get_files.outputs.no_files_changed != 'true'
run: |
git checkout main
REQUIRED_FIELDS_MAIN_BRANCH=$(./bin/run list-required-fields -p ${{ steps.get_files.outputs.files }} | jq -c .)
echo "REQUIRED_FIELDS_MAIN_BRANCH=$REQUIRED_FIELDS_MAIN_BRANCH" >> $GITHUB_ENV
- name: Add comment on PR if there is diff in required fields
if: steps.get_files.outputs.no_files_changed != 'true'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs')
const path = require('path')
const fieldsAdded = []
if(process.env.REQUIRED_FIELDS_CURR_BRANCH && process.env.REQUIRED_FIELDS_MAIN_BRANCH) {
const requiredFieldsOnBranch = JSON.parse(process.env.REQUIRED_FIELDS_CURR_BRANCH)
const requiredFieldsOnMain = JSON.parse(process.env.REQUIRED_FIELDS_MAIN_BRANCH)
Object.keys(requiredFieldsOnBranch).forEach(key => {
// Check if key is present in requiredFieldsOnMain
if(requiredFieldsOnMain[key]) {
const getActionKeys = Object.keys(requiredFieldsOnBranch[key])
for(const actionKey of getActionKeys) {
const branchRequiredFields = requiredFieldsOnBranch[key][actionKey]
const mainRequiredFields = requiredFieldsOnMain[key][actionKey]
const diff = branchRequiredFields.filter(field => !mainRequiredFields?.includes(field))
if(diff.length > 0) {
const isSettingsKey = actionKey === 'settings'
if (isSettingsKey) {
fieldsAdded.push(`- **Destination**: ${key}, **Settings**:${diff.join(',')}`)
} else {
fieldsAdded.push(`- **Destination**: ${key}, Action **Field(s)**:${diff.join(',')}`)
}
}
}
} else {
// If key is not present in requiredFieldsOnMain, then all fields are added recently
const getActionKeys = Object.keys(requiredFieldsOnBranch[key])
for(const actionKey of getActionKeys) {
const branchRequiredFields = requiredFieldsOnBranch[key][actionKey]
if(actionKey === 'settings') {
fieldsAdded.push(`- **Destination**: ${key}, **Settings**:${branchRequiredFields.join(',')}`)
} else {
fieldsAdded.push(`- **Destination**: ${key}, **Action**:${actionKey}, **Fields**:${branchRequiredFields.join(',')}`)
}
}
}
})
}
// Get the comments of the current PR
const comments = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo
})
const diffComment = comments.data.find(comment => comment.body.includes('<!--REQUIRED_FIELD_DIFF-->'))
const comment = fs.readFileSync("./.github/REQUIRED_FIELD_WARNING_TEMPLATE.md", "utf8")
const commentBody = comment.replace('{{FIELDS_ADDED}}', fieldsAdded.join('\n'))
if (fieldsAdded.length > 0) {
// Check if the comment is already added
if (!diffComment) {
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
})
} else {
await github.rest.issues.updateComment({
comment_id: diffComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
})
}
} else {
if (diffComment) {
await github.rest.issues.deleteComment({
comment_id: diffComment.id,
owner: context.repo.owner,
repo: context.repo.repo
})
}
}