diff --git a/.github/workflows/actions/notify-slack/action.yml b/.github/actions/notify-slack/action.yml similarity index 100% rename from .github/workflows/actions/notify-slack/action.yml rename to .github/actions/notify-slack/action.yml diff --git a/.github/octokit/index.js b/.github/octokit/index.js new file mode 100644 index 00000000..b308cb3b --- /dev/null +++ b/.github/octokit/index.js @@ -0,0 +1,30 @@ +import {Octokit} from "@octokit/rest"; +import {createAppAuth} from "@octokit/auth-app" + +export const getAccessToken = async () => { + + const {GITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY} = process.env + + const octoKitInstance = new Octokit({ + authStrategy: createAppAuth, + auth: { + appId: GITHUB_APP_ID, + privateKey: GITHUB_APP_PRIVATE_KEY + } + }); + + const {data: installations} = await octoKitInstance.rest.apps.listInstallations() + + console.log("installations -----", installations); + + + if(!installations.length) { + throw new Error("No Installations found for this github app") + } + + const installationId = installations[0].id; + + const installationAccessToken = await octoKitInstance.rest.apps.createInstallationAccessToken({installation_id: installationId}) + + return installationAccessToken.data.token +} \ No newline at end of file diff --git a/.github/octokit/package.json b/.github/octokit/package.json new file mode 100644 index 00000000..66fb13ca --- /dev/null +++ b/.github/octokit/package.json @@ -0,0 +1,16 @@ +{ + "name": "xero-octokit", + "version": "1.0.0", + "description": "", + "main": "index.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "@octokit/auth-app": "^7.1.1", + "@octokit/rest": "^21.0.2" + } + } \ No newline at end of file diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 05860d66..6ea5b553 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -10,6 +10,7 @@ on: jobs: publish: runs-on: ubuntu-latest + environment: prod outputs: release_number: ${{steps.get_latest_release_number.outputs.release_tag}} permissions: @@ -106,3 +107,54 @@ jobs: button_type: "danger" package_version: ${{needs.publish.outputs.release_number}} repo_link: ${{github.server_url}}/${{github.repository}} + + notify-codegen-repo: + needs: publish + if: always() + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + repository: XeroAPI/xero-python + path: xero-python + + - name: Install octokit dependencies + run: npm i + working-directory: xero-python/.github/octokit + + - name: Get github app access token + id: get_access_token + env: + GITHUB_APP_ID: ${{ secrets.XERO_CODEGEN_BOT_APPLICATION_ID }} + GITHUB_APP_PRIVATE_KEY: ${{ secrets.XERO_CODEGEN_BOT_APPLICATION_KEY }} + uses: actions/github-script@v7 + with: + result-encoding: string + script: | + const { getAccessToken } = await import('${{ github.workspace }}/xero-python/.github/octokit/index.js') + const token = await getAccessToken() + return token + + - name: Notify codegen repo + run: | + curl -X POST -H "Authorization: token ${{ steps.get_access_token.outputs.result }}" \ + -H "Accept: application/vnd.github.v3+json" \ + -H "Content-Type: application/json" \ + https://api.github.com/repos/xero-internal/xeroapi-sdk-codegen/actions/workflows/notify-sdk-publish.yml/dispatches \ + -d '{ + "ref": "master", + "inputs": { + "commit": "${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}", + "status": "${{needs.publish.result}}", + "deployer": "xero-codegen-bot", + "url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}", + "environment": "prod", + "sdk_type": "python", + "cab_key": "${{ github.event.inputs.cab_id }}" + } + }'