Skip to content

Commit dfd5b77

Browse files
thomhurstclaude
andauthored
refactor: Replace int.Parse with TryParse for safe parsing (#1667)
Replace unsafe int.Parse/long.Parse with TryParse for proper error handling in build modules: - SkipIfDependencyPullRequest: Handle invalid RefName and RepositoryId by returning true (skip the check) instead of throwing FormatException - CreateReleaseModule: Throw clear InvalidOperationException with context if RepositoryId parsing fails Fixes #1641 Fixes #1642 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 86b528b commit dfd5b77

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

src/ModularPipelines.Build/Attributes/SkipIfDependencyPullRequest.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ModularPipelines.Attributes;
1+
using ModularPipelines.Attributes;
22
using ModularPipelines.Context;
33
using ModularPipelines.GitHub.Extensions;
44

@@ -15,10 +15,19 @@ public override async Task<bool> Condition(IPipelineHookContext pipelineContext)
1515
return true;
1616
}
1717

18-
var prNumber = int.Parse(gitHubEnvironmentVariables.RefName!.Split('/').First());
18+
var refNamePart = gitHubEnvironmentVariables.RefName?.Split('/').FirstOrDefault();
19+
if (!int.TryParse(refNamePart, out var prNumber))
20+
{
21+
return true;
22+
}
23+
24+
if (!long.TryParse(gitHubEnvironmentVariables.RepositoryId, out var repositoryId))
25+
{
26+
return true;
27+
}
1928

20-
var pr = await pipelineContext.GitHub().Client.PullRequest.Get(int.Parse(gitHubEnvironmentVariables.RepositoryId!), prNumber);
29+
var pr = await pipelineContext.GitHub().Client.PullRequest.Get(repositoryId, prNumber);
2130

2231
return pr.Labels.All(x => x.Name != "dependencies");
2332
}
24-
}
33+
}

src/ModularPipelines.Build/Modules/CreateReleaseModule.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,17 @@ public async Task<SkipDecision> ShouldSkip(IPipelineContext context)
5353
{
5454
var versionInfoResult = context.GetModule<NugetVersionGeneratorModule, string>();
5555

56-
return await context.GitHub().Client.Repository.Release.Create(long.Parse(context.GitHub().EnvironmentVariables.RepositoryId!),
56+
var repositoryIdString = context.GitHub().EnvironmentVariables.RepositoryId;
57+
if (!long.TryParse(repositoryIdString, out var repositoryId))
58+
{
59+
throw new InvalidOperationException($"Failed to parse RepositoryId '{repositoryIdString}' as a valid long integer.");
60+
}
61+
62+
return await context.GitHub().Client.Repository.Release.Create(repositoryId,
5763
new NewRelease($"v{versionInfoResult.Value}")
5864
{
5965
Name = versionInfoResult.Value,
6066
GenerateReleaseNotes = true,
6167
});
6268
}
63-
}
69+
}

0 commit comments

Comments
 (0)