Skip to content

Commit

Permalink
Update pattini with the new pattini contract (#30)
Browse files Browse the repository at this point in the history
* fix: new pattini contract

* fix: get abi and contract address

* fix: name contractFetch

* fix: inform the user of the state of the call on chain

* fix: error management

* fix: lint

* fix lint

---------

Co-authored-by: theocdl <theo.claudel@edu.ece.fr>
  • Loading branch information
theocdl and theocdl authored Mar 24, 2024
1 parent 958a19d commit 25fd3db
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 59 deletions.
33 changes: 30 additions & 3 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
import * as main from '../src/main'
import * as core from '@actions/core'

// Mock the action's entrypoint
const runMock = jest.spyOn(main, 'run').mockImplementation()

// Mock the GitHub Actions core library
let getInputMock: jest.SpiedFunction<typeof core.getInput>

describe('main', () => {
beforeEach(() => {
jest.clearAllMocks()

getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
})

it('TO DO', async () => {
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('../src/index')
getInputMock.mockImplementation(name => {
switch (name) {
case 'ISSUE_NUMBER':
return '1234-5678'
case 'PULL_REQUEST_NUMBER':
return '1234'
case 'PRIVATE_KEY':
return '0x1234567890abcdef'
case 'ACTION':
return 'test'
case 'REPOSITORY':
return 'my-org/my-repo'
case 'GITHUB_TOKEN':
return 'my-token'
default:
return ''
}
})

expect(runMock).toHaveBeenCalled()
await main.run()
expect(runMock).toHaveReturned()
})
})
33 changes: 11 additions & 22 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 10 additions & 34 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ export async function run(): Promise<void> {
let abi = ''

const contractFetch = await fetch(
`https://raw.githubusercontent.com/${repository}/test/.github/workflows/pattini.config.json`
`https://raw.githubusercontent.com/${repository}/main/.github/workflows/pattini.config.json`
)
const contract = await contractFetch.text()
try {
const contractJSON = JSON.parse(contract)
contractAddress = contractJSON.address
abi = contractJSON.abi
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
} catch (errorContract) {
if (errorContract instanceof Error) core.setFailed(errorContract.message)
}

const provider = new ethers.JsonRpcProvider(
Expand All @@ -60,8 +60,8 @@ export async function run(): Promise<void> {
)
const issue = await response.json()
const issueDescription = issue.body

const issueDescriptionSplit = issueDescription.split('\n')

for (let i = 0; i < issueDescriptionSplit.length; i++) {

Check warning on line 65 in src/main.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Expected a `for-of` loop instead of a `for` loop with this simple iteration

Check warning on line 65 in src/main.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Expected a `for-of` loop instead of a `for` loop with this simple iteration
const line = issueDescriptionSplit[i]
const [key, value] = line.split(':').map((item: string) => item.trim())
Expand All @@ -72,39 +72,15 @@ export async function run(): Promise<void> {
}
}

console.log('action:', action)
console.log('issueNumber:', issueNumber)
console.log('contractAddress:', contractAddress)
console.log('recipientAddress:', recipientAddress)
console.log('privateKey:', privateKey)
console.log('amount:', amount)

const take = await pattini.take(
issueNumber,
amount,
'previousCommitHash',
recipientAddress
)

const take = await pattini.take(issueNumber, amount, recipientAddress)
const takeReceipt = await take.wait(1)
console.log('take:', takeReceipt.hash)
const message = `The wallet address ${recipientAddress} has been set to an issue. https://sepolia.etherscan.io/tx/${takeReceipt.hash}`
console.log(message)
} else if (action === 'pull_request') {
console.log('action:', action)
console.log('issueNumber:', issueNumber)
console.log('contractAddress:', contractAddress)
console.log('pullRequestNumber:', pullRequestNumber)
console.log('recipientAddress:', recipientAddress)
console.log('privateKey:', privateKey)

const pay = await pattini.pay(
issueNumber,
parseInt(pullRequestNumber),
'commitHashNew'
)
console.log('pay:', pay.hash)
const pay = await pattini.pay(issueNumber, parseInt(pullRequestNumber))
const message = `The person who created the ${issueNumberDataSplit} branch has just received a reward. https://sepolia.etherscan.io/tx/${pay.hash}`
console.log(message)
}

// core.setOutput('payReceipt', payReceipt.hash)
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
}
Expand Down

0 comments on commit 25fd3db

Please sign in to comment.