nickytonline / epic-actions

Epic GitHub Actions
MIT License
3 stars 0 forks source link

Create a GitHub action that will Tweet to a GitHub user who contributed their first pull request to a project #4

Open nickytonline opened 2 years ago

nickytonline commented 2 years ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

As a project maintainer, I want a GitHub action that will Tweet to a user who contributed their first pull request to a project.

Describe the solution you'd like A clear and concise description of what you want to happen.

  1. A GitHub action
  2. It should only Tweet if the GitHub user has a Twitter handle in their GitHub profile.

    image

  3. The Tweet should only go out once the pull request is merged.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

N/A

Additional context Add any other context or screenshots about the feature request here.

mtfoley commented 2 years ago

I'm just gonna drop a few notes in here:

Thinking out loud about how this would work, the way actions/first-interaction works is to seek thru prior pull requests (or issues) in the repo and see if any of them had been done by the user. Use case here is a bit different because this would be triggered by merging a PR rather than initially opening it. For that reason, we may be able to draw some inspiration in the way octokit is used and tweak it somewhat.

For the tweeps project and twitter-together they use the paradigm of creating files in a repo in order to trigger the call to Twitter APIs. I believe under the hood they use the Twitter npm package. Probably most straightforward to use the same APIs in that npm package.

mtfoley commented 2 years ago

One approach worth looking at could be whether GitHub search could be leveraged. @nickytonline curious if these links give results for you when you're logged in:

Should be > 0: https://api.github.com/search/issues?q=author:0-vortex%20org:open-sauced%20is:pull-request%20state:open&per_page=100 Might be 0 (at the moment): https://api.github.com/search/issues?q=author:nickytonline%20org:open-sauced%20is:pull-request%20state:open&per_page=100

mtfoley commented 2 years ago

@nickytonline Ok, I am now 100% sure I could not have gotten any appreciable part of this done in a live stream... 🤣

I have a solution at the moment that does work.

My journey so far:

At the moment I've got things working on a private repo of mine with a .github/workflows/*.yml workflow file like this:

name: Tweet First Merge
on:
  pull_request:
    types: [closed]

jobs:
  tweet-first-merge:
    name: "Tweet First Merge"   
    runs-on: ubuntu-latest
    steps:
    - uses: nickytonline/epic-actions/tweet-first-merge@main
      if: github.event.pull_request.merged == true
      id: validate
      with:
        repo-token: "${{ secrets.GITHUB_TOKEN }}"
    - uses: devigned/go-twitter-action@v1.0.2
      if: ${{ steps.validate.outputs.valid == 'true' }}
      with:
        message: ${{ steps.validate.outputs.message }}
        apiKey: ${{ secrets.API_KEY }}
        apiKeySecret: ${{ secrets.API_KEY_SECRET }}
        accessToken: ${{ secrets.ACCESS_TOKEN }}
        accessTokenSecret: ${{ secrets.ACCESS_TOKEN_SECRET }}
mtfoley commented 2 years ago

Alternate path here, just pasting as a reference for another PR. It does basically the same stuff as what I worked on before and I think it would run faster since it's just bash instead of Docker/NodeJS. I've tested this on a private repo.

name: Tweet

on:
  pull_request:
    types: [closed]

jobs:
  tweet-first-merge:
    name: "Tweet First Merge"  
    runs-on: ubuntu-latest
    steps:
    - name: validate
      id: validate
      if: ${{ github.event.pull_request.merged }}
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        TWEET_TEMPLATE: >
          Congrats @%handle% on you first PR!

          %url%
      run: |
        LOGIN=${{ github.event.pull_request.user.login }}
        URL=${{ github.event.pull_request.html_url }}
        REPO=${{ github.event.repository.full_name }}
        HANDLE="$(curl -s H "Authorization: token $GITHUB_TOKEN" https://api.github.com/users/$LOGIN | jq .twitter_username | tr -d \")"
        FIRST="$(curl -s -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/search/issues?q=repo%3A$REPO+author%3A$LOGIN+is%3Apull-request | jq '.total_count <= 1')"
        TWEET=""
        if [ "$HANDLE" != "null" ] && [ "$FIRST" = "true" ]; then
          # need to use different delimiter in sed since URL will have /
          TWEET="$(echo $TWEET_TEMPLATE | sed "s#%handle%#$HANDLE#g; s#%url%#$URL#g")"
        fi
        echo "::set-output name=tweet::$TWEET"

    - name: tweet
      id: tweet
      if: ${{ steps.validate.outputs.tweet != '' }}
      env:
        MESSAGE: ${{ steps.validate.outputs.tweet }}
      run: |
        echo "TWEET MSG: $TWEET"