-
Notifications
You must be signed in to change notification settings - Fork 7
/
findRefOnGithub.test.js
164 lines (148 loc) · 4.51 KB
/
findRefOnGithub.test.js
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
const nock = require("nock");
nock.disableNetConnect();
const debug = require("debug")("pin-github-action-test");
const run = require("./findRefOnGithub");
const findRef = (action) => {
return run.apply(null, [action, debug]);
};
const action = {
owner: "nexmo",
repo: "github-actions",
path: "",
pinnedVersion: "master",
currentVersion: "master",
};
afterEach(() => {
if (!nock.isDone()) {
throw new Error(
`Not all nock interceptors were used: ${JSON.stringify(
nock.pendingMocks()
)}`
);
}
nock.cleanAll();
});
test("looks up a specific hash", async () => {
const testAction = {
...action,
pinnedVersion: "73549280c1c566830040d9a01fe9050dae6a3036",
};
mockRefLookupFailure(
testAction,
"tags/73549280c1c566830040d9a01fe9050dae6a3036"
);
mockRefLookupFailure(
testAction,
"heads/73549280c1c566830040d9a01fe9050dae6a3036"
);
mockCommitLookupSuccess(
testAction,
"73549280c1c566830040d9a01fe9050dae6a3036"
);
const actual = await findRef(testAction);
expect(actual).toEqual("73549280c1c566830040d9a01fe9050dae6a3036");
});
test("looks up a tag", async () => {
const testAction = { ...action, pinnedVersion: "v1" };
mockTagRefLookupSuccess(
testAction,
"tags/v1",
"73549280c1c566830040d9a01fe9050dae6a3036"
);
mockTagLookupSuccess(
testAction,
"73549280c1c566830040d9a01fe9050dae6a3036",
"62ffef0ba7de4e1410b3c503be810ec23842e34a"
);
const actual = await findRef(testAction);
expect(actual).toEqual("62ffef0ba7de4e1410b3c503be810ec23842e34a");
});
test("looks up a branch", async () => {
mockRefLookupFailure(action, "tags/master");
mockBranchRefLookupSuccess(
action,
"heads/master",
"73549280c1c566830040d9a01fe9050dae6a3036"
);
const actual = await findRef(action);
expect(actual).toEqual("73549280c1c566830040d9a01fe9050dae6a3036");
});
test("fails to find ref (404)", () => {
mockRefLookupFailure(action, "tags/master");
mockRefLookupFailure(action, "heads/master");
mockCommitLookupFailure(action, "master");
return expect(findRef(action)).rejects.toEqual(
`Unable to find SHA for nexmo/github-actions@master\nPrivate repos require you to set process.env.GH_ADMIN_TOKEN to fetch the latest SHA`
);
});
test("fails to find ref (rate limiting)", () => {
mockRefLookupFailure(action, "tags/master");
mockRefLookupFailure(action, "heads/master");
mockCommitLookupRateLimit(action, "master");
return expect(findRef(action)).rejects.toEqual(
`Unable to find SHA for nexmo/github-actions@master\nAPI rate limit exceeded for 1.2.3.4. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)`
);
});
function mockRefLookupFailure(action, path) {
path = path.replace("/", "%2F");
nock("https://api.github.com")
.get(`/repos/${action.owner}/${action.repo}/git/ref/${path}`)
.reply(404);
}
function mockBranchRefLookupSuccess(action, path, sha) {
path = path.replace("/", "%2F");
const data = {
ref: `refs/${path}`,
object: {
sha: sha,
type: "commit",
},
};
nock("https://api.github.com")
.get(`/repos/${action.owner}/${action.repo}/git/ref/${path}`)
.reply(200, data);
}
function mockTagRefLookupSuccess(action, path, sha) {
path = path.replace("/", "%2F");
const data = {
ref: `refs/${path}`,
object: {
sha: sha,
type: "tag",
},
};
nock("https://api.github.com")
.get(`/repos/${action.owner}/${action.repo}/git/ref/${path}`)
.reply(200, data);
}
function mockTagLookupSuccess(action, tagSha, commitSha) {
const data = {
object: {
sha: commitSha,
type: "commit",
},
};
nock("https://api.github.com")
.get(`/repos/${action.owner}/${action.repo}/git/tags/${tagSha}`)
.reply(200, data);
}
function mockCommitLookupSuccess(action, commitSha) {
nock("https://api.github.com")
.get(`/repos/${action.owner}/${action.repo}/commits/${commitSha}`)
.reply(200, {
sha: commitSha,
});
}
function mockCommitLookupFailure(action, commitSha) {
nock("https://api.github.com")
.get(`/repos/${action.owner}/${action.repo}/commits/${commitSha}`)
.reply(404);
}
function mockCommitLookupRateLimit(action, commitSha) {
nock("https://api.github.com")
.get(`/repos/${action.owner}/${action.repo}/commits/${commitSha}`)
.reply(
429,
"API rate limit exceeded for 1.2.3.4. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)"
);
}