From 0fb471409b21eb0b6a95bfee77ee36222ef3bccc Mon Sep 17 00:00:00 2001 From: Amir Moualem Date: Sun, 15 Mar 2020 20:55:19 +0200 Subject: [PATCH] feat: too many changes to explain - consuming this action successfully - turns out I have to use node? rewriting all the things --- .github/workflows/consistency.yaml | 13 -------- .github/workflows/release-notes.yaml | 18 ++++------- .gitignore | 2 ++ .releaserc | 6 ---- action.yaml | 40 ++++++++--------------- dist/index.js | 36 +++++++++++++++++++++ package-lock.json | 48 ++++++++++++++++++++++++++++ package.json | 36 +++++++++++++++++++++ py/main.py | 15 ++++++--- 9 files changed, 152 insertions(+), 62 deletions(-) delete mode 100644 .github/workflows/consistency.yaml delete mode 100644 .releaserc create mode 100644 dist/index.js create mode 100644 package-lock.json create mode 100644 package.json diff --git a/.github/workflows/consistency.yaml b/.github/workflows/consistency.yaml deleted file mode 100644 index 5193d96..0000000 --- a/.github/workflows/consistency.yaml +++ /dev/null @@ -1,13 +0,0 @@ -name: consistency - -on: - pull_request: - branches: [ master ] - -jobs: - assert-consistency: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: assert action updated - run: cmp -s $GITHUB_WORKSPACE/action.yaml $GITHUB_WORKSPACE/.github/workflows/release-notes.yaml diff --git a/.github/workflows/release-notes.yaml b/.github/workflows/release-notes.yaml index 2286f44..5448513 100644 --- a/.github/workflows/release-notes.yaml +++ b/.github/workflows/release-notes.yaml @@ -7,24 +7,18 @@ on: jobs: preview: runs-on: ubuntu-latest - strategy: - matrix: - python-version: [3.8] steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v2 - run: | git fetch --prune --unshallow --tags - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v1 + # TODO: cache node_modules + - run: | + npm i + - uses: ./ with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install requests - - name: Post release notes - run: python $GITHUB_WORKSPACE/py/main.py + releaseBranch: master + githubPosterId: 18642669 env: GITHUB_USERNAME: ${{ github.actor }} GITHUB_TOKEN: ${{ secrets.RELEASE_NOTES_PREVIEW_GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index e69de29..de4d1f0 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,2 @@ +dist +node_modules diff --git a/.releaserc b/.releaserc deleted file mode 100644 index 051776e..0000000 --- a/.releaserc +++ /dev/null @@ -1,6 +0,0 @@ -{ - "plugins": [ - "@semantic-release/github" - ], - "branches": "master" -} \ No newline at end of file diff --git a/action.yaml b/action.yaml index 2286f44..9b4139c 100644 --- a/action.yaml +++ b/action.yaml @@ -1,30 +1,16 @@ name: Release-Notes-Preview +author: 'Amir Moualem' +description: 'Presents a preview of expected release notes should the PR get merged, based on Semantic Release.' -on: - pull_request: - branches: [ master ] +inputs: + releaseBranch: + description: 'Branch where the Semantic Release will occur.' + required: false + default: 'master' + githubPosterId: + description: 'ID of the user associated with the GITHUB_TOKEN. Used to delete previous release notes previews so we dont spam pull requests.' + required: true -jobs: - preview: - runs-on: ubuntu-latest - strategy: - matrix: - python-version: [3.8] - steps: - # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - - uses: actions/checkout@v2 - - run: | - git fetch --prune --unshallow --tags - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v1 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install requests - - name: Post release notes - run: python $GITHUB_WORKSPACE/py/main.py - env: - GITHUB_USERNAME: ${{ github.actor }} - GITHUB_TOKEN: ${{ secrets.RELEASE_NOTES_PREVIEW_GITHUB_TOKEN }} +runs: + using: node12 + main: dist/index.js diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..487341d --- /dev/null +++ b/dist/index.js @@ -0,0 +1,36 @@ +const fs = require('fs'); +const needle = require('needle'); + +const GITHUB_TOKEN = process.env.GITHUB_TOKEN; + +const eventName = process.env.GITHUB_EVENT_NAME; +if (eventName !== 'pull_request') { + process.exit(1); +} + +const eventPath = process.env.GITHUB_EVENT_PATH; +const eventData = fs.readFileSync(eventPath); +const event = JSON.parse(eventData); +console.log(event); + +const issueUrl = event.pull_request.issue_url; + +const url = `${issueUrl}/comments`; +const payload = { + body: 'There\'s butter on my face!', +}; +const options = { + json: true, + compressed: true, + headers: { + Authorization: 'token ' + GITHUB_TOKEN, + }, +}; + + +needle('post', url, payload, options) + .then((response) => { + console.log('huzzah!'); + }).catch((err) => { + console.log('I have no idea what I\'m doing'); + }); diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..3061b1e --- /dev/null +++ b/package-lock.json @@ -0,0 +1,48 @@ +{ + "name": "release-notes-preview", + "requires": true, + "lockfileVersion": 1, + "dependencies": { + "debug": { + "version": "3.2.6", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz", + "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==", + "requires": { + "ms": "^2.1.1" + } + }, + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + }, + "needle": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/needle/-/needle-2.3.3.tgz", + "integrity": "sha512-EkY0GeSq87rWp1hoq/sH/wnTWgFVhYlnIkbJ0YJFfRgEFlz2RraCjBpFQ+vrEgEdp0ThfyHADmkChEhcb7PKyw==", + "requires": { + "debug": "^3.2.6", + "iconv-lite": "^0.4.4", + "sax": "^1.2.4" + } + }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "sax": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", + "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..940f759 --- /dev/null +++ b/package.json @@ -0,0 +1,36 @@ +{ + "name": "release-notes-preview", + "description": "Presents a preview of expected release notes should the PR get merged, based on Semantic Release.", + "license": "MIT", + "main": "dist/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/snyk/release-notes-preview.git" + }, + "author": "Amir Moualem", + "bugs": { + "url": "https://github.com/snyk/release-notes-preview/issues" + }, + "homepage": "https://github.com/snyk/release-notes-preview#readme", + "dependencies": { + "needle": "^2.3.3" + }, + "release": { + "branches": "master", + "verifyConditions": [ + "@semantic-release/github" + ], + "publish": [ + "@semantic-release/github" + ], + "success": [ + "@semantic-release/github" + ], + "fail": [ + "@semantic-release/github" + ] + } +} diff --git a/py/main.py b/py/main.py index f0e92d3..d9079e0 100644 --- a/py/main.py +++ b/py/main.py @@ -8,6 +8,14 @@ if eventName != 'pull_request': sys.exit(0) +RELEASE_BRANCH = os.environ['INPUT_RELEASE_BRANCH'] +if not RELEASE_BRANCH: + print('INPUT_RELEASE_BRANCH is missing') + sys.exit(1) +GITHUB_POSTER_ID = os.environ['INPUT_GITHUB_POSTER_ID'] +if not GITHUB_POSTER_ID: + print('INPUT_GITHUB_POSTER_ID is missing') + sys.exit(1) GITHUB_USERNAME = os.environ.get('GITHUB_USERNAME', 'USER_NOT_FOUND') GITHUB_OWNER_AND_REPO = os.environ['GITHUB_REPOSITORY'] GITHUB_TOKEN = os.environ['GITHUB_TOKEN'] @@ -45,8 +53,7 @@ def main(): # TODO replace login & id for the details of the token commentIds = [ comment['id'] for comment in allCommentsResponse - if comment['user']['login'] == 'snyk-deployer' - and comment['user']['id'] == 18642669 + if comment['user']['id'] == 18642669 and 'Expected release notes' in comment['body'] ] @@ -57,8 +64,8 @@ def main(): headers=headers, ) - # TODO: "master" needs to be parameterised - lastVersion = subprocess.check_output(['git', 'describe', '--abbrev=0', '--tags', 'origin/master'], text=True, stderr=subprocess.STDOUT).strip() + releaseBranchString = 'origin/%s' % RELEASE_BRANCH + lastVersion = subprocess.check_output(['git', 'describe', '--abbrev=0', '--tags', releaseBranchString], text=True, stderr=subprocess.STDOUT).strip() commitsSinceLastVersion = subprocess.check_output(['git', 'log', '--no-decorate', lastVersion + '..HEAD', '--oneline'], text=True).strip() commits = processCommits(commitsSinceLastVersion) if not (commits['features'] or commits['fixes']):