Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
kolpav committed Mar 2, 2020
0 parents commit 04c636a
Show file tree
Hide file tree
Showing 23 changed files with 38,901 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
lib/
node_modules/
64 changes: 64 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"plugins": ["jest", "@typescript-eslint"],
"extends": ["plugin:github/es6"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"project": "./tsconfig.json"
},
"rules": {
"eslint-comments/no-use": "off",
"import/no-namespace": "off",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/explicit-member-accessibility": [
"error",
{ "accessibility": "no-public" }
],
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/ban-ts-ignore": "error",
"camelcase": "off",
"@typescript-eslint/camelcase": "error",
"@typescript-eslint/class-name-casing": "error",
"@typescript-eslint/explicit-function-return-type": [
"error",
{ "allowExpressions": true }
],
"@typescript-eslint/func-call-spacing": ["error", "never"],
"@typescript-eslint/generic-type-naming": ["error", "^[A-Z][A-Za-z]*$"],
"@typescript-eslint/no-array-constructor": "error",
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-extraneous-class": "error",
"@typescript-eslint/no-for-in-array": "error",
"@typescript-eslint/no-inferrable-types": "error",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-object-literal-type-assertion": "error",
"@typescript-eslint/no-unnecessary-qualifier": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-useless-constructor": "error",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-for-of": "warn",
"@typescript-eslint/prefer-function-type": "warn",
"@typescript-eslint/prefer-includes": "error",
"@typescript-eslint/prefer-interface": "error",
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/promise-function-async": "error",
"@typescript-eslint/require-array-sort-compare": "error",
"@typescript-eslint/restrict-plus-operands": "error",
"semi": "off",
"@typescript-eslint/semi": ["error", "never"],
"@typescript-eslint/type-annotation-spacing": "error",
"@typescript-eslint/unbound-method": "error"
},
"env": {
"node": true,
"es6": true,
"jest/globals": true
}
}
35 changes: 35 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: 'build-test'
on:
pull_request:
push:
branches:
- master
- 'releases/*'

jobs:
artifact:
runs-on: ubuntu-latest
steps:
- name: Generate artifact to delete
run: |
echo $(date) > date.txt
- name: Upload artifact
uses: actions/upload-artifact@v1
with:
name: artifact
path: date.txt
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: |
yarn
yarn run all
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: ./
with:
token: ${{ secrets.GITHUB_TOKEN }}
expire-in: '30minutes'
99 changes: 99 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Dependency directory
node_modules

# Rest pulled from https://github.com/github/gitignore/blob/master/Node.gitignore
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# OS metadata
.DS_Store
Thumbs.db

# Ignore built ts files
__tests__/runner/*
lib/**/*
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12.16.1
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
lib/
node_modules/
9 changes: 9 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": false,
"singleQuote": true,
"trailingComma": "none",
"arrowParens": "avoid"
}
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

The MIT License (MIT)

Copyright (c) 2018 GitHub, Inc. and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Delete artifacts action

Action responsible for deleting old artifacts by setting expire duration.

Hopefuly this is just temporary solution till github implements this functionality natively.

## Inputs
### `expire-in`
**Required** for how long the artifacts should be kept.
Most of the human readable formats are supported `10 minutes`, `1hr 20mins`, `1week`.
Take a look at [parse-duration](https://github.com/jkroso/parse-duration) for more information.


## Outputs
### `deleted-artifacts`
Serialized list of deleted artifacts. Empty `[]` when nothing is deleted

## Usage

Run this action as cron. This won't delete artifacts of running workflows because they
are persisted after workflow completion.

```yaml
name: 'Delete old artifacts'
on:
schedule:
- cron: '0 * * * *' # every hour

jobs:
delete-artifacts:
runs-on: ubuntu-latest
steps:
- uses: kolpav/purge-artifacts-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
expire-in: 7days # Setting this to 0 will delete all artifacts
```
## Contributing
There are few improvements to be made, namely
- More delete strategies (name, size, number of occurences, regex match on name etc..)
- Better test coverage
5 changes: 5 additions & 0 deletions __mocks__/@actions/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const core = jest.genMockFromModule("@actions/core")

core.debug = console.log

module.exports = core
8 changes: 8 additions & 0 deletions __mocks__/@actions/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const github = jest.genMockFromModule('@actions/github')

github.context.repo = {
repo: "https://github.com/kolpav/purge-artifacts-action",
owner: "kolpav"
}

module.exports = github
25 changes: 25 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { main, shouldDelete } from '../src/main'
import { sub } from 'date-fns'
import { IActionInputs } from '../src/utils'

describe('shouldDelete', () => {
test('expired', () => {
const days = 2
const expireInMs = days * 86400000
const expiredArtifact = { created_at: sub(new Date(), { days }) }
const actionInptus: IActionInputs = { expireInMs }
expect(shouldDelete(expiredArtifact as any, actionInptus)).toEqual(true)
})
test('not expired', () => {
const days = 2
const expireInMs = (days + 1) * 86400000
const expiredArtifact = { created_at: sub(new Date(), { days }) }
const actionInptus: IActionInputs = { expireInMs }
expect(shouldDelete(expiredArtifact as any, actionInptus)).toEqual(false)
})
test('expired when expireInDays is zero', () => {
const expiredArtifact = { created_at: new Date() }
const actionInptus: IActionInputs = { expireInMs: 0 }
expect(shouldDelete(expiredArtifact as any, actionInptus)).toEqual(true)
})
})
60 changes: 60 additions & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { eachArtifact } from '../src/utils'

describe('eachArtifact', () => {
test('called with correct arguments', async () => {
const octokit = {
actions: {
listArtifactsForRepo: jest.fn(async () => ({
data: {
artifacts: [],
total_count: 0
}
}))
}
}
for await (const artifact of eachArtifact(octokit as any)) {
}
expect(octokit.actions.listArtifactsForRepo).toBeCalledWith({
owner: 'kolpav',
repo: 'https://github.com/kolpav/purge-artifacts-action',
page: 1,
// eslint-disable-line @typescript-eslint/camelcase
per_page: 100
})
})
test('iterates over all artifacts', async () => {
const maxPerPage = 100
const totalCount = 117
const artifacts = []
for (let i = 0; i < totalCount; i++) {
artifacts[i] = i
}
const firstListArtifactsForRepoResponse = {
data: {
artifacts: artifacts.slice(0, maxPerPage),
// eslint-disable-line @typescript-eslint/camelcase
total_count: totalCount
}
}
const secondListArtifactsForRepoResponse = {
data: {
artifacts: artifacts.slice(maxPerPage, artifacts.length),
// eslint-disable-line @typescript-eslint/camelcase
total_count: totalCount
}
}
const listArtifactsForRepoMock = jest
.fn()
.mockResolvedValueOnce(firstListArtifactsForRepoResponse)
.mockResolvedValueOnce(secondListArtifactsForRepoResponse)
const octokit = {
actions: {
listArtifactsForRepo: listArtifactsForRepoMock
}
}
let artifactIndex = 0
for await (const artifact of eachArtifact(octokit as any)) {
expect(artifact).toEqual(artifacts[artifactIndex++])
}
})
})
Loading

0 comments on commit 04c636a

Please sign in to comment.