Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR lifetime lock #23

Open
imranismail opened this issue Oct 17, 2023 · 4 comments
Open

PR lifetime lock #23

imranismail opened this issue Oct 17, 2023 · 4 comments

Comments

@imranismail
Copy link

imranismail commented Oct 17, 2023

I can't get this to work with headless mode and PR lifetime lock. The unlock would trigger for any PR that was closed and because the lock was locked using headless mode, there is nothing that ties the lock to the pull-request.

name: deploy

on:
  issue_comment:
    types: [created]
  pull_request:
    types: [closed]

permissions:
  pull-requests: write
  contents: write

jobs:
  ilock:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'issue_comment' && github.event.issue.pull_request }}
    steps:
      - name: ilock
        uses: github/lock@v2.1.1
        with:
          lock_trigger: /lock
          unlock_trigger: /unlock
          lock_info_alias: /wcid
  unlock:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'pull_request' && github.event.action == 'closed' }}
    steps:
      - name: unlock
        uses: github/lock@v2.1.1
        with:
          mode: unlock
  deploy:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'issue_comment' && github.event.issue.pull_request && contains(github.event.comment.body, '/deploy') }}
    steps:
      - name: acknowledge
        uses: actions/github-script@v6
        with:
          script: |
            github.rest.reactions.createForIssueComment({
              comment_id: context.payload.comment.id,
              owner: context.repo.owner,
              repo: context.repo.repo,
              content: 'eyes',
            });

      - name: lock
        id: lock
        uses: github/lock@v2.1.1
        with:
          mode: check

      - name: locked_comment
        uses: actions/github-script@v6
        if: ${{ steps.lock.outputs.locked == 'true' && steps.lock.outputs.created_by != github.actor }}
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: "This repo is currently locked by ${{ steps.lock.outputs.created_by }}. Please wait for them to finish before trying again."
            });

      - name: acquire_lock
        uses: github/lock@v2.1.1
        if: ${{ steps.lock.outputs.locked == 'false' }}
        with:
          mode: lock

      - name: deploy
        uses: actions/github-script@v6
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: "Deploying!"
            });

      - name: success
        uses: actions/github-script@v6
        if: ${{ success() }}
        with:
          script: |
            github.rest.reactions.createForIssueComment({
              comment_id: context.payload.comment.id,
              owner: context.repo.owner,
              repo: context.repo.repo,
              content: '+1',
            });

      - name: failure
        uses: actions/github-script@v6
        if: ${{ failure() }}
        with:
          script: |
            github.rest.reactions.createForIssueComment({
              comment_id: context.payload.comment.id,
              owner: context.repo.owner,
              repo: context.repo.repo,
              content: '-1',
            });
@GrantBirki
Copy link
Member

👋 @imranismail, happy to help!

Firstly, it should be noted that this Action is considered supplemental to the main github/branch-deploy Action. This Action is mainly used when more advanced or custom "lock" operations are required that the branch-deploy Action cannot handle out-of-the-box. If you are using this Action in conjunction with deployments, I would highly suggest using the github/branch-deploy Action to obtain locks on pull requests as it will add metadata to the lock file that can be used to associate a lock with a pull request. See below:

Example commenting .lock <environment> on a pull request using the github/branch-deploy Action:

image

When performing a "lock" operation on a pull request using the github/branch-deploy Action, a link to the issue comment is left as metadata within the lock file. The link can be used to reference the pull request. Here is a live example:

{
    "reason": null,
    "branch": "test",
    "created_at": "2023-10-25T17:25:28.893Z",
    "created_by": "GrantBirki",
    "sticky": true,
    "environment": "production",
    "global": false,
    "unlock_command": ".unlock production",
    "link": "https://github.com/GrantBirki/actions-sandbox/pull/93#issuecomment-1779733746"
}

@GrantBirki
Copy link
Member

If the above comment does not complete what you are after, changes will likely need to be made to this Action to add more metadata into the lock.json file that will allow some sort of reference or ties back to a pull request.

@imranismail
Copy link
Author

Would you be happy to accept a PR for this?

I don't mind contributing to it, as I had a deep look into the code and found that the metadata was the key to enable this.

@GrantBirki
Copy link
Member

Of course! PRs are always welcome ⭐

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants