project.yml
5453 bytes
1name: Project tracking
2on:
3 issues:
4 types: [opened, reopened]
5 pull_request_target:
6 types: [opened, reopened, synchronize]
7
8concurrency:
9 group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
10 cancel-in-progress: true
11
12permissions: {}
13jobs:
14 add-to-project:
15 name: Add to project
16 runs-on: ubuntu-latest
17 if: github.repository == 'ohmyzsh/ohmyzsh'
18 steps:
19 - name: Harden the runner (Audit all outbound calls)
20 uses: step-security/harden-runner@b09bb98e06d4d774595224525879c09bc6e98c40 # v2.20.1
21 with:
22 egress-policy: audit
23 - name: Authenticate as @ohmyzsh
24 id: generate-token
25 uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
26 with:
27 client-id: ${{ secrets.OHMYZSH_CLIENT_ID }}
28 private-key: ${{ secrets.OHMYZSH_APP_PRIVATE_KEY }}
29 - name: Read project data
30 env:
31 GH_TOKEN: ${{ steps.generate-token.outputs.token }}
32 ORGANIZATION: ohmyzsh
33 PROJECT_NUMBER: "1"
34 run: |
35 # Get Project data
36 gh api graphql -f query='
37 query($org: String!, $number: Int!) {
38 organization(login: $org){
39 projectV2(number: $number) {
40 id
41 fields(first:20) {
42 nodes {
43 ... on ProjectV2Field {
44 id
45 name
46 }
47 }
48 }
49 }
50 }
51 }' -f org=$ORGANIZATION -F number=$PROJECT_NUMBER > project_data.json
52
53 # Parse project data
54 cat >> "$GITHUB_ENV" <<EOF
55 PROJECT_ID=$(jq '.data.organization.projectV2.id' project_data.json)
56 PLUGIN_FIELD_ID=$(jq '.data.organization.projectV2.fields.nodes[] | select(.name == "Plugin") | .id' project_data.json)
57 THEME_FIELD_ID=$(jq '.data.organization.projectV2.fields.nodes[] | select(.name == "Theme") | .id' project_data.json)
58 EOF
59 - name: Add to project
60 env:
61 GH_TOKEN: ${{ steps.generate-token.outputs.token }}
62 ISSUE_OR_PR_ID: ${{ github.event.issue.node_id || github.event.pull_request.node_id }}
63 run: |
64 item_id="$(gh api graphql -f query='
65 mutation($project: ID!, $content: ID!) {
66 addProjectV2ItemById(input: {projectId: $project, contentId: $content}) {
67 item {
68 id
69 }
70 }
71 }
72 ' -f project="$PROJECT_ID" -f content="$ISSUE_OR_PR_ID" --jq '.data.addProjectV2ItemById.item.id')"
73
74 echo "ITEM_ID=$item_id" >> $GITHUB_ENV
75 - name: Classify Pull Request
76 if: github.event_name == 'pull_request_target'
77 env:
78 GH_TOKEN: ${{ steps.generate-token.outputs.token }}
79 PR_NUMBER: ${{ github.event.pull_request.number }}
80 run: |
81 # Get the list of modified files in the PR, and extract plugins and themes
82 gh pr view "$PR_NUMBER" \
83 --repo "$GITHUB_REPOSITORY" \
84 --json files --jq '.files.[].path' | awk -F/ '
85 BEGIN {
86 plugins = 0
87 themes = 0
88 }
89 /^plugins\// {
90 if (plugin == $2) next
91 plugin = $2
92 plugins++
93 }
94 /^themes\// {
95 gsub(/\.zsh-theme$/, "", $2)
96 if (theme == $2) next
97 theme = $2
98 themes++
99 }
100 END {
101 # plugin and theme are values controlled by the PR author
102 # so we should sanitize them before using anywhere else
103 if (plugins == 1) {
104 gsub(/[^a-zA-Z0-9._-]/, "", plugin)
105 print "PLUGIN=" plugin
106 }
107 if (themes == 1) {
108 gsub(/[^a-zA-Z0-9._-]/, "", theme)
109 print "THEME=" theme
110 }
111 }
112 ' >> "$GITHUB_ENV"
113 - name: Fill Pull Request fields in project
114 if: github.event_name == 'pull_request_target'
115 env:
116 GH_TOKEN: ${{ steps.generate-token.outputs.token }}
117 run: |
118 gh api graphql -f query='
119 mutation (
120 $project: ID!
121 $item: ID!
122 $plugin_field: ID!
123 $plugin_value: String!
124 $theme_field: ID!
125 $theme_value: String!
126 ) {
127 set_plugin: updateProjectV2ItemFieldValue(input: {
128 projectId: $project
129 itemId: $item
130 fieldId: $plugin_field
131 value: {
132 text: $plugin_value
133 }
134 }) {
135 projectV2Item {
136 id
137 }
138 }
139 set_theme: updateProjectV2ItemFieldValue(input: {
140 projectId: $project
141 itemId: $item
142 fieldId: $theme_field
143 value: {
144 text: $theme_value
145 }
146 }) {
147 projectV2Item {
148 id
149 }
150 }
151 }
152 ' -f project="$PROJECT_ID" -f item="$ITEM_ID" \
153 -f plugin_field="$PLUGIN_FIELD_ID" -f plugin_value="$PLUGIN" \
154 -f theme_field="$THEME_FIELD_ID" -f theme_value="$THEME" \
155 --silent