-
Notifications
You must be signed in to change notification settings - Fork 274
121 lines (97 loc) · 4.19 KB
/
check-project.yml
File metadata and controls
121 lines (97 loc) · 4.19 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
on:
issues:
types: [opened, edited]
jobs:
checkProject:
if: "${{ contains(github.event.issue.body, 'Describe the bug') }}"
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
name: Check for StackBlitz or Bolt reproduction link
with:
script: |
const issue = await github.rest.issues.get({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
});
const projectInfo = parseProjectInfo(issue.data.body ?? '');
if (!projectInfo) {
// if no project info is found, we can assume that the user did not provide a StackBlitz or Bolt reproduction link
await comment(issue.data, 'We noticed that you did not provide a StackBlitz or Bolt reproduction link. Please update the issue and add a link to a public project.');
return;
}
const projectExists = await checkProject(projectInfo);
if (!projectExists) {
// if the response is not ok, we can assume that the project is not public
await comment(issue.data, 'We noticed that the project you shared is not public. Please change the visibility of the project to either secret or public.');
return;
}
// we can remove the label in case it was added before
await removeLabel();
function parseProjectInfo(comment) {
const reproRegex = /https:\/\/(?:stackblitz\.com|bolt\.new)\/(?:(?:~|c)\/)?(?:edit\/)?(?<slug>[a-zA-Z0-9-]+)/i;
const { slug } = comment.match(reproRegex)?.groups || {};
if (!slug) {
return null;
}
// if the slug is `github`, we test if a GitHub link is provided
if (slug === 'github') {
const githubRegex = /https:\/\/stackblitz\.com\/(?:~\/(?:github\.com|github_project)|github)\/(?<owner>[\w\-_]+)\/(?<repo>[\w\-_]+)/i;
const { owner, repo } = comment.match(githubRegex)?.groups || {};
if (owner && repo) {
return { type: 'github', owner, repo };
}
}
return { type: 'stackblitz', slug };
}
async function checkProject(projectInfo) {
if (projectInfo.type === 'github') {
const { owner, repo } = projectInfo;
try {
const response = await github.rest.repos.get({
owner,
repo,
});
return response.status === 200;
} catch {
return false;
}
}
if (projectInfo.type === 'stackblitz') {
const { slug } = projectInfo;
const response = await fetch(`https://stackblitz.com/api/projects/${slug}`, {
method: 'HEAD',
});
return response.ok;
}
return false;
}
async function comment(issue, body) {
if (issue.labels.some((label) => label.name === 'needs reproduction')) {
// do not comment or add the label again if we already have the label
return;
}
await Promise.allSettled([
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body,
}),
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['needs reproduction'],
})
]);
}
async function removeLabel() {
await github.rest.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: 'needs reproduction',
});
}