Skip to content

Commit

Permalink
pass through token
Browse files Browse the repository at this point in the history
  • Loading branch information
max-mapper committed May 11, 2021
1 parent 6e6283e commit c3a6933
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ jobs:
- name: Asana Github Pull Request Link
uses: ExodusMovement/[email protected]
with:
token: ${{ secrets.ASANA_TOKEN }}
token: ${{ secrets.ASANA_TOKEN }}
workspace: ${{ secrets.ASANA_WORKSPACE_ID }}
30 changes: 16 additions & 14 deletions asana.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
const { fetchival } = require('@exodus/fetch')
const xmlescape = require('xml-escape')

const fetch = fetchival('https://app.asana.com/api/1.0', {
headers: {
Authorization: 'Bearer ' + process.env.ASANA_ACCESS_TOKEN
}
})
const fetch = (token) => {
return fetchival('https://app.asana.com/api/1.0', {
headers: {
Authorization: 'Bearer ' + token
}
})
}

module.exports.addAsanaComment = async function (gid, comment) {
module.exports.addAsanaComment = async function (token, gid, comment) {
const data = {
'data': {
'is_pinned': true,
'html_text': '<body>' + comment + '</body>'
}
}
const url = 'tasks/' + gid + '/stories'
await fetch(url).post(data)
await fetch(token)(url).post(data)
}

module.exports.searchByDate = async function (before, after) {
const url = 'workspaces/' + process.env.ASANA_WORKSPACE_ID + '/tasks/search' +
module.exports.searchByDate = async function (token, gid, before, after) {
const url = 'workspaces/' + gid + '/tasks/search' +
'?opt_fields=gid,name' +
'&modified_at.before=' + before.toISOString() +
'&modified_at.after=' + after.toISOString() +
'&limit=100' +
'&sort_by=modified_at'
const res = await fetch(url).get()
const res = await fetch(token)(url).get()
if (res) return res.data
else return []
}

module.exports.getMatchingAsanaTask = async function (id) {
module.exports.getMatchingAsanaTask = async function (token, gid, id) {
var d1 = new Date()
var d2 = new Date(d1)
var lookedAt = 0
var callsMade = 0
var hoursInc = 3
while (lookedAt < 10000 && callsMade < 100) {
d2.setHours(d2.getHours() - hoursInc)
let rows = await module.exports.searchByDate(d1, d2)
let rows = await module.exports.searchByDate(token, gid, d1, d2)
callsMade++
lookedAt += rows.length
for (var i = 0; i < rows.length; i++) {
Expand All @@ -51,9 +53,9 @@ module.exports.getMatchingAsanaTask = async function (id) {
return null
}

module.exports.addGithubPrToAsanaTask = async function (title, url, gid) {
module.exports.addGithubPrToAsanaTask = async function (token, gid, title, url) {
const comment = '<strong>Linked PR:</strong> ' + xmlescape(title) + '\n<a href="' + url + '"/>'
await module.exports.addAsanaComment(asanaData.gid, comment)
await module.exports.addAsanaComment(token, gid, comment)
}

module.exports.getAsanaShortId = async function getAsanaShortId (str) {
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const asana = require('./asana')

try {
const ASANA_TOKEN = core.getInput('token')
const WORKSPACE = core.getInput('workspace')
const PR = github.context.payload.pull_request

if (!ASANA_TOKEN){
Expand All @@ -12,7 +13,7 @@ try {

const shortId = asana.getAsanaShortId(PR.title)
if (!shortId) return core.info('no matching asana short id in: ' + PR.title)
const task = asana.getMatchingAsanaTask(shortId)
const task = asana.getMatchingAsanaTask(ASANA_TOKEN, WORKSPACE, shortId)
core.info('got matching task: ' + task)
} catch (error) {
core.error(error.message);
Expand Down

0 comments on commit c3a6933

Please sign in to comment.