Skip to content

Commit

Permalink
Merge pull request #56 from open-ish/feat/use-utility
Browse files Browse the repository at this point in the history
feat: use utility on the name
  • Loading branch information
tassioFront authored Nov 6, 2024
2 parents fd81730 + 50f9e10 commit 981a26d
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 79 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/dependabot-pr-manager.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Merge and Close Dependabot PRs

on:
schedule:
- cron: '0 0 2 * *'
workflow_dispatch:
issue_comment:
types: [created]

jobs:
merge-dependabot-prs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install dependencies
run: npm install

- name: Run merge-dependabot-prs
run: |
npx merge-dependabot-prs \
--repoUrl=https://github.com/open-ish/utility.git \
--combinedBranch=ci/combined-dependabot-updates \
--mainBranch=master \
--githubToken=${{ secrets.GH_TOKEN }} \
--repoOwner=open-ish \
--repoName=utility
close-dependabot-prs:
if: github.event.issue.pull_request && contains(github.event.comment.body, '[dependabot-pr-manager] close prs')
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install dependencies
run: npm install

- name: Run close-dependabot-prs
run: |
npx close-dependabot-prs \
--githubToken=${{ secrets.GH_TOKEN }} \
--repoOwner=open-ish \
--repoName=utility
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ It's a monorepo with npm packages, all installable independently

## Packages

- [trycatchfy](./packages/trycatchfy/): Standardize the way you try/catch HTTP request - [example](https://github.com/tassioFront/frontend-pattern/blob/main/src/hooks/useGetGHInfoByUserName/useGetGHInfoByUserName.ts#L47);
- [storagefy](./packages/storagefy/): LocalStorage helper - [example](https://github.com/tassioFront/frontend-pattern/blob/main/src/helpers/useInfo.ts#L6);
- [http-front-cache](./packages/http-front-cache): Provide a simple and efficient way to cache the results of service functions in the browser;
- [trycatchfy](./packages/trycatchfy/README.md): Standardize the way you try/catch HTTP request - [example](https://github.com/tassioFront/frontend-pattern/blob/main/src/hooks/useGetGHInfoByUserName/useGetGHInfoByUserName.ts#L47);
- [storagefy](./packages/storagefy/README.md): LocalStorage helper - [example](https://github.com/tassioFront/frontend-pattern/blob/main/src/helpers/useInfo.ts#L6);
- [http-front-cache](./packages/http-front-cache/README.md): Provide a simple and efficient way to cache the results of service functions in the browser;
- [dependabot-pr-manager](./packages/dependabot-pr-manager/README.md)

## Installing utility packages

Expand Down
11 changes: 0 additions & 11 deletions packages/dependabot-pr-manager/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
# [1.2.0](https://github.com/open-ish/utility/compare/dependabot-pr-manager-v1.1.0...dependabot-pr-manager-v1.2.0) (2024-11-06)


### Bug Fixes

* use auth header on create pull ([6aabcef](https://github.com/open-ish/utility/commit/6aabcef8d7a9b8df58f25423834e17c58067fda0))


### Features

* use lib right name ([7e489af](https://github.com/open-ish/utility/commit/7e489af3dd0ccc2ee1e139fa54ac686251688d26))
135 changes: 76 additions & 59 deletions packages/dependabot-pr-manager/README.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,90 @@
# http front cache
# dependabot-pr-manager

## Installation

Install it on devDependencies. Ex:

```bash
npm i utility-http-front-cache
npm i dependabot-pr-manager -D
```

## Motivation

1. The data to be cached is not too big.
2. The data to be cached is not sensitive.
3. The data to be cached does not change too often.
4. The cache does not need to be updated based on user navigation (e.g., caching cart data is not recommended as the user can add more products and the cache will not be updated).
5. The service parameters do not change too often (if they change too often, the cache will not be used).

## How to Use
`dependabot-pr-manager` is a utility to manage dependabot PRs. It groups DependaBot PRs on the repository and creates a PR with the updated dependencies. It is useful when you have multiple dependabot PRs and you want to merge them all at once.

### Example
### What does it do?

```typescript
import { cacheOnSessionStorage } from 'utility-http-front-cache';
dependabot-pr-manager has two main scripts: `merge-dependabot-pr` and `close-dependabot-prs`.

type Params = [string];
type Result = { data: string[] };
- the `merge-dependabot-prs` groups all Dependabot PRs into a single PR. So, it do not merge it automatically, giving you the opportunity to review/update the changes before merging.
- the `close-dependabot-prs` close all Dependabot prs

const fetchData: ServiceFunction<Params, Result> = async (param: string) => {
const response = await fetch(`https://api.example.com/data?param=${param}`);
return response.json();
};
See the [How to Use](#how-to-use) section for more information on how to use the `dependabot-pr-manager` library in your CI pipeline.

const cachedFetchData = cacheOnSessionStorage(fetchData, 5 * 60 * 1000); // Cache for 5 minutes
## How to Use

// Usage
cachedFetchData('exampleParam').then((result) => {
console.log(result);
});
You can use the `dependabot-pr-manager` library in your CI pipeline to automatically manage and merge Dependabot PRs. Below is an example of how to set up a GitHub Action to run the `dependabot-pr-manager` script on the second day of every month and allow manual triggering via a GitHub button. Additionally, it includes a job to close the Dependabot PRs when the created PR is commented with "[dependabot-pr-manager] close prs".

file example: .github/workflows/dependabot-pr-manager.yml

```yaml
name: Merge and Close Dependabot PRs

on:
schedule:
- cron: '0 0 2 * *' # Runs at 00:00 on the 2nd day of every month, so if your dependabot runs monthly in the first day, all PRs will be merged on the second day. Change to fit your needs.
workflow_dispatch: # Allows manual triggering via GitHub button
issue_comment:
types: [created]

jobs:
merge-dependabot-prs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install dependencies
run: npm install

- name: Run merge-dependabot-prs
run: |
npx merge-dependabot-prs \
--repoUrl=https://github.com/open-ish/utility.git \
--combinedBranch=ci/combined-dependabot-update \
--mainBranch=main \
--githubToken=${{ secrets.YOUR_GIT_HUB_TOKEN }} \
--repoOwner=open-ish \
--repoName=utility
close-dependabot-prs:
if: github.event.issue.pull_request && contains(github.event.comment.body, '[dependabot-pr-manager] close prs')
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install dependencies
run: npm install

- name: Run close-dependabot-prs
run: |
npx close-dependabot-prs \
--repoUrl=https://github.com/open-ish/utility.git \
--githubToken=${{ secrets.YOUR_GIT_HUB_TOKEN }} \
--repoOwner=open-ish \
--repoName=utility
```
## Another Storage

Currently, the `cacheOnSessionStorage` utility only supports [session storage as provider](https://github.com/open-ish/utility/blob/c6d98898bbc6119cd482b736f57ec897443e71de/packages/http-front-cache/src/lib/providers/session-storage.ts#L1-L8). However, it can be easily extended to support other storage mechanisms such as local storage or indexedDB by using the `cacheFactory` function.

```typescript
import { cacheFactory, ServiceFunction } from 'utility-http-front-cache';

const customProvider = {
getItem: (key: string) => Uint8Array; // cacheFactory converts the result from serviceFunction on UInt8Array, so you can assumes that the data returned on getItem is always a UInt8Array. Example of provider here https://github.com/open-ish/utility/blob/c6d98898bbc6119cd482b736f57ec897443e71de/packages/http-front-cache/src/lib/providers/session-storage.ts#L1-L8
setItem: (key: string, value: Uint8Array) => void;
removeItem: (key: string) => void;
};

export const cacheOnMyCustomProvider = <TParams extends unknown[], TResult>(
serviceFunction: ServiceFunction<TParams, TResult>,
expire: number
): ServiceFunction<TParams, TResult> => {
return async (...params: TParams): Promise<TResult> => {
return cacheFactory<TParams, TResult>({
params,
expire,
serviceFunction: fetchData,
provider: customProvider,
});
};
};

// usage
const cachedFetchData = cacheOnMyCustomProvider(fetchData, 5 * 60 * 1000); // Cache for 5 minutes

cachedFetchData('exampleParam').then((result) => {
console.log(result);
});


```
- merge-dependabot-prs Job: This job runs the merge-dependabot-prs script to group and merge Dependabot PRs.
- close-dependabot-prs Job: This job runs the close-dependabot-prs script to close the Dependabot PRs when the pull request created from dependabot-pr-manager be commented with `'[dependabot-pr-manager] close prs'`.
12 changes: 7 additions & 5 deletions packages/dependabot-pr-manager/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "dependabot-pr-manager",
"name": "utility-dependabot-pr-manager",
"version": "0.0.0-semantic-release",
"description": "A script to manager Dependabot PRs",
"type": "commonjs",
Expand All @@ -13,13 +13,15 @@
"dependabot"
],
"scripts": {
"merge-dependabot-prs": "node ./src/lib/script.mjs",
"build": "mkdir -p bin && cp ./src/lib/script.mjs ./bin/merge-dependabot-prs"
"merge-dependabot-prs": "node ./src/lib/merge-dependabot-prs.mjs",
"close-dependabot-prs": "node ./src/lib/close-dependabot-prs.mjs",
"build": "mkdir -p bin && cp ./src/lib/merge-dependabot-prs.mjs ./bin/merge-dependabot-prs"
},
"bin": {
"merge-dependabot-prs": "./src/lib/script.mjs"
"merge-dependabot-prs": "./src/lib/merge-dependabot-prs.mjs",
"close-dependabot-prs": "./src/lib/close-dependabot-prs.mjs"
},
"main": "script.mjs",
"main": "merge-dependabot-prs.mjs",
"license": "MIT",
"devDependencies": {
"@octokit/rest": "19.0.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/dependabot-pr-manager/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './lib/script.mjs';
export * from './lib/merge-dependabot-prs.mjs';
export * from './lib/close-dependabot-prs.mjs';
Loading

0 comments on commit 981a26d

Please sign in to comment.