Skip to content

Commit d37946f

Browse files
authored
chore(ci): give new workspace comment workflow correct perms (#2606)
* chore(ci): give new workspace comment workflow correct perms Signed-off-by: Hope Hadfield <hhadfiel@redhat.com> * Update script to remove vulnerability Signed-off-by: Hope Hadfield <hhadfiel@redhat.com> --------- Signed-off-by: Hope Hadfield <hhadfiel@redhat.com>
1 parent 32a88b0 commit d37946f

1 file changed

Lines changed: 43 additions & 40 deletions

File tree

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Comment on PRs that add a workspace
22

33
on:
4-
pull_request:
4+
pull_request_target:
55
types: [opened, reopened, synchronize]
66
branches: [main]
77

@@ -25,64 +25,67 @@ jobs:
2525
with:
2626
egress-policy: audit
2727

28-
- name: Checkout PR head
29-
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
28+
- name: Detect new workspace and post comment if needed
29+
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
3030
with:
31-
ref: ${{ github.event.pull_request.head.sha }}
32-
fetch-depth: 0
31+
script: |
32+
const owner = context.repo.owner;
33+
const repo = context.repo.repo;
34+
const baseSha = context.payload.pull_request.base.sha;
35+
const headSha = context.payload.pull_request.head.sha;
3336
34-
- name: Fetch base commit
35-
run: git fetch --no-tags origin ${{ github.event.pull_request.base.sha }}
37+
async function listWorkspaceDirNames(ref) {
38+
try {
39+
const { data } = await github.rest.repos.getContent({
40+
owner,
41+
repo,
42+
path: 'workspaces',
43+
ref,
44+
});
45+
if (!Array.isArray(data)) {
46+
return [];
47+
}
48+
return data
49+
.filter((entry) => entry.type === 'dir')
50+
.map((entry) => entry.name)
51+
.sort();
52+
} catch (e) {
53+
if (e.status === 404) {
54+
return [];
55+
}
56+
throw e;
57+
}
58+
}
3659
37-
- name: Detect new top-level directories under workspaces/
38-
id: detect
39-
env:
40-
BASE_SHA: ${{ github.event.pull_request.base.sha }}
41-
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
42-
run: |
43-
set -euo pipefail
44-
if git cat-file -e "${BASE_SHA}:workspaces" 2>/dev/null; then
45-
git ls-tree "${BASE_SHA}:workspaces" | awk '$2=="tree" {print $4}' | sort -u > /tmp/base_ws.txt
46-
else
47-
: > /tmp/base_ws.txt
48-
fi
49-
if git cat-file -e "${HEAD_SHA}:workspaces" 2>/dev/null; then
50-
git ls-tree "${HEAD_SHA}:workspaces" | awk '$2=="tree" {print $4}' | sort -u > /tmp/head_ws.txt
51-
else
52-
: > /tmp/head_ws.txt
53-
fi
54-
comm -13 /tmp/base_ws.txt /tmp/head_ws.txt > /tmp/new_ws.txt || true
55-
if [ ! -s /tmp/new_ws.txt ]; then
56-
echo "has_new=false" >> "$GITHUB_OUTPUT"
57-
else
58-
echo "has_new=true" >> "$GITHUB_OUTPUT"
59-
fi
60+
const baseDirs = await listWorkspaceDirNames(baseSha);
61+
const headDirs = await listWorkspaceDirNames(headSha);
62+
const baseSet = new Set(baseDirs);
63+
const hasNew = headDirs.some((d) => !baseSet.has(d));
64+
if (!hasNew) {
65+
return;
66+
}
6067
61-
- name: Post PR comment
62-
if: steps.detect.outputs.has_new == 'true'
63-
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
64-
with:
65-
script: |
6668
const marker = '<!-- rhdh-new-workspace-pr-comment -->';
6769
const issue_number = context.payload.pull_request.number;
6870
const comments = await github.paginate(github.rest.issues.listComments, {
69-
owner: context.repo.owner,
70-
repo: context.repo.repo,
71+
owner,
72+
repo,
7173
issue_number,
7274
per_page: 100,
7375
});
7476
if (comments.some((c) => c.body?.includes(marker))) {
7577
return;
7678
}
77-
const contributing = `https://github.com/${context.repo.owner}/${context.repo.repo}/blob/main/CONTRIBUTING.md#submitting-a-pull-request-for-a-new-workspace`;
79+
80+
const contributing = `https://github.com/${owner}/${repo}/blob/main/CONTRIBUTING.md#submitting-a-pull-request-for-a-new-workspace`;
7881
const body = [
7982
marker,
8083
'',
8184
'This pull request adds a new top-level directory under `workspaces/`. Please follow **[Submitting a Pull Request for a New Workspace](' + contributing + ')** in `CONTRIBUTING.md`.',
8285
].join('\n');
8386
await github.rest.issues.createComment({
84-
owner: context.repo.owner,
85-
repo: context.repo.repo,
87+
owner,
88+
repo,
8689
issue_number,
8790
body,
8891
});

0 commit comments

Comments
 (0)