-
Notifications
You must be signed in to change notification settings - Fork 5
152 lines (132 loc) · 5.9 KB
/
Copy pathassign-docs-pr.yml
File metadata and controls
152 lines (132 loc) · 5.9 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
name: Assign Docs PR
on:
pull_request:
types: [opened, reopened, edited, ready_for_review, synchronize]
permissions:
issues: write
pull-requests: write
jobs:
assign-docs-pr:
name: Assign docs PR to source PR author
runs-on: ubuntu-latest
steps:
- name: Assign source PR author
uses: actions/github-script@v7
with:
script: |
const docsPr = context.payload.pull_request;
const body = docsPr.body || '';
const handleAliases = new Map([
['tatiana', 'tatianainama'],
['tatiana-inama', 'tatianainama'],
]);
const normalizeHandle = (handle) => handleAliases.get(handle.toLowerCase()) || handle;
const sourceMatch = body.match(/lightdash\/lightdash(?:#|\/pull\/)(\d+)/i);
const contextualSourceMatch = body.match(/(?:upstream PR|source PR|triggered by|follows|follow-up to)[^#]{0,120}#(\d{5,})/is);
let sourcePrNumber = sourceMatch ? Number(sourceMatch[1]) : undefined;
let sourceReason = sourceMatch ? 'body source reference' : undefined;
if (!sourcePrNumber && contextualSourceMatch) {
sourcePrNumber = Number(contextualSourceMatch[1]);
sourceReason = 'body contextual source reference';
}
if (!sourcePrNumber) {
const events = await github.paginate(github.rest.issues.listEventsForTimeline, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: docsPr.number,
per_page: 100,
});
const sourceEvent = [...events].reverse().find((event) => (
event.event === 'cross-referenced' &&
event.source?.issue?.repository?.full_name === 'lightdash/lightdash' &&
event.source?.issue?.pull_request
));
if (sourceEvent) {
sourcePrNumber = sourceEvent.source.issue.number;
sourceReason = 'timeline cross-reference';
}
}
// Resolve the author to assign: prefer the source PR author, then fall
// back to an explicit `cc @author` mention in the docs PR body.
let author;
let reason;
if (sourcePrNumber) {
let sourcePr;
try {
({ data: sourcePr } = await github.rest.pulls.get({
owner: 'lightdash',
repo: 'lightdash',
pull_number: sourcePrNumber,
}));
} catch (error) {
core.info(`Could not load lightdash/lightdash#${sourcePrNumber} (${error.status || error.message}). Falling back to cc mention.`);
}
const sourceAuthor = sourcePr?.user?.login ? normalizeHandle(sourcePr.user.login) : undefined;
if (sourcePr && !sourceAuthor) {
core.info(`Source PR #${sourcePrNumber} has no author.`);
} else if (sourceAuthor?.endsWith('[bot]')) {
core.info(`Source PR #${sourcePrNumber} author is ${sourceAuthor}. Skipping bot assignment.`);
return;
} else if (sourceAuthor) {
author = sourceAuthor;
reason = sourceReason;
}
}
if (!author) {
const ccMatch = body.match(/(?:^|\n)\s*cc\s+@([A-Za-z0-9-]+)/i);
if (ccMatch) {
const ccAuthor = normalizeHandle(ccMatch[1]);
if (ccAuthor.endsWith('[bot]')) {
core.info(`cc mention is bot @${ccAuthor}. Skipping.`);
} else {
author = ccAuthor;
reason = 'cc mention';
}
}
}
if (!author && docsPr.user?.login && !docsPr.user.login.endsWith('[bot]')) {
author = normalizeHandle(docsPr.user.login);
reason = 'docs PR author';
}
if (!author) {
core.info('No lightdash/lightdash source PR author or cc mention found.');
return;
}
try {
const { data: issue } = await github.rest.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: docsPr.number,
assignees: [author],
});
const wasAssigned = issue.assignees?.some((assignee) => assignee.login === author);
if (wasAssigned) {
core.info(`Assigned docs PR #${docsPr.number} to ${author} via ${reason}.`);
return;
}
} catch (error) {
core.info(`Could not assign docs PR #${docsPr.number} to ${author} (${error.status || error.message}). Falling back to cc comment.`);
}
const fallbackBody = `cc @${author}`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: docsPr.number,
per_page: 100,
});
const hasFallback = comments.some((comment) => comment.body?.trim() === fallbackBody);
if (hasFallback) {
core.info(`Assignment did not land, but fallback comment already exists for ${author}.`);
return;
}
try {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: docsPr.number,
body: fallbackBody,
});
core.info(`Assignment did not land for ${author}. Posted fallback cc comment.`);
} catch (error) {
core.info(`Assignment and fallback comment both failed for ${author} (${error.status || error.message}).`);
}