Skip to content
This repository was archived by the owner on Jan 10, 2024. It is now read-only.

Commit 8ec497e

Browse files
committed
update allow block format, force refresh
1 parent 07510ea commit 8ec497e

File tree

5 files changed

+73
-22
lines changed

5 files changed

+73
-22
lines changed

components/repo-detail/BlockPane.tsx

+53-15
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ export default function BlockPane({
9393

9494
if (appContext.isPrivate) {
9595
isAllowedBlock =
96-
appContext.blocksConfig.allow.includes(blockKey) ||
97-
(manageBlockResult.status === "success" &&
98-
block.owner === "githubnext" &&
99-
block.repo === "blocks-examples");
96+
!!block && isBlockOnAllowList(appContext.blocksConfig.allow, block);
10097
}
10198

10299
return (
@@ -161,14 +158,13 @@ const NotAllowedWarning = ({
161158
isBranchable: boolean;
162159
branchName: string;
163160
}) => {
164-
const [isAdding, setIsAdding] = useState(false);
165-
const allowList = blocksConfig.allow;
161+
const [proposedBlock, setProposedBlock] = useState<AllowBlock | null>(null);
166162
const queryClient = useQueryClient();
167163

168164
return (
169165
<div className="overflow-y-auto w-full flex-1 pb-10 flex items-center justify-center">
170166
<div className="max-w-3xl p-10">
171-
<Flash variant="danger" sx={{ mb: 3 }}>
167+
<Flash variant="danger" sx={{ mb: 4 }}>
172168
<p className="text-red-700">
173169
<StyledOcticon icon={AlertFillIcon} />
174170
This block is not allowed to be used in this repository.
@@ -180,33 +176,58 @@ const NotAllowedWarning = ({
180176
it to the allowlist in{" "}
181177
<code className="text-sm">.github/blocks/config.json</code>.
182178
</p>
183-
<div className="flex space-x-2">
179+
<div className="flex mt-6 space-x-2">
184180
<Button
185-
onClick={() => setIsAdding(true)}
181+
onClick={() =>
182+
setProposedBlock({
183+
owner: block.owner,
184+
repo: block.repo,
185+
id: block.id,
186+
})
187+
}
186188
variant="primary"
187-
className="mt-4"
189+
className="!font-normal"
190+
size="large"
191+
>
192+
Add <span className="font-semibold">{block.title}</span> to
193+
allowlist
194+
</Button>
195+
<Button
196+
onClick={() =>
197+
setProposedBlock({
198+
owner: block.owner,
199+
repo: block.repo,
200+
id: "*",
201+
})
202+
}
203+
variant="default"
204+
className="!font-normal"
188205
size="large"
189206
>
190-
Add {block.title} to allowlist
207+
Add{" "}
208+
<span className="font-semibold">
209+
all blocks in {block.owner}/{block.repo}
210+
</span>{" "}
211+
to allowlist
191212
</Button>
192213
</div>
193214
</div>
194-
{isAdding && (
215+
{!!proposedBlock && (
195216
<CommitCodeDialog
196217
repo={context.repo}
197218
owner={context.owner}
198219
path=".github/blocks/config.json"
199220
newCode={JSON.stringify(
200221
{
201222
...blocksConfig,
202-
allow: [...(blocksConfig.allow || []), blockKey],
223+
allow: [...(blocksConfig.allow || []), proposedBlock],
203224
},
204225
null,
205226
2
206227
)}
207228
currentCode={JSON.stringify(blocksConfig, null, 2)}
208229
onCommit={() => {
209-
setIsAdding(false);
230+
setProposedBlock(null);
210231
queryClient.invalidateQueries(
211232
QueryKeyMap.file.factory({
212233
owner: context.owner,
@@ -217,7 +238,7 @@ const NotAllowedWarning = ({
217238
);
218239
}}
219240
onCancel={() => {
220-
setIsAdding(false);
241+
setProposedBlock(null);
221242
}}
222243
isOpen
223244
branchName={branchName}
@@ -227,3 +248,20 @@ const NotAllowedWarning = ({
227248
</div>
228249
);
229250
};
251+
252+
const isBlockOnAllowList = (allowList: AllowBlock[], block: Block) => {
253+
if (!allowList) return false;
254+
// always allow example blocks
255+
if (block.owner === "githubnext" && block.repo === "blocks-examples")
256+
return true;
257+
return allowList.some((allowBlock) => {
258+
return doesAllowBlockMatch(allowBlock, block);
259+
});
260+
};
261+
262+
const doesAllowBlockMatch = (allowBlock: AllowBlock, block: Block) => {
263+
return ["owner", "repo", "id"].every((key) => {
264+
if (!allowBlock[key]) return false;
265+
return pm([allowBlock[key]], { bash: true, dot: true })(block[key]);
266+
});
267+
};

ghapi/index.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,21 @@ export const getFileContent: (
8888
) => Promise<FileData> = async (ctx) => {
8989
let meta = ctx.meta as BlocksQueryMeta;
9090
let params = ctx.queryKey[1];
91-
const { path, owner, repo, fileRef = "HEAD" } = params;
91+
const { path, owner, repo, fileRef = "HEAD", doForceCacheRefresh } = params;
9292
const query = fileRef && fileRef !== "HEAD" ? `?ref=${fileRef}` : "";
9393
const apiUrl = `repos/${owner}/${repo}/contents/${path}${query}`;
9494

9595
const file = path.split("/").pop() || "";
9696

97-
const res = await meta.ghapi(apiUrl);
97+
const res = await meta.ghapi(apiUrl, {
98+
headers: doForceCacheRefresh
99+
? {
100+
// this response is cached for 60s
101+
// we need to bypass this eg. when a metadata update creates a new file
102+
"If-None-Match": new Date().getTime().toString(),
103+
}
104+
: {},
105+
});
98106
if (res.status !== 200) {
99107
if (res.status === 404) {
100108
throw new Error(`File not found: ${owner}/${repo}: ${path}`);

index.d.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ type DevServerInfo = {
6969
devServer: string;
7070
};
7171

72+
type AllowBlock = {
73+
owner: string;
74+
repo: string;
75+
id: string;
76+
};
77+
7278
type BlocksConfig = {
73-
allow?: string[];
79+
allow?: AllowBlock[];
7480
};

lib/query-keys.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface FileKeyParams {
3737
owner: string;
3838
path: string;
3939
fileRef?: string;
40+
doForceCacheRefresh?: boolean;
4041
}
4142

4243
export interface BranchesKeyParams {

pages/[owner]/[repo]/blob/[...branchPath].tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,16 @@ function RepoDetailContainer({ installationUrl }: AppContextValue) {
9898
repo,
9999
path: ".github/blocks/config.json",
100100
fileRef: (router.query.sha || router.query.branchPath?.[0]) as string,
101+
doForceCacheRefresh: true,
101102
},
102103
{ enabled: queryClientMetaLoaded }
103104
);
104105
let blocksConfig = repoInfo?.private ? { allow: [] } : {};
105106
try {
106107
if (blocksConfigContentStatus === "success")
107108
blocksConfig = JSON.parse(blocksConfigContent.content);
108-
} catch (e) {
109-
console.log(e);
110-
}
109+
} catch (e) {}
111110

112-
// TODO: || repoInfo?.private
113111
if (repoInfoStatus === "error") {
114112
return (
115113
<div className="flex flex-col items-center justify-center h-full text-center">

0 commit comments

Comments
 (0)