-
Notifications
You must be signed in to change notification settings - Fork 2
/
Github.cs
101 lines (92 loc) · 3.68 KB
/
Github.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using Octokit;
namespace GithubUtils;
class Github
{
public static GithubEnvironment Env = new GithubEnvironment();
static Github()
{
Client = new GitHubClient(new ProductHeaderValue("issue-in-project"));
}
public static GitHubClient Client
{
get; private set;
}
private static Repository _Repository;
public static Repository Repository
{
get
{
if (_Repository is null)
{
var repo = Environment.GetEnvironmentVariable("GITHUB_REPOSITORY");
_Repository = Client.Repository.Get(repo.Split("/").First(), repo.Split("/").Last()).Result;
}
return _Repository;
}
}
public static Project GetProject(string projectName)
{
string owner = projectName.Split("/")[0];
string repo = projectName.Split("/")[1];
var id = Int64.Parse(projectName.Split("/")[2]);
var projects = Github.Client.Repository.Project.GetAllForRepository(owner, repo).Result;
return projects.FirstOrDefault(p => p.Number == id);
}
public static T GetEvent<T>()
{
string jsonString = File.ReadAllText(Env.EventPath);
return JsonSerializer.Deserialize<T>(jsonString);
}
}
class GithubEnvironment
{
/// <summary>
/// Always set to true when GitHub Actions is running the workflow. You can use this variable to differentiate when
/// tests are being run locally or by GitHub Actions.
/// </summary>
public bool Actions => Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true";
/// <summary>
/// The owner and repository name. For example, octocat/Hello-World.
/// </summary>
public string Repository => Environment.GetEnvironmentVariable("GITHUB_REPOSITORY");
/// <summary>
/// The type of ref that triggered the workflow run. Valid values are branch or tag.
/// </summary>
public string RefType => Environment.GetEnvironmentVariable("GITHUB_REF_TYPE");
/// <summary>
/// The branch or tag ref that triggered the workflow run. For branches this is the format refs/heads/<branch_name>,
/// and for tags it is refs/tags/<tag_name>. This variable is only set if a branch or tag is available for the event
/// type. For example, refs/heads/feature-branch-1.
/// </summary>
public string Ref => Environment.GetEnvironmentVariable("GITHUB_REF");
public string EventName => Environment.GetEnvironmentVariable("GITHUB_EVENT_NAME");
/// <summary>
/// The path to the file on the runner that contains the full event webhook payload. For example, /github/workflow/event.json.
/// </summary>
public string EventPath => Environment.GetEnvironmentVariable("GITHUB_EVENT_PATH");
/// <summary>
/// The branch or tag name that triggered the workflow run. For example, feature-branch-1.
/// </summary>
public string RefName => Environment.GetEnvironmentVariable("GITHUB_REF_NAME");
/// <summary>
/// true if branch protections are configured for the ref that triggered the workflow run.
/// </summary>
public bool RefProtected => Environment.GetEnvironmentVariable("GITHUB_REF_PROTECTED") == "true";
/// <summary>
/// The commit SHA that triggered the workflow. For example, ffac537e6cbbf934b08745a378932722df287a53.
/// </summary>
public string Sha => Environment.GetEnvironmentVariable("GITHUB_SHA");
}
class GithubConsole
{
public static void WriteOutput(string outputName, string value)
{
Console.WriteLine($"::set-output name={outputName}::{value}");
}
}