-
Notifications
You must be signed in to change notification settings - Fork 5.8k
136 lines (114 loc) · 4.99 KB
/
external-code-affected.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
# Check if code checked into external resources (blogs, tutorials)
# that we also track in our CI is affected by a PR.
# In that case, we add a label to the PR (`external-code-affected`) and
# add a comment to make sure that the external code still works and is
# eventually updated.
name: External code check
on: pull_request_target
jobs:
check-changes:
permissions: write-all
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- name: Check for changes in tracked files
run: |
set -xe
git clone https://github.com/ray-project/rayci.git ./rayci
# Find changed files
GIT_DIFF=$(git diff --name-only ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }})
echo "All changed files:"
echo "$GIT_DIFF"
GIT_DIFF_SERIALIZED=$(echo "$GIT_DIFF" | tr '\n' '|')
echo "GIT_DIFF_SERIALIZED=$GIT_DIFF_SERIALIZED" >> $GITHUB_ENV
- name: Add label and comment if a tracked file changed
uses: actions/github-script@v5
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const {
deserializeIntoArray,
filterFilesByNames,
getCommentContentChanged,
getCommentContentNotChanged,
parseTrackedFilesToURIs,
readFileContent
} = require('./rayci/external_code_tracker/track_code');
const fs = require("fs");
const commentHeader = `## Attention: External code changed`
const externalCodeFile = "doc/external/external_code.txt"
// Get existing comments
const existingComments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
// Find comment by the bot that starts with the header
let commentToUpdate = existingComments.data.find(comment =>
comment.user.login === 'github-actions[bot]' && comment.body.startsWith(commentHeader)
);
let externCodeFileContent;
let trackedFilesToURIs;
// Read and parse external_code.txt file
try {
externCodeFileContent = fs.readFileSync(externalCodeFile, "utf8");
trackedFilesToURIs = parseTrackedFilesToURIs(externCodeFileContent);
} catch (error) {
console.error("An error occurred reading the external code file:", error);
trackedFilesToURIs = {};
}
console.log("trackedFileToURIs");
console.log(trackedFilesToURIs);
// Get changed files from environment variable
let changedFiles = await deserializeIntoArray(process.env.GIT_DIFF_SERIALIZED)
console.log("changedFiles");
console.log(changedFiles);
// Filter associative array
let changedFileToURIs = filterFilesByNames(trackedFilesToURIs, changedFiles);
console.log("changedFileToURIs");
console.log(changedFileToURIs);
console.log(changedFileToURIs.length);
if (Object.keys(changedFileToURIs).length === 0) {
console.log("No changes to tracked files detected");
commentBody = getCommentContentNotChanged(commentHeader);
if (commentToUpdate && commentBody !== commentToUpdate.body) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: commentToUpdate.id,
body: commentBody
});
}
} else {
console.log("Changes to tracked files detected");
commentBody = getCommentContentChanged(commentHeader, changedFileToURIs);
if (commentToUpdate) {
// Only update if content changed
if (commentBody !== commentToUpdate.body) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: commentToUpdate.id,
body: commentBody
});
}
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: commentBody
});
}
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: ['external-code-affected']
});
}