Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V0 #2

Merged
merged 22 commits into from
Jul 3, 2024
Merged

V0 #2

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .checkov.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
skip-check:
- CKV_GHA_7
6 changes: 4 additions & 2 deletions .github/linters/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ rules:
'eslint-comments/no-unused-disable': 'off',
'i18n-text/no-en': 'off',
'import/no-namespace': 'off',
'import/no-commonjs': 'off',
'import/first': 'off',
'no-console': 'off',
'no-unused-vars': 'off',
'prettier/prettier': 'error',
Expand All @@ -63,12 +65,12 @@ rules:
'@typescript-eslint/no-misused-new': 'error',
'@typescript-eslint/no-namespace': 'error',
'@typescript-eslint/no-non-null-assertion': 'warn',
'@typescript-eslint/no-require-imports': 'error',
'@typescript-eslint/no-require-imports': 'off',
'@typescript-eslint/no-unnecessary-qualifier': 'error',
'@typescript-eslint/no-unnecessary-type-assertion': 'error',
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/no-useless-constructor': 'error',
'@typescript-eslint/no-var-requires': 'error',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/prefer-for-of': 'warn',
'@typescript-eslint/prefer-function-type': 'warn',
'@typescript-eslint/prefer-includes': 'error',
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/check-dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ on:
branches:
- main

concurrency:
group: hf-oss-dist-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: read

Expand Down
41 changes: 23 additions & 18 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ on:

permissions:
contents: read
env:
# setting them for the JS tests
OSS_ACCESS_KEY_ID: ${{ secrets.OSS_ACCESS_KEY_ID }}
OSS_ACCESS_KEY_SECRET: ${{ secrets.OSS_ACCESS_KEY_SECRET }}
HF_MODEL_REPO_DIR: model_to_sync

concurrency:
group: hf-oss-ci-${{ github.ref }}
cancel-in-progress: true

jobs:
test-typescript:
Expand Down Expand Up @@ -40,25 +49,21 @@ jobs:
id: npm-lint
run: npm run lint

- run: |
git clone \
https://huggingface.co/J5Tsai/debug-static-files \
${{ env.HF_MODEL_REPO_DIR }}
env:
GIT_LFS_SKIP_SMUDGE: 1

- name: Test
id: npm-ci-test
run: npm run ci-test
env:
HF_TO_OSS_DEBUG_REPO_DIR: ${{ env.HF_MODEL_REPO_DIR }}

test-action:
name: GitHub Actions Test
runs-on: ubuntu-latest

steps:
- name: Checkout
id: checkout
uses: actions/checkout@v4

- name: Test Local Action
id: test-action
uses: ./
with:
milliseconds: 2000

- name: Print Output
id: output
run: echo "${{ steps.test-action.outputs.time }}"
call-workflow-to-sync:
uses: ./.github/workflows/sync.yml
secrets: inherit
with:
hf-repo: J5Tsai/debug-static-files
4 changes: 4 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ on:
schedule:
- cron: '31 7 * * 3'

concurrency:
group: hf-code-ci-${{ github.ref }}
cancel-in-progress: true

permissions:
actions: read
checks: write
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ permissions:
packages: read
statuses: write

concurrency:
group: hf-oss-linter-${{ github.ref }}
cancel-in-progress: true

jobs:
lint:
name: Lint Codebase
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/start-sync.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Start Sync

on:
workflow_dispatch:
inputs:
hf-repo:
required: true
type: string
default: J5Tsai/debug-static-files

permissions:
contents: read

concurrency:
group: start-sync-${{ github.ref }}-${{ github.actor }}-${{ inputs.hf-repo }}
cancel-in-progress: true

jobs:
call-workflow-to-sync:
uses: ./.github/workflows/sync.yml
secrets: inherit
with:
hf-repo: ${{ inputs.hf-repo }}
87 changes: 87 additions & 0 deletions .github/workflows/sync.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Sync to OSS

on:
workflow_call:
inputs:
hf-repo:
required: true
type: string
secrets:
OSS_ACCESS_KEY_ID:
required: true
OSS_ACCESS_KEY_SECRET:
required: true
HF_TOKEN:
required: true

permissions:
contents: read
env:
OSS_ACCESS_KEY_ID: ${{ secrets.OSS_ACCESS_KEY_ID }}
OSS_ACCESS_KEY_SECRET: ${{ secrets.OSS_ACCESS_KEY_SECRET }}
HF_MODEL_REPO_DIR: model_to_sync
HF_USERNAME: J5Tsai

concurrency:
group: hf-oss-sync-${{ github.ref }}-${{ github.actor }}-${{ inputs.hf-repo }}
cancel-in-progress: true

jobs:
list:
name: GitHub Actions Test
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.list-action.outputs.matrix }}

steps:
- name: Checkout
id: checkout
uses: actions/checkout@v4

- name: Checkout Repo
run: |
git clone \
https://${{ env.HF_USERNAME }}:${{ secrets.HF_TOKEN }}@huggingface.co/${{ inputs.hf-repo }} \
${{ env.HF_MODEL_REPO_DIR }}
env:
GIT_LFS_SKIP_SMUDGE: 1
- name: Get HEAD
id: get-head
run: echo "commit=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
working-directory: ${{ env.HF_MODEL_REPO_DIR }}
- name: Upload to oss
id: upload_to_oss
uses: tvrcgo/oss-action@master
with:
key-id: ${{ env.OSS_ACCESS_KEY_ID }}
key-secret: ${{ env.OSS_ACCESS_KEY_SECRET }}
region: oss-cn-beijing
bucket: hf-sync
assets: |
${{ env.HF_MODEL_REPO_DIR }}/**:/${{ inputs.hf-repo }}/${{ steps.get-head.outputs.commit }}/

- name: List files
id: list-action
uses: ./actions/list
with:
repo-dir: ${{ env.HF_MODEL_REPO_DIR }}

sync-oss:
name: '#${{ matrix.task.id }} Sync ${{ matrix.task.size }} to OSS'
runs-on: ubuntu-latest
needs: list
strategy:
fail-fast: false
max-parallel: 12
matrix: ${{ fromJson(needs.list.outputs.matrix) }}
steps:
- name: Checkout
id: checkout
uses: actions/checkout@v4

- name: Sync files
id: list-action
uses: ./actions/sync
with:
urls: ${{ matrix.task.urls }}
hf-token: ${{ secrets.HF_TOKEN }}
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# Create a GitHub Action Using TypeScript
# Sync HF Repository to OSS

## Development

```bash
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/J5Tsai/debug-static-file
```

## Create a GitHub Action Using TypeScript

[![GitHub Super-Linter](https://github.com/actions/typescript-action/actions/workflows/linter.yml/badge.svg)](https://github.com/super-linter/super-linter)
![CI](https://github.com/actions/typescript-action/actions/workflows/ci.yml/badge.svg)
Expand Down
67 changes: 21 additions & 46 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,78 +12,53 @@ import * as main from '../src/main'
// Mock the action's main function
const runMock = jest.spyOn(main, 'run')

// Other utilities
const timeRegex = /^\d{2}:\d{2}:\d{2}/

// Mock the GitHub Actions core library
let debugMock: jest.SpiedFunction<typeof core.debug>
let errorMock: jest.SpiedFunction<typeof core.error>
let getInputMock: jest.SpiedFunction<typeof core.getInput>
let setFailedMock: jest.SpiedFunction<typeof core.setFailed>
let setOutputMock: jest.SpiedFunction<typeof core.setOutput>

describe('action', () => {
beforeEach(() => {
jest.clearAllMocks()

debugMock = jest.spyOn(core, 'debug').mockImplementation()
errorMock = jest.spyOn(core, 'error').mockImplementation()
getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation()
})

it('sets the time output', async () => {
it('sets the file output', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
return '500'
case 'repo-dir':
return getRepoDir()
case 'repo-ref':
return 'main'
case 'action':
return 'list'
case 'max-parallel':
return '2'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(debugMock).toHaveBeenNthCalledWith(1, 'Waiting 500 milliseconds ...')
expect(debugMock).toHaveBeenNthCalledWith(
2,
expect.stringMatching(timeRegex)
)
expect(debugMock).toHaveBeenNthCalledWith(
3,
expect.stringMatching(timeRegex)
)
expect(setOutputMock).toHaveBeenNthCalledWith(1, 'file', '.gitattributes')
expect(setOutputMock).toHaveBeenNthCalledWith(3, 'lfs', 'sd3demo.jpg')
expect(setOutputMock).toHaveBeenNthCalledWith(
1,
'time',
expect.stringMatching(timeRegex)
)
expect(errorMock).not.toHaveBeenCalled()
})

it('sets a failed status', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
return 'this is not a number'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'milliseconds not a number'
4,
'lfs_url',
expect.stringMatching(
/https:\/\/huggingface.co\/J5Tsai\/debug-static-files\/resolve\/.+\/sd3demo.jpg/
)
)
expect(errorMock).not.toHaveBeenCalled()
})
})
function getRepoDir(): string {
const fromEnv = process.env.HF_TO_OSS_DEBUG_REPO_DIR
if (fromEnv) return fromEnv
throw new Error('Function not implemented.')
}
25 changes: 0 additions & 25 deletions __tests__/wait.test.ts

This file was deleted.

Loading
Loading