Repository: mheap/pin-github-action (js)

Issue:
Private repos require you to set process.env.GH_ADMIN_TOKEN to fetch the latest SHA
Hellos @mheap. Thanks for this excellent tool.

I am running it against build files in a public repo and getting an unexpected error:
```
Unable to find SHA for actions/checkout@v2
Private repos require you to set process.env.GH_ADMIN_TOKEN to fetch the latest SHA
```

These are the specific actions triggering the issue:
```
Unable to find SHA for actions/checkout@v2
Unable to find SHA for actions/github-script@v3
Unable to find SHA for actions/github-script@v3.1.0
Unable to find SHA for dorny/paths-filter@v2
Unable to find SHA for hmarr/debug-action@v2
```

My understanding is that these are not private repos and shouldn't require a token. `uses: actions/checkout@main` is an example in your own home page. 

I can work around the issue for now, but it does complicate my team's usage of the tool so it's worth asking about.


Pull request:
Add error handling for rate limits
Resolves #73 

Patch:
diff --git a/findRefOnGithub.js b/findRefOnGithub.js
index 4992823..cc8eadf 100644
--- a/findRefOnGithub.js
+++ b/findRefOnGithub.js
@@ -9,6 +9,8 @@ module.exports = function (action) {
     const repo = action.repo;
     const pinned = action.pinnedVersion;
 
+    let error;
+
     // In order: Tag, Branch
     const possibleRefs = [`tags/${pinned}`, `heads/${pinned}`];
     for (let ref of possibleRefs) {
@@ -39,6 +41,7 @@ module.exports = function (action) {
       } catch (e) {
         // We can ignore failures as we validate later
         //console.log(e);
+        error = handleCommonErrors(e);
       }
     }
 
@@ -53,10 +56,22 @@ module.exports = function (action) {
     } catch (e) {
       // If it's not a commit, it doesn't matter
       //console.log(e);
+      error = handleCommonErrors(e);
     }
 
     return reject(
-      `Unable to find SHA for ${owner}/${repo}@${pinned}\nPrivate repos require you to set process.env.GH_ADMIN_TOKEN to fetch the latest SHA`
+      `Unable to find SHA for ${owner}/${repo}@${pinned}\n${error}`
     );
   });
 };
+
+function handleCommonErrors(e) {
+  if (e.status == 404) {
+    return "Private repos require you to set process.env.GH_ADMIN_TOKEN to fetch the latest SHA";
+  }
+
+  if (e.message.includes("API rate limit exceeded")) {
+    return e.message;
+  }
+  return;
+}


Test patch:
diff --git a/findRefOnGithub.test.js b/findRefOnGithub.test.js
index 6458b03..75c1ce3 100644
--- a/findRefOnGithub.test.js
+++ b/findRefOnGithub.test.js
@@ -69,7 +69,7 @@ test("looks up a branch", async () => {
   expect(actual).toEqual("73549280c1c566830040d9a01fe9050dae6a3036");
 });
 
-test("fails to find ref", () => {
+test("fails to find ref (404)", () => {
   mockRefLookupFailure(action, "tags/master");
   mockRefLookupFailure(action, "heads/master");
   mockCommitLookupFailure(action, "master");
@@ -78,6 +78,15 @@ test("fails to find ref", () => {
   );
 });
 
+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")
@@ -139,3 +148,12 @@ function mockCommitLookupFailure(action, commitSha) {
     .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.)"
+    );
+}