37d88683b0
删除后,同一 GitHub 来源可通过“新建迁移”重新拉取,生成新的项目、任务和仓库。 旧任务的“重试迁移”保持原有校验。
48 lines
2.0 KiB
Java
48 lines
2.0 KiB
Java
package com.linkerbot.vibemake.common;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.SecureRandom;
|
|
|
|
/** 仅用于项目编码;旧的 UUID 编码继续有效。 */
|
|
public final class ProjectCodes {
|
|
public static final String ALPHABET = "0123456789abcdefghjkmnpqrstvwxyz";
|
|
private static final SecureRandom RANDOM = new SecureRandom();
|
|
|
|
private ProjectCodes() {}
|
|
|
|
public static String next() {
|
|
StringBuilder code = new StringBuilder(12);
|
|
for (int i = 0; i < 12; i++) code.append(ALPHABET.charAt(RANDOM.nextInt(32)));
|
|
return code.toString();
|
|
}
|
|
|
|
public static String github(long repositoryId) {
|
|
return github(repositoryId, 0);
|
|
}
|
|
|
|
/** 同一来源删除后重新迁移时使用新一代编码,避免复用旧项目及其仓库、资源。 */
|
|
public static String github(long repositoryId, long generation) {
|
|
if (repositoryId <= 0) throw new IllegalArgumentException("GitHub 仓库 ID 必须为正数");
|
|
if (generation < 0) throw new IllegalArgumentException("迁移代次不能为负数");
|
|
String seed = generation == 0 ? "github-import:v1:" + repositoryId
|
|
: "github-import:v2:" + repositoryId + ":" + generation;
|
|
try {
|
|
byte[] hash =
|
|
MessageDigest.getInstance("SHA-256")
|
|
.digest(seed.getBytes(StandardCharsets.UTF_8));
|
|
StringBuilder code = new StringBuilder(12);
|
|
for (int i = 0; i < 12; i++) {
|
|
int offset = i * 5;
|
|
int pair = (hash[offset / 8] & 255) << 8;
|
|
if (offset / 8 + 1 < hash.length) pair |= hash[offset / 8 + 1] & 255;
|
|
code.append(ALPHABET.charAt((pair >>> (11 - offset % 8)) & 31));
|
|
}
|
|
return code.toString();
|
|
} catch (NoSuchAlgorithmException error) {
|
|
throw new IllegalStateException("SHA-256 不可用", error);
|
|
}
|
|
}
|
|
}
|