├── .travis.yml ├── package.json └── index.js /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: trusty 2 | 3 | language: node_js 4 | 5 | node_js: 6 | - "10" 7 | 8 | cache: 9 | directories: 10 | - node_modules 11 | 12 | install: 13 | - npm install 14 | - npm update 15 | 16 | jobs: 17 | include: 18 | - stage: Test Stage 19 | if: type = pull_request 20 | script: 21 | - npm test:stage 22 | - stage: Test Stage 2 23 | if: type = pull_request 24 | script: 25 | - npm test:stage2 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-status-travis", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "test:stage": "", 9 | "test:stage2": "" 10 | }, 11 | "dependencies": { 12 | "@octokit/rest": "15.15.1", 13 | "args-parser": "1.1.0" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "git+https://github.com/miduga/github-status-travis.git" 18 | }, 19 | "keywords": [], 20 | "author": "", 21 | "license": "ISC", 22 | "bugs": { 23 | "url": "https://github.com/miduga/github-status-travis/issues" 24 | }, 25 | "homepage": "https://github.com/miduga/github-status-travis#readme" 26 | } 27 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | function initGitHubStatus({baseUrl}) { 2 | const octokit = require('@octokit/rest')({ 3 | baseUrl, 4 | timeout: 0, // 0 means no request timeout 5 | headers: { 6 | accept: 'application/vnd.github.v3+json', 7 | 'user-agent': 'octokit/rest.js v1.2.3' // v1.2.3 will be current version 8 | } 9 | }) 10 | 11 | return octokit.authenticate({ 12 | type: 'oauth', 13 | token: process.env.GITHUB_TOKEN 14 | }) 15 | } 16 | 17 | function getCommitSha (eventType) { 18 | if (eventType === 'push') { 19 | return process.env.TRAVIS_COMMIT 20 | } else if (eventType === 'pull_request') { 21 | const travisCommitRange = process.env.TRAVIS_COMMIT_RANGE 22 | const parsed = travisCommitRange.split('...') 23 | 24 | return parsed.length === 1 ? travisCommitRange : parsed[1] 25 | } 26 | 27 | console.error("event type '%s' not supported", eventType) 28 | return null 29 | } 30 | 31 | function extractParamsFromEnv () { 32 | const {env} = process.env 33 | const eventType = env.TRAVIS_EVENT_TYPE 34 | const repoSlug = env.TRAVIS_REPO_SLUG 35 | const [owner, repo] = repoSlug.split('/') 36 | 37 | return { 38 | sha: getCommitSha(eventType), 39 | eventType, 40 | repo, 41 | owner 42 | } 43 | } 44 | 45 | function extractParamsFromArgs () { 46 | return require('args-parser')(process.argv) 47 | } 48 | 49 | octokit.repos 50 | .createStatus({ 51 | ...extractParamsFromEnv(), 52 | ...extractParamsFromArgs() 53 | }) 54 | .then(() => { 55 | console.log('Sent status correctly') 56 | process.exit(0) 57 | }) 58 | .catch(() => { 59 | console.log('Error sending GitHub Status') 60 | process.exit(1) 61 | }) 62 | --------------------------------------------------------------------------------