From 58ab192af75e37eb5c2447b7dba61fc9d069d44a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=B8=AD=E9=B8=AD=E3=80=8C=E3=82=AB=E3=83=A2=E3=80=8D?=
<89643991+DuckDuckStudio@users.noreply.github.com>
Date: Tue, 16 Jun 2026 10:46:25 +0800
Subject: [PATCH 1/5] =?UTF-8?q?refactor:=20=E5=B0=86=E5=B8=AE=E5=8A=A9?=
=?UTF-8?q?=E7=9B=B8=E5=85=B3=E5=8F=82=E6=95=B0=E5=88=A4=E6=96=AD=E5=90=88?=
=?UTF-8?q?=E5=B9=B6=E5=88=B0=E6=A8=A1=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
PinAction/Program.cs | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)
diff --git a/PinAction/Program.cs b/PinAction/Program.cs
index ee2398c..6266a8a 100644
--- a/PinAction/Program.cs
+++ b/PinAction/Program.cs
@@ -19,14 +19,10 @@ static int Main(string[] args)
{
if (
(args.Length == 0) || // 不提供参数
- (
- // 单独提供帮助相关参数
- (args.Length == 1) &&
- (args[0] is "help"
- or "--help"
- or "-h"
- or "/?")
- )
+ args is ["help"
+ or "--help"
+ or "-h"
+ or "/?"]
)
{
Console.WriteLine(Strings.Help);
From ce1d2151f8c0e40e00702f2105bd7342a66d96eb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=B8=AD=E9=B8=AD=E3=80=8C=E3=82=AB=E3=83=A2=E3=80=8D?=
<89643991+DuckDuckStudio@users.noreply.github.com>
Date: Tue, 16 Jun 2026 10:47:12 +0800
Subject: [PATCH 2/5] =?UTF-8?q?refactor(Main):=20=E4=BD=BF=E7=94=A8?=
=?UTF-8?q?=E6=98=BE=E5=BC=8F=20private=20=E4=BF=AE=E9=A5=B0=E7=AC=A6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
PinAction/Program.cs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/PinAction/Program.cs b/PinAction/Program.cs
index 6266a8a..ab29c6b 100644
--- a/PinAction/Program.cs
+++ b/PinAction/Program.cs
@@ -15,7 +15,7 @@ internal partial class Program
///
/// 参数
/// 退出代码
- static int Main(string[] args)
+ private static int Main(string[] args)
{
if (
(args.Length == 0) || // 不提供参数
From 53023796373632dbf74cb1630cd1e435ebf08c88 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=B8=AD=E9=B8=AD=E3=80=8C=E3=82=AB=E3=83=A2=E3=80=8D?=
<89643991+DuckDuckStudio@users.noreply.github.com>
Date: Tue, 16 Jun 2026 10:48:03 +0800
Subject: [PATCH 3/5] =?UTF-8?q?refactor:=20=E5=86=85=E8=81=94=20'out'=20?=
=?UTF-8?q?=E5=8F=98=E9=87=8F=E5=A3=B0=E6=98=8E?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
PinAction/Program.cs | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/PinAction/Program.cs b/PinAction/Program.cs
index ab29c6b..e60c355 100644
--- a/PinAction/Program.cs
+++ b/PinAction/Program.cs
@@ -149,8 +149,7 @@ private static bool PinActionHash(string path)
}
- string? hash;
- if (!pinedActions.TryGetValue($"{repo}@{tag}", out hash))
+ if (!pinedActions.TryGetValue($"{repo}@{tag}", out string? hash))
{
// 尝试 tags/{tag} 和 heads/{tag}
foreach (string refType in new[] { "tags", "heads" })
From 4b5eb0e09878f35d85970a698bdd8ec05aaa06da Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=B8=AD=E9=B8=AD=E3=80=8C=E3=82=AB=E3=83=A2=E3=80=8D?=
<89643991+DuckDuckStudio@users.noreply.github.com>
Date: Tue, 16 Jun 2026 10:57:08 +0800
Subject: [PATCH 4/5] =?UTF-8?q?refactor:=20=E5=8F=8D=E8=BD=AC=20'if'=20?=
=?UTF-8?q?=E4=BB=A5=E5=87=8F=E5=B0=91=E5=B5=8C=E5=A5=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
PinAction/Program.cs | 171 +++++++++++++++++++++----------------------
1 file changed, 84 insertions(+), 87 deletions(-)
diff --git a/PinAction/Program.cs b/PinAction/Program.cs
index e60c355..5bd0233 100644
--- a/PinAction/Program.cs
+++ b/PinAction/Program.cs
@@ -110,114 +110,111 @@ private static bool PinActionHash(string path)
for (int i = 0; i < lines.Length; i++)
{
- if (lines[i].Contains("uses:"))
- {
- // 移除注释
- string[] cleanLinePaths = lines[i].Split('#');
+ if (!lines[i].Contains("uses:")) continue;
- // 非注释内容匹配正则 "^\s+uses:\s*([^@]+)@([^@|\s]+)\s*$"
- Match match = UsesRegex().Match(cleanLinePaths[0]);
+ // 移除注释
+ string[] cleanLinePaths = lines[i].Split('#');
- if (match.Success)
- {
- string repo = match.Groups[1].Value;
- string tag = match.Groups[2].Value;
+ // 非注释内容匹配正则 "^\s+uses:\s*([^@]+)@([^@|\s]+)\s*$"
+ Match match = UsesRegex().Match(cleanLinePaths[0]);
+ if (!match.Success) continue;
+
+ string repo = match.Groups[1].Value;
+ string tag = match.Groups[2].Value;
+
+ AnsiConsole.MarkupLine($"{Print.MSHead.Information} {string.Format(Strings.FindAction, Path.GetRelativePath(Environment.CurrentDirectory, path), Markup.Escape($"{repo}@{tag}"))}");
+
+ // 在这里你可以定义排除哪些项
+ // 例如排除以 actions/ 开头的项(actions/*@*)
+ // if (repo.StartsWith("actions/"))
+ // {
+ // AnsiConsole.MarkupLine($"{Print.MSHead.Warning} 跳过 {repo}@{tag},因为它是官方工作流");
+ // continue;
+ // }
- AnsiConsole.MarkupLine($"{Print.MSHead.Information} {string.Format(Strings.FindAction, Path.GetRelativePath(Environment.CurrentDirectory, path), Markup.Escape($"{repo}@{tag}"))}");
- // 在这里你可以定义排除哪些项
- // 例如排除以 actions/ 开头的项(actions/*@*)
- // if (repo.StartsWith("actions/"))
- // {
- // AnsiConsole.MarkupLine($"{Print.MSHead.Warning} 跳过 {repo}@{tag},因为它是官方工作流");
- // continue;
- // }
+ // 操作前检查
+ // 检查是否已经是哈希值(40个十六进制字符)
+ if (HashRegex().IsMatch(tag))
+ {
+ AnsiConsole.MarkupLine($"{Print.MSHead.Information} {string.Format(Strings.SkippingAlreadyPinnedHashes, Markup.Escape($"{repo}@{tag}"))}");
+ continue;
+ }
+ // 检查仓库是否是 owner/repo 的格式
+ if (repo.Split('/').Length != 2)
+ {
+ AnsiConsole.MarkupLine($"{Print.MSHead.Warning} {Markup.Escape(repo)} 看起来不像是仓库的格式,跳过 {Markup.Escape($"{repo}@{tag}")}");
+ continue;
+ }
- // 操作前检查
- // 检查是否已经是哈希值(40个十六进制字符)
- if (HashRegex().IsMatch(tag))
+ if (!pinedActions.TryGetValue($"{repo}@{tag}", out string? hash))
+ {
+ // 尝试 tags/{tag} 和 heads/{tag}
+ foreach (string refType in new[] { "tags", "heads" })
+ {
+ try
{
- AnsiConsole.MarkupLine($"{Print.MSHead.Information} {string.Format(Strings.SkippingAlreadyPinnedHashes, Markup.Escape($"{repo}@{tag}"))}");
- continue;
+ // 获取该版本的 git commit hash
+ Reference reference = GitHubClient.Git.Reference.Get(repo.Split('/')[0], repo.Split('/')[1], $"{refType}/{tag}").Result;
+ hash = reference.Object.Sha;
+
+ pinedActions.TryAdd($"{repo}@{tag}", hash);
+ break;
}
- // 检查仓库是否是 owner/repo 的格式
- if (repo.Split('/').Length != 2)
+ catch (AggregateException ex) when (ex.InnerException != null)
{
- AnsiConsole.MarkupLine($"{Print.MSHead.Warning} {Markup.Escape(repo)} 看起来不像是仓库的格式,跳过 {Markup.Escape($"{repo}@{tag}")}");
- continue;
- }
-
+ AnsiConsole.Markup($"{Print.MSHead.Warning} {Strings.ErrorGetHashFailed}");
- if (!pinedActions.TryGetValue($"{repo}@{tag}", out string? hash))
- {
- // 尝试 tags/{tag} 和 heads/{tag}
- foreach (string refType in new[] { "tags", "heads" })
+ switch (ex.InnerException)
{
- try
- {
- // 获取该版本的 git commit hash
- Reference reference = GitHubClient.Git.Reference.Get(repo.Split('/')[0], repo.Split('/')[1], $"{refType}/{tag}").Result;
- hash = reference.Object.Sha;
-
- pinedActions.TryAdd($"{repo}@{tag}", hash);
- break;
- }
- catch (AggregateException ex) when (ex.InnerException != null)
- {
- AnsiConsole.Markup($"{Print.MSHead.Warning} {Strings.ErrorGetHashFailed}");
-
- switch (ex.InnerException)
+ // 还要再试的用 break;
+ // 最后一次的用 continue;
+ // 直接整个程序失败的 return false;
+ case Octokit.NotFoundException:
+ if (refType == "tags")
+ {
+ AnsiConsole.MarkupLineInterpolated($"[yellow]{string.Format(Strings.ErrorTagNotFound, tag)}[/]");
+ break;
+ }
+ else
{
- // 还要再试的用 break;
- // 最后一次的用 continue;
- // 直接整个程序失败的 return false;
- case Octokit.NotFoundException:
- if (refType == "tags")
- {
- AnsiConsole.MarkupLineInterpolated($"[yellow]{string.Format(Strings.ErrorTagNotFound, tag)}[/]");
- break;
- }
- else
- {
- AnsiConsole.MarkupLineInterpolated($"[red]{string.Format(Strings.ErrorBranchNotFound, tag, $"{repo}@{tag}")}[/]");
- continue;
- }
- case Octokit.RateLimitExceededException:
- AnsiConsole.MarkupLine($"[yellow]{Strings.ErrorRateLimitExceeded}[/]");
- return false;
- default:
- AnsiConsole.MarkupLineInterpolated($"[red]{ex.InnerException.Message}[/]");
- continue;
+ AnsiConsole.MarkupLineInterpolated($"[red]{string.Format(Strings.ErrorBranchNotFound, tag, $"{repo}@{tag}")}[/]");
+ continue;
}
- }
+ case Octokit.RateLimitExceededException:
+ AnsiConsole.MarkupLine($"[yellow]{Strings.ErrorRateLimitExceeded}[/]");
+ return false;
+ default:
+ AnsiConsole.MarkupLineInterpolated($"[red]{ex.InnerException.Message}[/]");
+ continue;
}
}
+ }
+ }
#if DEBUG
- else
- {
- AnsiConsole.MarkupLine($"{Print.MSHead.Debug} 读取缓存 {Markup.Escape($"{repo}@{hash}")} # {Markup.Escape(tag)}");
- }
+ else
+ {
+ AnsiConsole.MarkupLine($"{Print.MSHead.Debug} 读取缓存 {Markup.Escape($"{repo}@{hash}")} # {Markup.Escape(tag)}");
+ }
#endif
- if (hash is null)
- {
- continue;
- }
-
- lines[i] = $"{cleanLinePaths[0].Replace($"{repo}@{tag}", $"{repo}@{hash}")} # {tag}";
- if (cleanLinePaths.Length > 1)
- {
- // 将注释部分重新添加到行末
- foreach (string commentPart in cleanLinePaths.Skip(1))
- {
- lines[i] += commentPart;
- }
- }
+ if (hash is null)
+ {
+ continue;
+ }
- AnsiConsole.MarkupLine($"{Print.MSHead.Success} {Strings.Pinned} {Markup.Escape($"{repo}@{hash}")} # {Markup.Escape(tag)}");
+ lines[i] = $"{cleanLinePaths[0].Replace($"{repo}@{tag}", $"{repo}@{hash}")} # {tag}";
+ if (cleanLinePaths.Length > 1)
+ {
+ // 将注释部分重新添加到行末
+ foreach (string commentPart in cleanLinePaths.Skip(1))
+ {
+ lines[i] += commentPart;
}
}
+
+ AnsiConsole.MarkupLine($"{Print.MSHead.Success} {Strings.Pinned} {Markup.Escape($"{repo}@{hash}")} # {Markup.Escape(tag)}");
}
// 将修改后的内容写回文件
From 2ef70a3caeb3952f344ae830250d180bf54bb48c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=B8=AD=E9=B8=AD=E3=80=8C=E3=82=AB=E3=83=A2=E3=80=8D?=
<89643991+DuckDuckStudio@users.noreply.github.com>
Date: Tue, 16 Jun 2026 11:00:59 +0800
Subject: [PATCH 5/5] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=E5=91=BD?=
=?UTF-8?q?=E5=90=8D=E4=BB=A5=E5=8C=B9=E9=85=8D=E5=91=BD=E5=90=8D=E8=A7=84?=
=?UTF-8?q?=E5=88=99?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
PinAction/Program.cs | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/PinAction/Program.cs b/PinAction/Program.cs
index 5bd0233..cc4e0d0 100644
--- a/PinAction/Program.cs
+++ b/PinAction/Program.cs
@@ -148,7 +148,7 @@ private static bool PinActionHash(string path)
}
- if (!pinedActions.TryGetValue($"{repo}@{tag}", out string? hash))
+ if (!PinedActions.TryGetValue($"{repo}@{tag}", out string? hash))
{
// 尝试 tags/{tag} 和 heads/{tag}
foreach (string refType in new[] { "tags", "heads" })
@@ -159,7 +159,7 @@ private static bool PinActionHash(string path)
Reference reference = GitHubClient.Git.Reference.Get(repo.Split('/')[0], repo.Split('/')[1], $"{refType}/{tag}").Result;
hash = reference.Object.Sha;
- pinedActions.TryAdd($"{repo}@{tag}", hash);
+ PinedActions.TryAdd($"{repo}@{tag}", hash);
break;
}
catch (AggregateException ex) when (ex.InnerException != null)
@@ -233,7 +233,7 @@ private static bool PinActionHash(string path)
/// 缓存已固定哈希值的 Action,第二次遇到时不用再去请求 GitHub API 获取。
/// 按 repo@tag, hash 一对存储。
///
- private static readonly ConcurrentDictionary pinedActions = new();
+ private static readonly ConcurrentDictionary PinedActions = new();
[GeneratedRegex(@"^\s*uses:\s*([^@]+)@([^@|\s]+)\s*$")]
private static partial Regex UsesRegex();