1+ # Copyright (c) Microsoft Corporation.
2+ # Licensed under the MIT License.
3+ #
4+ # GitHub Actions workflow to run yamllint on specific YAML files in a pull request.
5+ ---
6+ name : Run YAML Lint
7+ on :
8+ pull_request :
9+ types :
10+ - opened
11+ - synchronize
12+ - reopened
13+ branches :
14+ - next-devel
15+ jobs :
16+ check :
17+ name : YAML Lint Check
18+ runs-on : bdba
19+
20+ steps :
21+ - name : Checkout repository
22+ uses : actions/checkout@v4
23+
24+ - name : Setup Python
25+ uses : actions/setup-python@v4
26+ with :
27+ python-version : ' 3.x'
28+
29+ - name : Install PYPI packages
30+ run : pip install yamllint pyyaml
31+
32+ - name : Display YAML Lint version
33+ run : yamllint --version
34+
35+ - name : Bot Auto Assignee
36+ uses : actions/github-script@v7
37+ with :
38+ script : |
39+ github.rest.issues.addAssignees({
40+ owner: context.repo.owner,
41+ repo: context.repo.repo,
42+ issue_number: context.payload.pull_request.number,
43+ assignees: [context.actor],
44+ });
45+
46+ - name : Bot Auto Reviewer
47+ uses : actions/github-script@v7
48+ with :
49+ script : |
50+ try {
51+ await github.rest.pulls.requestReviewers({
52+ owner: context.repo.owner,
53+ repo: context.repo.repo,
54+ pull_number: context.payload.pull_request.number,
55+ reviewers: ['jinghuitham', 'JiayongT', 'Copilot'],
56+ });
57+ console.log('Reviewers added successfully');
58+ } catch (error) {
59+ core.setFailed(`Failed to add reviewers: ${error.message}`);
60+ }
61+
62+ - name : Bot Auto Labels
63+ uses : actions/github-script@v7
64+ with :
65+ script : |
66+ github.rest.issues.addLabels({
67+ owner: context.repo.owner,
68+ repo: context.repo.repo,
69+ issue_number: context.payload.pull_request.number,
70+ labels: ['needs-review'],
71+ });
72+
73+ - name : Bot assign PR title
74+ uses : actions/github-script@v7
75+ with :
76+ script : |
77+
78+ const { data: files } = await github.rest.pulls.listFiles({
79+ owner: context.repo.owner,
80+ repo: context.repo.repo,
81+ pull_number: context.payload.pull_request.number,
82+ });
83+
84+ const args = files.map(file => file.filename.split('/')[1]).filter(spec => spec !== undefined);
85+
86+
87+ const MAX_TITLE_LENGTH = 256;
88+ const title = context.payload.pull_request.title;
89+ let newTitle = title;
90+
91+ if (args.length > 0) {
92+ const prefix = 'Review Analysis Configuration File - ';
93+ const remainingForContent = Math.max(0, MAX_TITLE_LENGTH - prefix.length);
94+
95+ let displayedFiles = [];
96+
97+ for (let i = 0; i < args.length; i++) {
98+ const file = args[i];
99+ const nextFiles = displayedFiles.concat(file);
100+ const remainingCount = args.length - (i + 1);
101+ const moreText = remainingCount > 0 ? ` (+${remainingCount} more)` : '';
102+ const candidateContent = nextFiles.join(', ');
103+ const candidateTitle = prefix + candidateContent + moreText;
104+
105+ if (candidateTitle.length <= MAX_TITLE_LENGTH) {
106+ displayedFiles = nextFiles;
107+ } else {
108+ break;
109+ }
110+ }
111+
112+ let content = displayedFiles.join(', ');
113+ const remainingCountFinal = args.length - displayedFiles.length;
114+ if (remainingCountFinal > 0) {
115+ content += ` (+${remainingCountFinal} more)`;
116+ }
117+
118+ newTitle = prefix + content;
119+ }
120+
121+ try {
122+ await github.rest.pulls.update({
123+ owner: context.repo.owner,
124+ repo: context.repo.repo,
125+ pull_number: context.payload.pull_request.number,
126+ title: newTitle,
127+ });
128+ }
129+ catch (error) {
130+ core.setFailed(`Failed to update PR title: ${error.message}`);
131+ }
132+
133+ - name : Run YAML Linting
134+ if : github.event_name == 'pull_request'
135+ uses : actions/github-script@v7
136+ with :
137+ script : |
138+ // Run yamllint on changed YAML files in the PR
139+ const { data: files } = await github.rest.pulls.listFiles({
140+ owner: context.repo.owner,
141+ repo: context.repo.repo,
142+ pull_number: context.payload.pull_request.number,
143+ });
144+ console.log(files.map(file => file.filename).join('\n'));
145+ core.setOutput('files', files.map(file => file.filename).join('\n'));
146+ const command = 'yamllint';
147+ const args = files.map(file => file.filename);
148+ await exec.exec(command, args).catch(error => core.setFailed(error.message));
149+ // set environment variable for next steps
150+ core.exportVariable('EDGE_MT_SPECS', args.join(' '));
151+ // Add labels based on changed files
152+ const specs = args
153+ .map(file => file.split('/')[1])
154+ .filter(spec => spec !== undefined);
155+ // Add labels to the PR
156+ const validSpec = specs.filter(spec => spec && spec.trim().length >0);
157+ try {
158+ validSpec.forEach(spec => console.log(`Package to be labeled as ${spec}`));
159+ await github.rest.issues.addLabels({
160+ owner: context.repo.owner,
161+ repo: context.repo.repo,
162+ issue_number: context.payload.pull_request.number,
163+ labels: [...new Set(validSpec)],
164+ });
165+ }
166+ catch (error) {
167+ core.setFailed(`Failed to add labels: ${error.message}`);
168+ }
169+
170+ - name : Run YAML Syntax Checks
171+ run : |
172+ python3 scripts/check-bdba-yaml.py
173+
174+ - name : Bot End message
175+ uses : actions/github-script@v7
176+ with :
177+ script : |
178+ const username = context.payload.pull_request.user.login;
179+ const ref = context.payload.pull_request.head.ref;
180+ github.rest.issues.createComment({
181+ owner: context.repo.owner,
182+ repo: context.repo.repo,
183+ issue_number: context.payload.pull_request.number,
184+ body: `user @${username} updated the PR on branch ${ref}. YAML Linting and Syntax Checks rerun and was completed.`,
185+ });
0 commit comments