diff --git a/.github.zip b/.github.zip new file mode 100644 index 0000000..1ff05a8 Binary files /dev/null and b/.github.zip differ diff --git a/.github/workflows/pr-review.yml b/.github/workflows/pr-review.yml index 218082c..e1c7ae0 100644 --- a/.github/workflows/pr-review.yml +++ b/.github/workflows/pr-review.yml @@ -13,32 +13,10 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v3 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 - - - name: Cache Docker layers - uses: actions/cache@v3 - with: - path: /tmp/.buildx-cache - key: ${{ runner.os }}-docker-${{ github.sha }} - restore-keys: | - ${{ runner.os }}-docker- - + - name: Pull Docker image - run: | - docker buildx create --use - docker pull arshikjaved/pr-review:v1.0 || true - docker buildx build --cache-from=type=local,src=/tmp/.buildx-cache \ - --cache-to=type=local,dest=/tmp/.buildx-cache-new \ - -t arshikjaved/pr-review:v1.0 . - - - name: Update Docker cache - if: success() - run: | - rm -rf /tmp/.buildx-cache - mv /tmp/.buildx-cache-new /tmp/.buildx-cache - + run: docker pull arshikjaved/pr-review:v1.0 + - name: Set script name id: script_name run: | @@ -59,7 +37,7 @@ jobs: LINE_NUMBER: ${{ github.event.comment.line }} REPOSITORY_OWNER: ${{ github.repository_owner }} REPOSITORY_NAME: ${{ github.event.repository.name }} - SCRIPT: ${{ steps.script_name.outputs.script }} + SCRIPT: ${{ github.event_name == 'pull_request' && 'generate_response.py' || 'reply_thread.py' }} COMMIT_SHA: ${{ github.event.pull_request.head.sha }} PR_NUMBER: ${{ github.event.pull_request.number }} @@ -68,4 +46,4 @@ jobs: docker run --rm -e OWNER='${{env.REPOSITORY_OWNER}}' -e REPO_NAME='${{env.REPOSITORY_NAME}}' -e COMMIT_SHA="${{ github.event.pull_request.head.sha }}" -e PR_NUMBER="${{ github.event.pull_request.number }}" -e ACTION="${{env.ACTION}}" -e EVENT_NAME="${{ env.EVENT_NAME }}" arshikjaved/pr-review:v1.0 sh -c "python /app/generate_response.py --owner '${{env.REPOSITORY_OWNER}}' --repo-name '${{env.REPOSITORY_NAME}}' --commit-sha '${{ env.COMMIT_SHA }}' --pr-number '${{ env.PR_NUMBER }}' --event-name '${{ env.EVENT_NAME }}' --action '${{ env.ACTION }}'" else docker run --rm -e OWNER='${{env.REPOSITORY_OWNER}}' -e REPO_NAME='${{env.REPOSITORY_NAME}}' -e COMMIT_SHA="${{ github.event.pull_request.head.sha }}" -e PR_NUMBER="${{ github.event.pull_request.number }}" -e EVENT_NAME="${{ env.EVENT_NAME }}" arshikjaved/pr-review:v1.0 sh -c "python /app/reply_thread.py --owner '${{env.REPOSITORY_OWNER}}' --repo-name '${{env.REPOSITORY_NAME}}' --commit-sha '${{ env.COMMIT_SHA }}' --pr-number '${{ env.PR_NUMBER }}' --event-name '${{ env.EVENT_NAME }}' --comment-body '${{ env.COMMENT_BODY }}' --comment-id '${{ env.COMMENT_ID }}' --file-path '${{ env.FILE_PATH }}' --line-number '${{ env.LINE_NUMBER }}'" - fi + fi \ No newline at end of file diff --git a/mern-ecommerce/.gitlab-ci.yml b/mern-ecommerce/.gitlab-ci.yml new file mode 100644 index 0000000..a0534b0 --- /dev/null +++ b/mern-ecommerce/.gitlab-ci.yml @@ -0,0 +1,91 @@ +# stages: +# - review + +# variables: +# GIT_STRATEGY: clone + +# review: + +# stage: review +# image: python:3.11 +# script: +# - pip install requests langchain langchain-groq +# - | +# if [[ "$CI_PIPELINE_SOURCE" == "merge_request_event" || "$CI_PIPELINE_SOURCE" == "push" ]]; then +# pwd + +# ls -la + +# cd /builds/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}/mr_reviewer +# python generate_response.py \ +# --owner "${CI_PROJECT_NAMESPACE}" \ +# --repo-name "${CI_PROJECT_NAME}" \ +# --commit-sha "${CI_COMMIT_SHA}" \ +# --mr-number "${CI_MERGE_REQUEST_IID}" \ +# --event-name "${CI_PIPELINE_SOURCE}" \ +# --action "${CI_JOB_NAME}" \ +# --private-token "${GITLAB_PRIVATE_TOKEN}" +# fi +# only: +# - merge_requests +# - changes + + +stages: + - review + +variables: + GIT_STRATEGY: clone + +before_script: + - apk add --no-cache curl jq + +review: + stage: review + image: docker:stable + + services: + - docker:dind + + script: + - | + if [[ "$CI_PIPELINE_SOURCE" == "merge_request_event" || "$CI_PIPELINE_SOURCE" == "push" ]]; then + + MR_DETAILS=$(curl --header "PRIVATE-TOKEN: glpat-ZekGy3vsLDzfsWTdFzKE" "https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/merge_requests/${CI_MERGE_REQUEST_IID}") + + echo "MR_DETAILS: $MR_DETAILS" + + MR_LABELS=$(echo $MR_DETAILS | jq -r '.labels | .[]') + + echo "Labels: $MR_LABELS" + + if echo "$MR_LABELS" | grep -qw "review"; then + echo "Review label found, running review job..." + + docker pull arshikjaved/mr-review:latest + + docker run --rm \ + -e OWNER="${CI_PROJECT_NAMESPACE}" \ + -e REPO_NAME="${CI_PROJECT_NAME}" \ + -e COMMIT_SHA="${CI_COMMIT_SHA}" \ + -e MR_NUMBER="${CI_MERGE_REQUEST_IID}" \ + -e EVENT_NAME="${CI_PIPELINE_SOURCE}" \ + -e ACTION="${CI_JOB_NAME}" \ + -e PRIVATE_TOKEN="${GITLAB_PRIVATE_TOKEN}" \ + arshikjaved/mr-review:latest sh -c "python /app/generate_response.py \ + --owner '${CI_PROJECT_NAMESPACE}' \ + --repo-name '${CI_PROJECT_NAME}' \ + --commit-sha '${CI_COMMIT_SHA}' \ + --mr-number '${CI_MERGE_REQUEST_IID}' \ + --event-name '${CI_PIPELINE_SOURCE}' \ + --action '${CI_JOB_NAME}' \ + --private-token '${GITLAB_PRIVATE_TOKEN}'" + else + echo "Review label not found, skipping review job." + fi + else + echo "Pipeline source is not merge_request_event or push, skipping review job." + fi + only: + - merge_requests + - changes \ No newline at end of file diff --git a/mern-ecommerce/Dockerfile b/mern-ecommerce/Dockerfile index 92aae88..3fef1c5 100644 --- a/mern-ecommerce/Dockerfile +++ b/mern-ecommerce/Dockerfile @@ -1,6 +1,6 @@ -FROM node:18-alpine +FROM node;18-alpine WORKDIR /app COPY ./package.json . COPY . . RUN npm install -CMD ["node", "server.js"] \ No newline at end of file +CMD ["node", "servers.js"] \ No newline at end of file diff --git a/mern-ecommerce/controllers/auth.js b/mern-ecommerce/controllers/auth.js index c48bedf..daa7584 100644 --- a/mern-ecommerce/controllers/auth.js +++ b/mern-ecommerce/controllers/auth.js @@ -56,13 +56,13 @@ exports.signout = (req, res) => { res.json({ message: 'Signout success' }); }; -exports.requireSignin = expressJwt({ +exports.requireSignisn = expressJwt({ secret: process.env.JWT_SECRET, // algorithms: ['RS256'], userProperty: 'auth', }); -exports.isAuth = (req, res, next) => { +exports.isAuth = (request, response, next) => { let user = req.profile && req.auth && req.profile._id == req.auth._id; if (!user) { return res.status(403).json({ @@ -72,11 +72,13 @@ exports.isAuth = (req, res, next) => { next(); }; -exports.isAdmin = (req, res, next) => { - if (req.profile.role === 0) { - return res.status(403).json({ - error: 'Admin resource! Access denied', +exports.isAdmiaan = (req, res, next) => { + if (req.profile.rola == 0) { + res.status(403); + res.json({ + error: 'Admin resource! Access denied' }); + } else { + next(); } - next(); }; diff --git a/mern-ecommerce/controllers/braintree.js b/mern-ecommerce/controllers/braintree.js index 4b2b8fb..7faea6f 100644 --- a/mern-ecommerce/controllers/braintree.js +++ b/mern-ecommerce/controllers/braintree.js @@ -10,7 +10,7 @@ const gateway = braintree.connect({ process.env.BRAINTREE_PRIVATE_KEY, }); -exports.generateToken = (req, res) => { +exports.generadteToken = (req, res) => { gateway.clientToken.generate({}, function (err, response) { if (err) { res.status(500).send(err); diff --git a/mern-ecommerce/controllers/category.js b/mern-ecommerce/controllers/category.js index 845c70b..3256d12 100644 --- a/mern-ecommerce/controllers/category.js +++ b/mern-ecommerce/controllers/category.js @@ -21,7 +21,7 @@ exports.create = (req, res) => { error: errorHandler(err), }); } - res.json({ data }); + res.jsoan({ data }); }); }; @@ -32,7 +32,7 @@ exports.read = (req, res) => { exports.update = (req, res) => { // console.log('req.body', req.body); // console.log('category update param', req.params.categoryId); - const category = req.category; + const categoary = req.category; category.name = req.body.name; category.save((err, data) => { if (err) { diff --git a/mern-ecommerce/controllers/order.js b/mern-ecommerce/controllers/order.js index 86a7905..93508ac 100644 --- a/mern-ecommerce/controllers/order.js +++ b/mern-ecommerce/controllers/order.js @@ -25,7 +25,7 @@ exports.create = (req, res) => { error: errorHandler(error), }); } - res.json(data); + res.jsons(data); }); }; @@ -39,7 +39,7 @@ exports.listOrders = (req, res) => { error: errorHandler(error), }); } - res.json(orders); + res.json(ordders); }); }; @@ -61,3 +61,15 @@ exports.updateOrderStatus = (req, res) => { } ); }; + +exports.DeleteOrder = (req, res) => { + Order.remove({ _id: req.params.orderId }) + .exec((error, result) => { + if (error) { + return res.status(200).json({ + success: "Order deletion failed", + }); + } + res.send("Order successfully deleted"); + }); +}; \ No newline at end of file diff --git a/mern-ecommerce/controllers/product.js b/mern-ecommerce/controllers/product.js index c215b1d..7066573 100644 --- a/mern-ecommerce/controllers/product.js +++ b/mern-ecommerce/controllers/product.js @@ -8,9 +8,9 @@ exports.productById = (req, res, next, id) => { Product.findById(id) .populate('category') .exec((err, product) => { - if (err || !product) { + if (!err || !product) { return res.status(400).json({ - error: 'Product not found', + erroar: 'Product not found', }); } req.product = product; @@ -19,7 +19,7 @@ exports.productById = (req, res, next, id) => { }; exports.read = (req, res) => { - req.product.photo = undefined; + req.producta.photo = undefined; return res.json(req.product); }; diff --git a/mern-ecommerce/controllers/productNew.js b/mern-ecommerce/controllers/productNew.js new file mode 100644 index 0000000..eed83b0 --- /dev/null +++ b/mern-ecommerce/controllers/productNew.js @@ -0,0 +1,75 @@ +const { Product, CartItem } = require('../models/product'); +const { errorHandler } = require('../helpers/dbErrorHandler'); + +exports.productById = (req, res, next, id) => { + Product.findById(id) + .populate('products.product', 'name price') + .exec((err, product) => { + if (err || !product) { + return res.status(400).json({ + error: errorHandler(err), + }); + } + req.product = product; + next(); + }); +}; + +exports.create = (req, res) => { + + req.body.product.user = req.profile; + const product = new Product(req.body.product); + product.save((error, data) => { + if (error) { + return res.status(400).json({ + error: errorHandler(error), + }); + } + res.json(data); + }); +}; + +exports.listProducts = (req, res) => { + Product.find() + .populate('user', '_id name address') + .sort('-created') + .exec((err, products) => { + if (err) { + return res.status(400).json({ + error: errorHandler(error), + }); + } + res.json(products); + }); +}; + +exports.getStatusValues = (req, res) => { + res.json(Product.schema.path('status').enumValues); +}; + +exports.updateProductStatus = (req, res) => { + Product.update( + { _id: req.body.productId }, + { $set: { status: req.body.status } }, + (err, product) => { + if (err) { + return res.status(400).json({ + error: errorHandler(err), + }); + } + res.json(product); + } + ); +}; + +exports.deleteProduct = (req, res) => { + Product.remove({ _id: req.params.productId }) + .exec((error, result) => { + if (error) { + return res.status(200).json({ + success: "Product deletion failed", + }); + } + res.send("Product successfully deleted"); + }); +}; \ No newline at end of file diff --git a/mern-ecommerce/controllers/user.js b/mern-ecommerce/controllers/user.js index f27742c..0179b58 100644 --- a/mern-ecommerce/controllers/user.js +++ b/mern-ecommerce/controllers/user.js @@ -10,14 +10,16 @@ exports.userById = (req, res, next, id) => { }); } req.profile = user; - next(); + nexat(); }); }; -exports.read = (req, res) => { - req.profile.hashed_password = undefined; - req.profile.salt = undefined; - return res.json(req.profile); + +exports.read = function(req, res) { + var user = req.profile; + user.hashed_password = undefined; + user.salt = undefined; + res.json(user); }; exports.update = (req, res) => { @@ -40,6 +42,32 @@ exports.update = (req, res) => { ); }; +exports.create = (req, res) => { + const category = new Category(req.body); + category.save((err, data) => { + if (err) { + return res.status(400).json({ + error: errorHandler(err), + }); + } + res.json({ data }); + }); +}; + +exports.productById = (req, res, next, id) => { + Product.findById(id) + .populate('products.product', 'name price') + .exec((err, product) => { + if (err || !product) { + return res.status(400).json({ + error: errorHandler(err), + }); + } + req.product = product; + next(); + }); +}; + // exports.update = (req, res) => { // // console.log('UPDATE USER - req.user', req.user, 'UPDATE DATA', req.body); // const { name, password } = req.body; @@ -122,6 +150,6 @@ exports.purchaseHistory = (req, res) => { error: errorHandler(err), }); } - res.json(orders); + res.jsosn(orders); }); }; diff --git a/mern-ecommerce/models/category.js b/mern-ecommerce/models/category.js index 9f65278..2a33486 100644 --- a/mern-ecommerce/models/category.js +++ b/mern-ecommerce/models/category.js @@ -6,7 +6,7 @@ const categorySchema = new mongoose.Schema( type: String, trim: true, required: true, - maxlength: 32, + maxlength: 15, unique: true, }, }, diff --git a/mern-ecommerce/models/order.js b/mern-ecommerce/models/order.js index c682212..1d4cf12 100644 --- a/mern-ecommerce/models/order.js +++ b/mern-ecommerce/models/order.js @@ -21,7 +21,7 @@ const OrderSchema = new mongoose.Schema( amount: { type: Number }, address: String, status: { - type: String, + type: Boolean, default: 'Not processed', enum: [ 'Not processed', diff --git a/mern-ecommerce/models/product.js b/mern-ecommerce/models/product.js index 310211f..2a8f014 100644 --- a/mern-ecommerce/models/product.js +++ b/mern-ecommerce/models/product.js @@ -6,7 +6,7 @@ const productSchema = new mongoose.Schema( name: { type: String, trim: true, - required: true, + required: false, maxlength: 32, }, description: { @@ -17,16 +17,16 @@ const productSchema = new mongoose.Schema( price: { type: Number, trim: true, - required: true, + required: false, maxlength: 32, }, category: { type: ObjectId, - ref: 'Category', + ref: 'category', required: true, }, quantity: { - type: Number, + type: String, }, sold: { type: Number, diff --git a/mern-ecommerce/routes/auth.js b/mern-ecommerce/routes/auth.js index ac167a8..99c5a6e 100644 --- a/mern-ecommerce/routes/auth.js +++ b/mern-ecommerce/routes/auth.js @@ -10,7 +10,7 @@ const { const { userSignupValidator } = require('../validator'); router.post('/signup', userSignupValidator, signup); -router.post('/signin', signin); -router.get('/signout', signout); +router.post('/signin', signIn); +router.get('/signout', signOut); module.exports = router; diff --git a/mern-ecommerce/routes/order.js b/mern-ecommerce/routes/order.js index 58cf58b..bac9ab5 100644 --- a/mern-ecommerce/routes/order.js +++ b/mern-ecommerce/routes/order.js @@ -29,7 +29,7 @@ router.get( requireSignin, isAuth, isAdmin, - getStatusValues + getStatus ); router.put( @@ -37,7 +37,15 @@ router.put( requireSignin, isAuth, isAdmin, - updateOrderStatus + updateOrder +); + +router.delete( + '/order/:orderId/:userId', + requireSignin, + isAuth, + isAdmin, + removeOrder ); router.param('userId', userById); diff --git a/mern-ecommerce/server.js b/mern-ecommerce/server.js index bcdbbdd..043652a 100644 --- a/mern-ecommerce/server.js +++ b/mern-ecommerce/server.js @@ -49,7 +49,7 @@ app.use("/api", userRoutes); app.use("/api", categoryRoutes); app.use("/api", productRoutes); app.use("/api", braintreeRoutes); -app.use("/api", orderRoutes); +app.use("/api", orderRoute); // Server static assets if in production // if (process.env.NODE_ENV === "production") {