Skip to content

Commit

Permalink
Initial rebase support
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Jun 15, 2024
1 parent 888d5ed commit 2d25593
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
15 changes: 9 additions & 6 deletions Runner/JobBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,17 @@ protected async Task RunProcessAsync(
string? logPrefix = null,
string? workDir = null,
bool checkExitCode = true,
Func<string, string>? processLogs = null,
CancellationToken cancellationToken = default)
{
processLogs ??= i => i;

if (logPrefix is not null)
{
logPrefix = $"[{logPrefix}] ";
}

await LogAsync($"{logPrefix}Running '{fileName} {arguments}'");
await LogAsync(processLogs($"{logPrefix}Running '{fileName} {arguments}'"));

using var process = new Process
{
Expand All @@ -288,8 +291,8 @@ protected async Task RunProcessAsync(
catch { }

await Task.WhenAll(
Task.Run(() => ReadOutputStreamAsync(process.StandardOutput)),
Task.Run(() => ReadOutputStreamAsync(process.StandardError)),
Task.Run(() => ReadOutputStreamAsync(process.StandardOutput), CancellationToken.None),
Task.Run(() => ReadOutputStreamAsync(process.StandardError), CancellationToken.None),
Task.Run(async () =>
{
try
Expand All @@ -301,7 +304,7 @@ await Task.WhenAll(
process.Kill(true);
throw;
}
}));
}, CancellationToken.None));

if (checkExitCode && process.ExitCode != 0)
{
Expand All @@ -310,7 +313,7 @@ await Task.WhenAll(

async Task ReadOutputStreamAsync(StreamReader reader)
{
while (await reader.ReadLineAsync() is string line)
while (await reader.ReadLineAsync(cancellationToken) is string line)
{
if (output is not null)
{
Expand All @@ -320,7 +323,7 @@ async Task ReadOutputStreamAsync(StreamReader reader)
}
}

await LogAsync($"{logPrefix}{line}");
await LogAsync(processLogs($"{logPrefix}{line}"));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Runner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ args[0] is string eventPath &&
{
nameof(JitDiffJob) => new JitDiffJob(client, metadata),
nameof(FuzzLibrariesJob) => new FuzzLibrariesJob(client, metadata),
nameof(RebaseJob) => new RebaseJob(client, metadata),
var type => throw new NotSupportedException(type),
};

Expand Down
35 changes: 35 additions & 0 deletions Runner/RebaseJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Runner;

internal sealed class RebaseJob : JobBase
{
private string SourceRepo => Metadata["PrRepo"];
private string SourceBranch => Metadata["PrBranch"];

public RebaseJob(HttpClient client, Dictionary<string, string> metadata) : base(client, metadata) { }

protected override async Task RunJobCoreAsync()
{
const string ScriptName = "clone-rebase.bat";

string pushToken = Metadata["MihuBotPushToken"];

File.WriteAllText(ScriptName,
$$"""
git config --system core.longpaths true
git clone --progress https://github.com/dotnet/runtime runtime
cd runtime
git log -1
git config --global user.email mihubot@mihubot.xyz
git config --global user.name MihuBot
git remote add pr https://MihuBot:{{pushToken}}@github.com/{{SourceRepo}}.git
git fetch pr {{SourceBranch}}
git checkout {{SourceBranch}}
git log -1
git rebase main --no-edit
git push pr -f
""");

await RunProcessAsync(ScriptName, string.Empty,
processLogs: line => line.Replace(pushToken, "<REDACTED>", StringComparison.OrdinalIgnoreCase));
}
}

0 comments on commit 2d25593

Please sign in to comment.