diff --git a/.github/workflows/comment_on_first_contribution.yml b/.github/workflows/comment_on_first_contribution.yml new file mode 100644 index 0000000..9865721 --- /dev/null +++ b/.github/workflows/comment_on_first_contribution.yml @@ -0,0 +1,54 @@ +name: Comment on first contribution + +on: + workflow_call: + inputs: + pull_request_message: + description: > + The Markdown message to comment when a contributor creates their first + pull request. + type: string + required: false + issue_message: + description: > + The Markdown message to comment when a contributor creates their first + issue. + type: string + required: false + +jobs: + comment: + if: > + ( + (github.event_name == 'pull_request_target' && inputs.pull_request_message) || + (github.event_name == 'issues' && inputs.issue_message) + ) && + github.event.action == 'opened' && + contains( + fromJSON('["FIRST_TIMER", "FIRST_TIME_CONTRIBUTOR"]'), + github.event_name == 'issues' + && github.event.issue.author_association + || github.event.pull_request.author_association + ) + runs-on: ubuntu-latest + permissions: + issues: write + steps: + - uses: actions/github-script@v7 + env: + # Pass the message via an environment variable rather than + # interpolating to prevent code injection. + message: >- + ${{ + github.event_name == 'issues' + && inputs.issue_message + || inputs.pull_request_message + }} + with: + script: | + await github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: process.env.message + }) diff --git a/README.md b/README.md index 8e7bb99..236a65b 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,36 @@ pre_build_command: "apt-get update -y -q && apt-get install -y -q example" macOS and Windows platform support will be available soon. +### Welcome a first-time contributor + +This is example of a recommended workflow for creating a comment with the +specified Markdown text when a first-time contributor opens an issue or pull +request: + +```yaml +name: Comment on first contribution + +on: + pull_request_target: + types: [opened] + issues: + types: [opened] + +jobs: + comment: + uses: swiftlang/github-workflows/.github/workflows/comment_on_first_contribution.yml@main + with: + pull_request_message: + issue_message: +``` + +The `pull_request_message` or `issue_message` inputs are optional. +If omitted, comments will not be created on pull requests or issues, +respectively. +If you only want to greet first-time contributors on either pull requests or +issues, adjust the events that cause the workflow to run under the `on` keyword +besides omitting the corresponding input. + ## Running workflows locally You can run the Github Actions workflows locally using