forked from mathieudutour/github-tag-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.ts
More file actions
94 lines (81 loc) · 2.22 KB
/
Copy pathgithub.ts
File metadata and controls
94 lines (81 loc) · 2.22 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
import { context, getOctokit } from '@actions/github';
import * as core from '@actions/core';
let octokitSingleton: ReturnType<typeof getOctokit> | undefined;
interface Tag {
name: string;
commit: {
sha: string;
url: string;
};
zipball_url: string;
tarball_url: string;
node_id: string;
}
export function getOctokitSingleton(): ReturnType<typeof getOctokit> {
if (octokitSingleton) {
return octokitSingleton;
}
const githubToken = core.getInput('github_token');
octokitSingleton = getOctokit(githubToken);
return octokitSingleton;
}
/**
* Fetch all tags for a given repository recursively.
*/
export async function listTags(
shouldFetchAllTags = false,
fetchedTags: Tag[] = [],
page = 1
): Promise<Tag[]> {
const octokit = getOctokitSingleton();
const tags = await octokit.rest.repos.listTags({
...context.repo,
per_page: 100,
page,
});
if (tags.data.length < 100 || !shouldFetchAllTags) {
return [...fetchedTags, ...tags.data];
}
return listTags(shouldFetchAllTags, [...fetchedTags, ...tags.data], page + 1);
}
/**
* Compare `headRef` to `baseRef` (i.e. baseRef...headRef).
* @param baseRef - old commit
* @param headRef - new commit
*/
export async function compareCommits(baseRef: string, headRef: string) {
const octokit = getOctokitSingleton();
core.debug(`Comparing commits (${baseRef}...${headRef})`);
const commits = await octokit.rest.repos.compareCommits({
...context.repo,
base: baseRef,
head: headRef,
});
return commits.data.commits;
}
export async function createTag(
newTag: string,
createAnnotatedTag: boolean,
GITHUB_SHA: string
): Promise<void> {
const octokit = getOctokitSingleton();
let annotatedTag:
| Awaited<ReturnType<typeof octokit.rest.git.createTag>>
| undefined;
if (createAnnotatedTag) {
core.debug(`Creating annotated tag.`);
annotatedTag = await octokit.rest.git.createTag({
...context.repo,
tag: newTag,
message: newTag,
object: GITHUB_SHA,
type: 'commit',
});
}
core.debug(`Pushing new tag to the repo.`);
await octokit.rest.git.createRef({
...context.repo,
ref: `refs/tags/${newTag}`,
sha: annotatedTag ? annotatedTag.data.sha : GITHUB_SHA,
});
}