-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: replace API with octokit (#9)
- Loading branch information
1 parent
d3c5a7d
commit 997e5c8
Showing
375 changed files
with
75,372 additions
and
7,436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module.exports = { | ||
'env': { | ||
'commonjs': true, | ||
'es6': true, | ||
'node': true | ||
}, | ||
'extends': 'eslint:recommended', | ||
'globals': { | ||
'Atomics': 'readonly', | ||
'SharedArrayBuffer': 'readonly' | ||
}, | ||
'parserOptions': { | ||
'ecmaVersion': 2018 | ||
}, | ||
'rules': { | ||
'indent': [ | ||
'error', | ||
4 | ||
], | ||
'linebreak-style': [ | ||
'error', | ||
'unix' | ||
], | ||
'quotes': [ | ||
'error', | ||
'single' | ||
], | ||
'semi': [ | ||
'error', | ||
'never' | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,65 @@ | ||
/** | ||
* API: https://docs.github.com/en/rest/reference/repos#get-a-branch | ||
*/ | ||
const axios = require('axios') | ||
const path = require('path') | ||
const core = require('@actions/core') | ||
const { connected } = require('process') | ||
|
||
async function checkBranch( | ||
{ Authorization, owner, repo, branchName } | ||
) { | ||
|
||
// load api url from context | ||
let BASE_URL = process.env.GITHUB_API_URL | ||
core.debug(`Using API url: ${BASE_URL}`) | ||
core.debug(`Checking if branch exists with name ${branchName} in repo ${repo}`) | ||
|
||
const url = | ||
BASE_URL + | ||
path.posix.join( | ||
`/repos/${owner}/${repo}/branches` | ||
) | ||
core.debug(`Request URL: ${url}`) | ||
// load all branches, since we need the info about the default branch as well | ||
const res = await axios({ | ||
method: 'get', | ||
url, | ||
responseType: 'application/json', | ||
headers: { | ||
Authorization, | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
.then(({data}) => { | ||
// result succesful | ||
let jsonResult = JSON.stringify(data); | ||
core.debug(`Succesful API call with result: ${jsonResult}`) | ||
return { data: data } | ||
}) | ||
.catch(err => { | ||
if (err.toString() !== 'Error: Request failed with status code 404') { | ||
console.log(err) | ||
} | ||
// errors should not happen | ||
return { data: { } } | ||
}) | ||
let branches = res.data | ||
|
||
let json = JSON.stringify(branches); | ||
core.debug(`Branches: ${json}`) | ||
const core = require('@actions/core') | ||
const github = require('@actions/github') | ||
|
||
if (branches == null) { | ||
core.debug(`Error loading existing branches from API`) | ||
return null | ||
} | ||
async function checkBranch({ token, owner, repo, branchName }) { | ||
const octokit = github.getOctokit(token) | ||
|
||
// check if the branch name already exists | ||
let branch = branches.find(function(branch) { return branch.name === branchName }) | ||
core.debug( | ||
`Checking if branch exists with name ${branchName} in repo ${repo}` | ||
) | ||
|
||
if (branch) { | ||
core.debug(`Branch with name ${branchName} already exists, continuing as normal`) | ||
return { } | ||
} | ||
else { | ||
console.log(`Need to create a new branch first with name ${branchName}`) | ||
let defaultBranch = branches[0] | ||
console.log(`Found default branch to branch of: ${defaultBranch.name} with sha: ${defaultBranch.commit.sha}`) | ||
// load all branches, since we need the info about the default branch as well | ||
const { data: branches } = await octokit.rest.repos | ||
.listBranches({ owner, repo }) | ||
.catch(({ status, data, message }) => { | ||
core.error('Error loading existing branches from API') | ||
core.debug(`status: ${status}`) | ||
core.error(JSON.stringify(data)) | ||
throw new Error(message) | ||
}) | ||
core.debug(`Branches: ${JSON.stringify(branches)}`) | ||
|
||
let branchCreateUrl = BASE_URL + | ||
path.posix.join( | ||
`/repos/${owner}/${repo}/git/refs` | ||
) | ||
core.debug(`Request URL to create new branch: ${branchCreateUrl}`) | ||
if (!branches || !branches.length) { | ||
throw new Error('No branches found') | ||
} | ||
|
||
return axios({ | ||
method: 'post', | ||
url: branchCreateUrl, | ||
responseType: 'application/json', | ||
headers: { | ||
Authorization, | ||
'Content-Type': 'application/json' | ||
}, | ||
data: { | ||
ref: `refs/heads/${branchName}`, | ||
sha: defaultBranch.commit.sha | ||
} | ||
}).then(({ data }) => { | ||
core.debug(`Branch with name ${defaultBranch.name} created`) | ||
// return non empty object to check on | ||
console.log(`Created new branch with ref: ${data.ref} based on ${defaultBranch.name}`) | ||
return { } | ||
}).catch(err => { | ||
core.debug(`Error creatng new branch: ${err}`) | ||
console.log(`Error creating the branch with name ${branchName} and sha ${defaultBranch.commit.sha}: ${error}`) | ||
return null | ||
// check if the branch name already exists | ||
let branch = branches.find(function (branch) { | ||
return branch.name === branchName | ||
}) | ||
|
||
if (branch) { | ||
core.debug( | ||
`Branch with name ${branchName} already exists, continuing as normal` | ||
) | ||
core.debug('✔️ Check branch Done') | ||
return branch | ||
} else { | ||
core.debug(`Need to create a new branch first with name ${branchName}`) | ||
let defaultBranch = branches[0] | ||
core.debug( | ||
`Found default branch to branch of: ${defaultBranch.name} with sha: ${defaultBranch.commit.sha}` | ||
) | ||
|
||
// Docs: https://octokit.github.io/rest.js/v18#git-create-ref | ||
const { data } = await octokit.rest.git | ||
.createRef({ | ||
owner, | ||
repo, | ||
ref: `refs/heads/${branchName}`, | ||
sha: defaultBranch.commit.sha, | ||
}) | ||
.catch(err => { | ||
core.error(`Error creatng new branch: ${err}`) | ||
throw err | ||
}) | ||
core.debug( | ||
`Created new branch with ref: ${data.ref} based on ${defaultBranch.name}` | ||
) | ||
} | ||
} | ||
module.exports = checkBranch | ||
core.debug('✔️ Check branch Done') | ||
} | ||
module.exports = checkBranch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.