forked from CodeYourFuture/curriculum
-
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.
clone issues with netlify function... (CodeYourFuture#255)
Adds clone issues functionality. Same as CodeYourFuture#182 but uses netlify functions for backend
- Loading branch information
Showing
20 changed files
with
1,689 additions
and
2 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,8 @@ | ||
# Allow accessing a GitHub bearer token to avoid rate limits when doing HTTP fetches to the GitHub API. | ||
# This can be generated at https://github.com/settings/tokens?type=beta and needs read-only access to all public CYF GitHub repos. | ||
CYF_CURRICULUM_GITHUB_BEARER_TOKEN="" | ||
# Client ID and secret for the GitHub OAuth app used to authenticate users. | ||
GITHUB_CLIENT_ID="clientid" | ||
GITHUB_CLIENT_SECRET="clientsecret" | ||
# The domain of the site, used for generating redirect URLs. | ||
DOMAIN="http://localhost:8888" |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
18 |
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 @@ | ||
legacy-peer-deps=true |
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,27 @@ | ||
## Github OAuth app | ||
|
||
We use Github OAuth app to authenticate users for cloning issues. You can create your own Github OAuth app and use it for your local development. | ||
|
||
Refer to: https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/creating-an-oauth-app | ||
|
||
The only important option from the OAuth app is the `Authorization callback URL`. You need to set it to `http://localhost:8888/api/clone`. This is the endpoint where netlify functions are running (./netlify/functions/clone). | ||
|
||
## Environment Variables | ||
|
||
You need to create a `.env` file in the root of the project. You can refer to `.env.example` file for the required variables. | ||
|
||
## Running the project | ||
|
||
As we use netlify functions, you will need netlify cli to run the project locally. You can install it using the following command: | ||
|
||
```bash | ||
npm install netlify-cli -g | ||
``` | ||
|
||
Once you have netlify cli installed, you can run the project using the following command: | ||
|
||
```bash | ||
netlify dev | ||
``` | ||
|
||
This will start the project on `http://localhost:8888`. |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.c-alert { | ||
background-color: var(--theme-color--paper-fade); | ||
z-index: 2; | ||
position: relative; | ||
padding: var(--theme-spacing--2) var(--theme-spacing--gutter) var(--theme-spacing--3); | ||
width: 100%; | ||
|
||
&__info { | ||
background-color: var(--theme-color--ink-fade); | ||
} | ||
|
||
&__warning { | ||
background-color: var(--theme-color--contrast-max); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[build] | ||
publish = "public" | ||
command = "npm install --legacy-peer-deps && hugo --minify && npx pagefind --source=public" | ||
[dev] | ||
framework = "hugo" | ||
targetPort = 3000 | ||
command = "hugo server -p 3000" | ||
[[redirects]] | ||
from = "/api/*" | ||
to = "/.netlify/functions/:splat" | ||
status = 200 | ||
[functions] | ||
external_node_modules = ["node-fetch"] |
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,78 @@ | ||
import { Handler, HandlerEvent, HandlerResponse } from "@netlify/functions"; | ||
import { githubServiceBuilder } from "../helpers/github"; | ||
import { | ||
checkState, | ||
checkCode, | ||
withHeaders, | ||
makeCookieString, | ||
getTokenFromCookies, | ||
redirect, | ||
clonedMessage, | ||
} from "../helpers/util"; | ||
import config from "../helpers/config"; | ||
import { CloneResponse } from "../helpers/types"; | ||
|
||
const handler: Handler = async (event: HandlerEvent, context) => { | ||
const response: HandlerResponse = { | ||
statusCode: 200, | ||
}; | ||
|
||
// check for state | ||
// this endpoint is only to be used for cloning issues, so we should ignore everything that doesn't have state (see type for state). | ||
const stateOrFail = checkState(event); | ||
if ("statusCode" in stateOrFail) { | ||
return withHeaders(stateOrFail); | ||
} | ||
|
||
// check for auth token in cookies | ||
let token = getTokenFromCookies(event.headers); | ||
|
||
if (!token) { | ||
// check for code in query params | ||
// we fail hard if we don't have a token AND code | ||
const tokenOrFail = await checkCode(event, stateOrFail); | ||
if (typeof tokenOrFail !== "string") { | ||
return tokenOrFail; | ||
} | ||
|
||
token = tokenOrFail; | ||
|
||
const cookieString = makeCookieString(token); | ||
response.headers = { | ||
"Set-Cookie": cookieString, | ||
}; | ||
} | ||
|
||
// if we have a token, we can now clone issues | ||
|
||
const gh = await githubServiceBuilder(token); | ||
|
||
const { module, sprint, issue, prevPath } = stateOrFail; | ||
|
||
const url = new URL(prevPath || "/", config().domain); | ||
|
||
// if issue is defined, we clone just that issue | ||
if (issue) { | ||
await gh.cloneIssue(module, issue).catch((err) => { | ||
console.error(err); | ||
url.searchParams.set("error", err.message); | ||
return redirect(url.toString()); | ||
}); | ||
|
||
url.searchParams.set("message", "Issue cloned successfully"); | ||
|
||
return redirect(url.toString()); | ||
} | ||
|
||
const resp = await gh.cloneAllIssues(module, sprint).catch((err) => { | ||
console.error(err); | ||
url.searchParams.set("error", err.message); | ||
return redirect(url.toString()); | ||
}); | ||
|
||
url.searchParams.set("message", clonedMessage(resp as CloneResponse)); | ||
|
||
return redirect(url.toString()); | ||
}; | ||
|
||
export { handler }; |
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,43 @@ | ||
import Joi from "joi"; | ||
import * as dotenv from "dotenv"; | ||
|
||
dotenv.config(); | ||
|
||
const schema = Joi.object({ | ||
NODE_ENV: Joi.string().valid("dev", "prod", "test").default("dev"), | ||
defaultRepo: Joi.string().default("My-Coursework-Planner"), | ||
defaultOwner: Joi.string().default("CodeYourFuture"), | ||
github: Joi.object({ | ||
oauth: Joi.object({ | ||
clientId: Joi.string().required(), | ||
clientSecret: Joi.string().required(), | ||
}), | ||
}), | ||
domain: Joi.string().default("localhost"), | ||
}); | ||
|
||
const config = () => { | ||
const { value, error } = schema.validate( | ||
{ | ||
NODE_ENV: process.env.NODE_ENV, | ||
defaultRepo: process.env.DEFAULT_REPO, | ||
defaultOwner: process.env.DEFAULT_OWNER, | ||
github: { | ||
oauth: { | ||
clientId: process.env.GITHUB_CLIENT_ID, | ||
clientSecret: process.env.GITHUB_CLIENT_SECRET, | ||
}, | ||
}, | ||
domain: process.env.DOMAIN, | ||
}, | ||
{ abortEarly: false } | ||
); | ||
|
||
if (error) { | ||
throw new Error(`Config validation error: ${error.message}`); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
export default config; |
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,20 @@ | ||
import config from "./config"; | ||
|
||
export const GITHUB_DEFAULT_LABELS = [ | ||
"bug", | ||
"documentation", | ||
"duplicate", | ||
"enhancement", | ||
"good first issue", | ||
"help wanted", | ||
"invalid", | ||
"question", | ||
"wontfix", | ||
]; | ||
|
||
export const COOKIE_NAME = "github-token"; | ||
|
||
export const DEFAULT_HEADERS = { | ||
"Access-Control-Allow-Origin": config().domain, | ||
"Access-Control-Allow-Credentials": true, | ||
}; |
Oops, something went wrong.