forked from actions/publish-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-utils.ts
121 lines (108 loc) · 2.84 KB
/
api-utils.ts
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
import * as core from '@actions/core';
import {context} from '@actions/github';
import {GitHub} from '@actions/github/lib/utils';
import {HttpClient} from '@actions/http-client';
interface GitRef {
ref: string;
node_id: string;
url: string;
object: {
type: string;
sha: string;
url: string;
};
}
interface ErrorStatus extends Error {
status?: number;
}
async function findTag(
tag: string,
octokitClient: InstanceType<typeof GitHub>
): Promise<GitRef | null> {
try {
const {data: foundTag} = await octokitClient.rest.git.getRef({
...context.repo,
ref: `tags/${tag}`
});
return foundTag;
} catch (err) {
if ((err as ErrorStatus).status === 404) {
return null;
} else {
throw new Error(
`Retrieving refs failed with the following error: ${err}`
);
}
}
}
async function getTagSHA(
tag: string,
octokitClient: InstanceType<typeof GitHub>
): Promise<string> {
const foundTag = await findTag(tag, octokitClient);
if (!foundTag) {
throw new Error(`The '${tag}' tag does not exist in the remote repository`);
}
return foundTag.object.sha;
}
export async function validateIfReleaseIsPublished(
tag: string,
octokitClient: InstanceType<typeof GitHub>
): Promise<void> {
try {
const {data: foundRelease} = await octokitClient.rest.repos.getReleaseByTag(
{
...context.repo,
tag
}
);
if (foundRelease.prerelease) {
throw new Error(
`The '${foundRelease.name}' release is marked as pre-release. Updating tags for pre-release is not supported`
);
}
} catch (err) {
if ((err as ErrorStatus).status === 404) {
throw new Error(`No GitHub release found for the ${tag} tag`);
} else {
throw new Error(
`Retrieving releases failed with the following error: ${err}`
);
}
}
}
export async function updateTag(
sourceTag: string,
targetTag: string,
octokitClient: InstanceType<typeof GitHub>
): Promise<void> {
const sourceTagSHA = await getTagSHA(sourceTag, octokitClient);
const foundTargetTag = await findTag(targetTag, octokitClient);
const refName = `tags/${targetTag}`;
if (foundTargetTag) {
core.info(
`Updating the '${targetTag}' tag to point to the '${sourceTag}' tag`
);
await octokitClient.rest.git.updateRef({
...context.repo,
ref: refName,
sha: sourceTagSHA,
force: true
});
} else {
core.info(`Creating the '${targetTag}' tag from the '${sourceTag}' tag`);
await octokitClient.rest.git.createRef({
...context.repo,
ref: `refs/${refName}`,
sha: sourceTagSHA
});
}
}
export async function postMessageToSlack(
slackWebhook: string,
message: string
): Promise<void> {
const jsonData = {text: message};
const http = new HttpClient();
await http.postJson(slackWebhook, jsonData);
}