-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
185 lines (162 loc) · 6.1 KB
/
Copy pathProgram.cs
File metadata and controls
185 lines (162 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using Octokit;
using OpenTap.Diagnostic;
using System.Collections;
using System.Diagnostics;
using PRVersionComment;
using System.IO;
if (args.Length > 0)
{
foreach (var arg in args)
{
var option = arg.Split('=').First();
var value = arg.Substring(2);
if (String.IsNullOrWhiteSpace(value))
continue;
switch (option)
{
case "t":
Config.Token = value;
break;
case "p":
Config.Body = value;
break;
case "i":
Config.IssueBody = value;
break;
case "d":
Config.WorkingDir = value;
break;
}
}
}
else
{
Config.Token = Environment.GetEnvironmentVariable("token") ?? Config.Token;
Config.Body = Environment.GetEnvironmentVariable("body") ?? Config.Body;
Config.IssueBody = Environment.GetEnvironmentVariable("issue-body") ?? Config.IssueBody;
Config.WorkingDir = Environment.GetEnvironmentVariable("working-directory") ?? Config.WorkingDir;
}
if (String.IsNullOrEmpty(Config.Token))
{
Console.WriteLine("::error:: Missing required github token. See expected usage below for how to pass this in.");
Console.WriteLine("::error:: Expected usage is is either:");
Console.WriteLine("::error:: - name: Run comment action");
Console.WriteLine("::error:: uses: AsgerIversen/pr-version-comment");
Console.WriteLine("::error:: with:");
Console.WriteLine("::error:: token: ${{ secrets.GITHUB_TOKEN }}");
Console.WriteLine("::error:: or:");
Console.WriteLine("::error:: - name: Run comment action");
Console.WriteLine("::error:: uses: docker://ghcr.io/asgeriversen/pr-version-comment:main");
Console.WriteLine("::error:: env:");
Console.WriteLine("::error:: token: ${{ secrets.GITHUB_TOKEN }}");
}
if (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_REPOSITORY_OWNER")))
{ // These will be set when running in a github action. When debugging, we use the defaults set in Config
Config.Owner = Environment.GetEnvironmentVariable("GITHUB_REPOSITORY_OWNER");
Config.RepoName = Environment.GetEnvironmentVariable("GITHUB_REPOSITORY").Split("/").Last();
}
// Console.WriteLine("::group::Variables:");
// foreach (DictionaryEntry v in Environment.GetEnvironmentVariables())
// Console.WriteLine($" {v.Key}={v.Value}");
// Console.WriteLine("::endgroup::");
var github = new GitHubClient(new ProductHeaderValue("pr-version-comment"));
github.Credentials = new Credentials(Config.Token);
var repo = await github.Repository.Get(Config.Owner, Config.RepoName);
var repoid = repo.Id;
// find version number for the current commit:
string sha = Environment.GetEnvironmentVariable("GITHUB_SHA");
string version = GetVersion(sha);
var envFile = Environment.GetEnvironmentVariable("GITHUB_OUTPUT");
if (!string.IsNullOrWhiteSpace(envFile) && File.Exists(envFile))
{
using (var f = new StreamWriter(envFile, append: true))
{
f.WriteLine($"version={version}");
}
}
else
{
// fallback
Console.WriteLine($"::set-output name=version::{version}");
}
// Find the PR that created this merge commit (if any)
var prs = await github.PullRequest.GetAllForRepository(repoid, new PullRequestRequest { State = ItemStateFilter.Closed });
PullRequest pullrequest = null;
Console.WriteLine("::group::Searching merged pull requests");
foreach (var pr in prs)
{
if (pr.Merged)
{
if (pr.MergeCommitSha == sha)
{
pullrequest = pr;
}
Console.WriteLine($"{(pullrequest == pr ? "*" : " ")} PR #{pr.Number} : {pr.Title}");
if(pullrequest != null)
// don't print all pull requests after we find the one we need (which is usually one of the first ones)
break;
}
}
Console.WriteLine("::endgroup::");
if (pullrequest != null)
{
int prNum = pullrequest.Number;
Console.WriteLine($"Commenting on PR #{prNum}.");
var body = Config.Body.Replace("{version}", version);
await github.Issue.Comment.Create(repoid, prNum, body);
// Also add comments to linked issues
var issueBody = Config.IssueBody.Replace("{version}", version);
var linkedIssues = await new LinkedIssues().GetLinkedIssues(prNum);
foreach (int issueNumber in linkedIssues)
{
Console.WriteLine($"Commenting on linked issue #{issueNumber}.");
await github.Issue.Comment.Create(repoid, issueNumber, issueBody);
}
}
else
{
Console.WriteLine($"This commit was not a merge commit for a PR. No action taken.");
}
string GetVersion(string sha)
{
var test = OpenTap.PluginManager.DirectoriesToSearch;
if (test.Count > 0)
{
var l = new VersionLogListener();
OpenTap.Log.AddListener(l);
var action = new OpenTap.Package.GitVersionAction();
action.Sha = sha;
action.RepoPath = Path.Combine(Directory.GetCurrentDirectory(),Config.WorkingDir);
Console.WriteLine($"::group::running tap sdk gitversion {sha} --dir {action.RepoPath}");
action.Execute(CancellationToken.None);
OpenTap.Log.Flush();
OpenTap.Log.RemoveListener(l);
Console.WriteLine("::endgroup::");
return l.Version?.ToString();
}
return null;
}
class VersionLogListener : OpenTap.Diagnostic.ILogListener
{
public OpenTap.SemanticVersion Version { get; private set; }
public void EventsLogged(IEnumerable<Event> Events)
{
foreach (Event e in Events)
{
if ((OpenTap.LogEventType)e.EventType == OpenTap.LogEventType.Debug)
Console.Write($"::debug:: ");
if ((OpenTap.LogEventType)e.EventType == OpenTap.LogEventType.Warning)
Console.Write($"::warning:: ");
if ((OpenTap.LogEventType)e.EventType == OpenTap.LogEventType.Error)
Console.Write($"::error:: ");
Console.WriteLine($"{e.Message}");
if (e.Source == "GitVersion" && OpenTap.SemanticVersion.TryParse(e.Message, out var ver))
{
Version = ver;
}
}
}
public void Flush()
{
}
}