-
Notifications
You must be signed in to change notification settings - Fork 82
184 lines (161 loc) · 7.1 KB
/
contributor-check.yml
File metadata and controls
184 lines (161 loc) · 7.1 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
name: Contributor Trust Check
on:
pull_request_target:
types: [opened, reopened, synchronize]
permissions:
contents: read
pull-requests: write
jobs:
check:
name: Scan PR author
runs-on: ubuntu-24.04
concurrency:
group: brin-check-${{ github.event.pull_request.number }}
cancel-in-progress: true
steps:
- name: Query Brin API
id: brin
run: |
AUTHOR="${{ github.event.pull_request.user.login }}"
RESPONSE=$(curl -sf "https://api.brin.sh/contributor/${AUTHOR}?details=true&mode=full" || echo '{}')
echo "response<<BRIN_EOF" >> "$GITHUB_OUTPUT"
echo "$RESPONSE" >> "$GITHUB_OUTPUT"
echo "BRIN_EOF" >> "$GITHUB_OUTPUT"
- name: Apply label and comment
uses: actions/github-script@v7
env:
BRIN_RESPONSE: ${{ steps.brin.outputs.response }}
with:
script: |
const marker = "<!-- brin-check -->";
const pr = context.payload.pull_request;
let data;
try {
data = JSON.parse(process.env.BRIN_RESPONSE);
} catch {
core.warning("Failed to parse Brin API response");
return;
}
if (!data.score && data.score !== 0) {
core.warning("Brin API returned no score");
return;
}
const score = data.score;
const verdict = data.verdict ?? "unknown";
const confidence = data.confidence ?? "unknown";
const isSafe = verdict === "safe";
const labels = [
{ name: "contributor:verified", color: "0969da", description: "Contributor passed trust analysis." },
{ name: "contributor:flagged", color: "e16f24", description: "Contributor flagged for review by trust analysis." },
];
for (const label of labels) {
try {
const { data: existing } = await github.rest.issues.getLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: label.name,
});
if (existing.color !== label.color || (existing.description ?? "") !== label.description) {
await github.rest.issues.updateLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: label.name,
color: label.color,
description: label.description,
});
}
} catch (error) {
if (error.status !== 404) throw error;
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: label.name,
color: label.color,
description: label.description,
});
}
}
const nextLabel = isSafe ? "contributor:verified" : "contributor:flagged";
const labelNames = labels.map((l) => l.name);
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
});
const preserved = currentLabels
.map((l) => l.name)
.filter((name) => !labelNames.includes(name));
await github.rest.issues.setLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
labels: [...preserved, nextLabel],
});
core.info(`PR #${pr.number}: ${verdict} -> ${nextLabel}`);
if (isSafe) {
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
per_page: 100,
});
const existing = comments.find((c) => c.body?.includes(marker));
if (existing) {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
});
}
return;
}
const sub = data.sub_scores ?? {};
const fmt = (v) => (v != null ? String(Math.round(v)) : "\u2014");
const verdictEmoji = {
caution: "\u26A0\uFE0F",
suspicious: "\u26D4",
dangerous: "\uD83D\uDEA8",
}[verdict] ?? "\u2753";
let body = `${marker}\n`;
body += `### ${verdictEmoji} Contributor Trust Check \u2014 Review Recommended\n\n`;
body += `This contributor's profile shows patterns that may warrant additional review. `;
body += `This is based on their GitHub activity, not the contents of this PR.\n\n`;
body += `**[${data.name}](https://github.com/${data.name})** \u00b7 Score: **${score}**/100\n\n`;
if (data.threats && data.threats.length > 0) {
body += `#### Why was this flagged?\n\n`;
body += `| Signal | Severity | Detail |\n|--------|----------|--------|\n`;
for (const t of data.threats) {
body += `| ${t.type} | ${t.severity} | ${t.detail} |\n`;
}
body += `\n`;
}
body += `<details>\n<summary>Dimension breakdown</summary>\n\n`;
body += `| Dimension | Score | What it measures |\n|-----------|-------|------------------|\n`;
body += `| Identity | ${fmt(sub.identity)} | Account age, contribution history, GPG keys, org memberships |\n`;
body += `| Behavior | ${fmt(sub.behavior)} | PR patterns, unsolicited contribution ratio, activity cadence |\n`;
body += `| Content | ${fmt(sub.content)} | PR body substance, issue linkage, contribution quality |\n`;
body += `| Graph | ${fmt(sub.graph)} | Cross-repo trust, co-contributor relationships |\n\n`;
body += `</details>\n\n`;
body += `<sub>Analyzed by [Brin](https://brin.sh) \u00b7 [Full profile](${data.url}?details=true)</sub>\n`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
per_page: 100,
});
const existingComment = comments.find((c) => c.body?.includes(marker));
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body,
});
}