forked from ryo-ma/github-profile-trophy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements on workflow and files (ryo-ma#222)
* chore: improvements * chore: add testing workflow * fix: types and lint * chore: change dynamic * chore: improvements * chore: no need repository to check status
- Loading branch information
1 parent
a4d50a1
commit 5dc3379
Showing
15 changed files
with
446 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Check PR Test | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- master | ||
jobs: | ||
install-dependencies: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check if PR is in draft mode | ||
run: | | ||
if [ "${{ github.event.pull_request.draft }}" == "true" ]; then | ||
echo "PR is in draft mode, skipping workflow" | ||
exit 78 | ||
fi | ||
- name: Set up Deno | ||
uses: denolib/setup-deno@v2 | ||
with: | ||
deno-version: "1.37.0" | ||
- name: Run tests | ||
run: deno test --allow-env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { serve } from "https://deno.land/std@0.125.0/http/server.ts"; | ||
import requestHandler from "./index.ts"; | ||
import requestHandler from "./api/index.ts"; | ||
|
||
serve(requestHandler, { port: 8080 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,21 @@ | ||
export { soxa } from "https://deno.land/x/soxa@1.4/mod.ts"; | ||
import { Soxa as ServiceProvider } from "https://deno.land/x/soxa@1.4/src/core/Soxa.ts"; | ||
import { defaults } from "https://deno.land/x/soxa@1.4/src/defaults.ts"; | ||
import { | ||
assertEquals, | ||
assertRejects, | ||
} from "https://deno.land/std@0.203.0/assert/mod.ts"; | ||
import { | ||
assertSpyCalls, | ||
spy, | ||
} from "https://deno.land/std@0.203.0/testing/mock.ts"; | ||
|
||
import { CONSTANTS } from "./src/utils.ts"; | ||
|
||
const baseURL = Deno.env.get("GITHUB_API") || CONSTANTS.DEFAULT_GITHUB_API; | ||
|
||
const soxa = new ServiceProvider({ | ||
...defaults, | ||
baseURL, | ||
}); | ||
|
||
export { assertEquals, assertRejects, assertSpyCalls, soxa, spy }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
export type RetryCallbackProps = { | ||
attempt: number; | ||
}; | ||
|
||
type callbackType<T = unknown> = (data: RetryCallbackProps) => Promise<T> | T; | ||
|
||
async function* createAsyncIterable<T>( | ||
callback: callbackType<T>, | ||
retries: number, | ||
delay: number, | ||
) { | ||
for (let i = 0; i < retries; i++) { | ||
try { | ||
const data = await callback({ attempt: i }); | ||
yield data; | ||
return; | ||
} catch (e) { | ||
yield null; | ||
console.error(e); | ||
await new Promise((resolve) => setTimeout(resolve, delay)); | ||
} | ||
} | ||
} | ||
|
||
export class Retry { | ||
constructor(private maxRetries = 2, private retryDelay = 1000) {} | ||
async fetch<T = unknown>( | ||
callback: callbackType<T>, | ||
) { | ||
for await ( | ||
const callbackResult of createAsyncIterable<T>( | ||
callback, | ||
this.maxRetries, | ||
this.retryDelay, | ||
) | ||
) { | ||
if (callbackResult) { | ||
return callbackResult as T; | ||
} | ||
} | ||
|
||
throw new Error(`Max retries (${this.maxRetries}) exceeded.`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Retry } from "../Retry.ts"; | ||
import { | ||
assertEquals, | ||
assertRejects, | ||
assertSpyCalls, | ||
spy, | ||
} from "../../../deps.ts"; | ||
|
||
type MockResponse = { | ||
value: number; | ||
}; | ||
|
||
Deno.test("Retry.fetch", () => { | ||
const retryInstance = new Retry(); | ||
const callback = spy(retryInstance, "fetch"); | ||
|
||
retryInstance.fetch<MockResponse>(() => { | ||
return { value: 1 }; | ||
}); | ||
|
||
assertSpyCalls(callback, 1); | ||
}); | ||
|
||
Deno.test("Should retry", async () => { | ||
let countErrors = 0; | ||
|
||
const callbackError = () => { | ||
countErrors++; | ||
throw new Error("Panic! Threw Error"); | ||
}; | ||
const retries = 3; | ||
const retryInstance = new Retry(retries); | ||
|
||
await assertRejects( | ||
() => { | ||
return retryInstance.fetch<MockResponse>(callbackError); | ||
}, | ||
Error, | ||
`Max retries (${retries}) exceeded.`, | ||
); | ||
|
||
assertEquals(countErrors, 3); | ||
}); | ||
|
||
Deno.test("Should retry the asyncronous callback", async () => { | ||
let countErrors = 0; | ||
|
||
const callbackError = async () => { | ||
countErrors++; | ||
// Mock request in callback | ||
await new Promise((_, reject) => setTimeout(reject, 100)); | ||
}; | ||
|
||
const retries = 3; | ||
const retryInstance = new Retry(retries); | ||
|
||
await assertRejects( | ||
() => { | ||
return retryInstance.fetch(callbackError); | ||
}, | ||
Error, | ||
`Max retries (${retries}) exceeded.`, | ||
); | ||
|
||
assertEquals(countErrors, 3); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { | ||
GitHubUserActivity, | ||
GitHubUserIssue, | ||
GitHubUserPullRequest, | ||
GitHubUserRepository, | ||
UserInfo, | ||
} from "../user_info.ts"; | ||
|
||
export abstract class GithubRepository { | ||
abstract requestUserInfo(username: string): Promise<UserInfo | null>; | ||
abstract requestUserActivity( | ||
username: string, | ||
): Promise<GitHubUserActivity | null>; | ||
abstract requestUserIssue(username: string): Promise<GitHubUserIssue | null>; | ||
abstract requestUserPullRequest( | ||
username: string, | ||
): Promise<GitHubUserPullRequest | null>; | ||
abstract requestUserRepository( | ||
username: string, | ||
): Promise<GitHubUserRepository | null>; | ||
} | ||
|
||
export class GithubRepositoryService { | ||
constructor(public repository: GithubRepository) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
export const queryUserActivity = ` | ||
query userInfo($username: String!) { | ||
user(login: $username) { | ||
createdAt | ||
contributionsCollection { | ||
totalCommitContributions | ||
restrictedContributionsCount | ||
totalPullRequestReviewContributions | ||
} | ||
organizations(first: 1) { | ||
totalCount | ||
} | ||
followers(first: 1) { | ||
totalCount | ||
} | ||
} | ||
} | ||
`; | ||
|
||
export const queryUserIssue = ` | ||
query userInfo($username: String!) { | ||
user(login: $username) { | ||
openIssues: issues(states: OPEN) { | ||
totalCount | ||
} | ||
closedIssues: issues(states: CLOSED) { | ||
totalCount | ||
} | ||
} | ||
} | ||
`; | ||
|
||
export const queryUserPullRequest = ` | ||
query userInfo($username: String!) { | ||
user(login: $username) { | ||
pullRequests(first: 1) { | ||
totalCount | ||
} | ||
} | ||
} | ||
`; | ||
|
||
export const queryUserRepository = ` | ||
query userInfo($username: String!) { | ||
user(login: $username) { | ||
repositories(first: 100, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}) { | ||
totalCount | ||
nodes { | ||
languages(first: 3, orderBy: {direction:DESC, field: SIZE}) { | ||
nodes { | ||
name | ||
} | ||
} | ||
stargazers { | ||
totalCount | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; |
Oops, something went wrong.